Get UTC timestamp in python with date

Is there a way to get the UTC timestamp by specifying a date? What I expect:

datetime(2008, 1, 1, 0, 0, 0, 0) 

should lead to

  1199145600 

Creating a naive datetime object means that there is no time zone information. If I look at the documentation for datetime.utcfromtimestamp, creating a UTC timestamp means giving up timezone information. Therefore, I assume that creating a naive datetime object (like me) will lead to a UTC timestamp. However:

 then = datetime(2008, 1, 1, 0, 0, 0, 0) datetime.utcfromtimestamp(float(then.strftime('%s'))) 

leads to

 2007-12-31 23:00:00 

Is there any hidden timezone information in the datetime object? What am I doing wrong?

+72
python datetime timestamp utc
Feb 21 2018-11-21T00:
source share
7 answers

What is a naive datetime ?

By default, datetime objects are considered “naive”: they store time information without time zone information. Think of a naive datetime as a relative number (i.e.: +4 ) without a clear origin (in fact, your origin will be spread all over your system boundary). Think of the value of datetime as absolute numbers (i.e.: 8 ) with a common origin for the whole world.

Without information about the time zone, you cannot convert a “naive” datetime to some non-naive representation of time (where +4 sets if we don’t know where to start?). This is why you cannot use the datetime.datetime.toutctimestamp() method. (cf: http://bugs.python.org/issue1457227 )

To check if your datetime dt naive, check dt.tzinfo , if None , then it is naive:

 datetime.now() ## DANGER: returns naïve datetime pointing on local time datetime(1970, 1, 1) ## returns naïve datetime pointing on user given time 

I have naive dates, what can I do?

You have to make an assumption depending on your specific context: The question you need to ask yourself is: is this your datetime in UTC? or was it local time?

  • If you used UTC (you have problems):

     import calendar def dt2ts(dt): """Converts a datetime object to UTC timestamp naive datetime will be considered UTC. """ return calendar.timegm(dt.utctimetuple()) 
  • If you have NOT used UTC , welcome to hell.

    You should make your datetime non-naive before using the old function by returning their estimated time zone.

    You will need the time zone name and information about if the DST was in effect when creating the target naive datetime (the latest DST information is required for corner cabinets):

     import pytz ## pip install pytz mytz = pytz.timezone('Europe/Amsterdam') ## Set your timezone dt = mytz.normalize(mytz.localize(dt, is_dst=True)) ## Set is_dst accordingly 

    The consequences of not providing is_dst :

    Do not use is_dst will generate the wrong time (and UTC timestamp) if the target time was created when the reverse DST was applied (for example, changing the DST time by deleting one hour).

    Providing the wrong is_dst , of course, will result in the wrong time (and the UTC timestamp) only when the DST or holes overlap. And when collateral is also the wrong time occurring in the “holes” (the time that never existed due to the DST shift forward), is_dst will give an interpretation of how to count this fictitious time, and this is the only case when .normalize(..) really something will do, since then translate it as the actual real time (change the date and time AND DST, if necessary). Note that .normalize() not required because there is a UTC timestamp at the end, but it is probably recommended if you do not like the idea of ​​having dummy times in your variables, especially if you are reusing this variable elsewhere.

    and AVOID USING THE FOLLOWING : (cf: Datetime timezone conversion using pytz )

     dt = dt.replace(tzinfo=timezone('Europe/Amsterdam')) ## BAD !! 

    Why? because .replace() blindly replaces tzinfo without taking into account the target time and selects a bad DST object. While .localize() uses the target time and your is_dst hint to select the correct DST object.

OLD is the wrong answer (thanks @JFSebastien for this):

Hopefully it's pretty easy to guess the time zone (your local origin) when you create your naive datetime object, since it is related to the system configuration, which you hope DO NOT change between the naive creation of the datetime object and the moment you want to get the UTC timestamp. This trick can be used to ask an imperfect question.

Using time.mktime , we can create utc_mktime :

 def utc_mktime(utc_tuple): """Returns number of seconds elapsed since epoch Note that no timezone are taken into consideration. utc tuple must be: (year, month, day, hour, minute, second) """ if len(utc_tuple) == 6: utc_tuple += (0, 0, 0) return time.mktime(utc_tuple) - time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0)) def datetime_to_timestamp(dt): """Converts a datetime object to UTC timestamp""" return int(utc_mktime(dt.timetuple())) 

You must ensure that your datetime object is created in the same time zone as your created datetime .

This last decision is incorrect because it assumes that UTC is offset from the same time as UTC offset from EPOCH. This is not the case for many time zones (at some point of the year for daylight saving time offsets).

+79
Mar 31 '11 at 12:33
source share

Another possibility:

 d = datetime.datetime.utcnow() epoch = datetime.datetime(1970,1,1) t = (d - epoch).total_seconds() 

This works with both "d" and "epoch" - naive datetimes, which makes the "-" operator valid and returns the interval. total_seconds() turns the interval into seconds. Note that total_seconds() returns a float, even d.microsecond == 0

