1
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?

Kshitij Mittal
  • 2,698
  • 3
  • 25
  • 40
  • `timestamp` is UTC. – pvg Sep 13 '17 at 18:44
  • Okay, so how should I get number of seconds from Jan 1 1970, in my local time? – Kshitij Mittal Sep 13 '17 at 18:48
  • Added suggestion to my answer. – meisen99 Sep 13 '17 at 18:50
  • @KshitijMittal you almost certainly shouldn't. – pvg Sep 13 '17 at 18:52
  • @KshitijMittal it would be helpful to explain why you think you need the timestamp (which by definition is in UTC) in a local time. There might be better methods in Arrow (and general good practices working with local times) to acheive your end result. Can you edit your question to clarify? – meisen99 Sep 13 '17 at 18:59
  • OK - check my answer, it should give you the result you need: take a UTC date, convert to Kolkata time (+5:30) but then treat that as UTC. – meisen99 Sep 13 '17 at 19:04
  • Alternately, if it's always Kolkata time,you can just add 5.5 hours to the timestamp by adding 5.5 * 3600 (the number of seconds in the timezone offset) – meisen99 Sep 13 '17 at 19:05
  • So, the reason why I am using this is I am using an API. It takes start and end timestamps. Earlier, I was using this code to generate current timestamp: int((datetime.now() - datetime(1970,1,1)).total_seconds() However, when I changed it to arrow.utcnow().timestamp, I realized, it takes the difference of number of seconds in Asia/Kolkata timezone, instead of utc. – Kshitij Mittal Sep 13 '17 at 19:07
  • What is the API? And how do you know it doesn't take UTC timestamps, since timestamps are generally UTC? – pvg Sep 13 '17 at 19:08
  • The API returns data generated for increasing timestamps. When I send it the utc timestamp, it returns data of 5.5 hours less. – Kshitij Mittal Sep 13 '17 at 19:09

2 Answers2

2

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
meisen99
  • 576
  • 4
  • 16
0

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/

pvg
  • 2,673
  • 4
  • 17
  • 31