Kshitij Mittal
Kshitij Mittal

Reputation: 2776

Timestamp with utc and any other timezone is coming out to be same with arrow

import arrow    
print arrow.utcnow()
print arrow.utcnow().timestamp
print arrow.utcnow().to('Asia/Kolkata')
print arrow.utcnow().to('Asia/Kolkata').timestamp

I need the timestamp (in int) of 'Asia/Kolkata' timezone, which is +5:30 from utc.

arrow.utcnow() and arrow.utcnow().to('Asia/Kolkata') are coming out to be different and the second one is +5:30 the first, as expected.

However, arrow.utcnow().timestamp and arrow.utcnow().to('Asia/Kolkata').timestamp are still coming out to be same.

I am sure I am missing something very basic here, but can anyone explain this?

Upvotes: 1

Views: 1686

Answers (2)

meisen99
meisen99

Reputation: 576

I think "timestamp", by definition, is always in UTC:

The Unix time (or Unix epoch or POSIX time or Unix timestamp) is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.

If you take your localized time string, convert it to a UTC date time (that is, 5pm Kolkata time becomes 5pm UTC), then you can get a timestamp that corresponds to the local clock time. Example:

import arrow    
print arrow.utcnow()
print arrow.utcnow().timestamp
kolkata = arrow.utcnow().to('Asia/Kolkata')
print kolkata.replace(tzinfo='UTC').timestamp

Upvotes: 2

pvg
pvg

Reputation: 2729

Timestamps are UTC, this is also described in the Arrow docs

timestamp

Returns a timestamp representation of the Arrow object, in UTC time.

Arrow will let you convert a non-UTC timestamp into arrow time but it won't let you shoot yourself in the foot by generating non-UTC timestamps for you.

classmethod fromtimestamp(timestamp, tzinfo=None)

Constructs an Arrow object from a timestamp, converted to the given timezone.

Parameters: timestamp – an int or float timestamp, or a str that converts to either. tzinfo – (optional) a tzinfo object. Defaults to local time. Timestamps should always be UTC. If you have a non-UTC timestamp:

arrow.Arrow.utcfromtimestamp(1367900664).replace(tzinfo='US/Pacific') <Arrow [2013-05-07T04:24:24-07:00]>

The full docs are here:

http://arrow.readthedocs.io/en/latest/

Upvotes: 0

Related Questions