+24
Feb 28 '14 at 16:57
source share

Also notice the calendar.timegm () function, as described in this blog post:

 import calendar calendar.timegm(utc_timetuple) 

The output should match the vaab solution.

+19
Jul 11 2018-12-12T00:
source share

If the input datetime object is in UTC:

 >>> dt = datetime(2008, 1, 1, 0, 0, 0, 0) >>> timestamp = (dt - datetime(1970, 1, 1)).total_seconds() 1199145600.0 

Note: it returns a float, i.e. microseconds are represented as fractions of a second.

If the input date object is in UTC:

 >>> from datetime import date >>> utc_date = date(2008, 1, 1) >>> timestamp = (utc_date.toordinal() - date(1970, 1, 1).toordinal()) * 24*60*60 1199145600 

See Converting datetime.date to a UTC timestamp in Python for more details.

+12
Nov 16 '12 at 19:25
source share

It seems to me that the main answer is still not so clear, and it is worth spending time understanding time and timezones .

The most important thing to understand when working with time is that time is relative !

  • 2017-08-30 13:23:00 : (naive datetime), represents local time somewhere in the world, but note that 2017-08-30 13:23:00 in London is NOT THE TIME like 2017-08-30 13:23:00 in San Francisco.

Since the same time line can be interpreted as different points depending on where you are in the world, there is a need for an absolute understanding of time.

A UTC timestamp is the number in seconds (or milliseconds) of an Epoch (defined as 1 January 1970 00:00:00 GMT time zone +00: 00 offset).

The era is tied to the GMT time zone and, therefore, is an absolute point in time. The UTC timestamp is an offset from absolute time, so it defines an absolute time point .

This allows you to organize events in time.

Without time zone information, time is relative and cannot be converted to the concept of absolute time without indicating some indication of in which time zone the naive datetime should be tied.

What are the types of time used in a computer system?

  • naive datetime : usually for displaying at local time (i.e. in a browser), where the OS can provide time zone information for the program.

  • UTC timestamps . The UTC time stamp is an absolute time point, as mentioned above, but it is tied to a given time zone, so the UTC time stamp can be converted to any date and time, however it does not contain time zone information. What does it mean? This means that 1504119325 corresponds to 2017-08-30T18:55:24Z or 2017-08-30T17:55:24-0100 or also 2017-08-30T10:55:24-0800 . He does not tell you where the date-time is. It is usually used on the server side to record events (logs, etc.) or used to convert time and time to time in time and calculate time differences .

  • String ISO-8601 datetime : ISO-8601 is a standardized format for recording date and time with a time zone. (These are actually several formats, read here: https://en.wikipedia.org/wiki/ISO_8601 ) It is used to transmit time and time information based on time in serialized mode between systems.

When to use which? or rather, when you need to pay attention to time zones?

  • If you need to take care of the time of day in any way, you need time zone information. A calendar or an alarm clock needs the time of day to set an appointment at the right time of day for any user in the world. If this data is stored on the server, the server must know which time zone corresponds to the date-time.

  • To calculate the time differences between events occurring from different places in the world, the UTC timestamp is sufficient, but you lose the ability to analyze what time of the day the events occurred (i.e. for web analytics, you may want to know when users come to Your site in your local time : Do you see more users in the morning or in the evening? You cannot understand this without information about the time of day.

Time zone offset in date line :

Another important point is that the clockwise offset in the date string is not fixed . This means that since 2017-08-30T10:55:24-0800 says that the offset is -0800 or 8 hours ago, does not mean that it will always be!

In summer it can be summer time and it will be -0700

This means that the time zone offset (+0100) does not match the time zone name (Europe / France) or even the time zone designation (CET)

America/Los_Angeles Time Zone is a place in the world , but in winter it includes PST (Pacific Standard Time) clockwise rotation and PDT (Pacific Daylight Time) in summer.

So, instead of getting the time zone offset from the date, you should also get the exact name of the time zone.

Most packages will be able to convert numerical offsets from daylight saving time to standard time on their own, but this is not necessarily trivial with just an offset. For example, the WAT time zone designation in West Africa is UTC + 0100, just like the time zone in France, but France marks daylight saving time, while in West Africa it’s not (because they are close to the equator)

So, in a word, it's complicated. VERY complicated, and why you should not do it yourself, but trust the package that does it for you, and KEEP IT BEFORE THE DATE!

+8
Aug 30 '17 at 19:35
source share

There is really a problem with using utcfromtimestamp and specifying time zones. A good example / explanation is available on the following question:

How to specify the time zone (UTC) when converting to Unix time? (Python)

+1
Feb 21 '11 at 2:44 p.m.
source share

The accepted answer seems to me ineffective. My decision:

 import time utc_0 = int(time.mktime(datetime(1970, 01, 01).timetuple())) def datetime2ts(dt): """Converts a datetime object to UTC timestamp""" return int(time.mktime(dt.utctimetuple())) - utc_0 
0
Feb 22 '14 at 6:57
source share



All Articles