pythonarrow-python

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?


Solution

  • 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