Why does tzinfo break creating an epoch time in python? - python

Why does the following happen?
from datetime import datetime
import pytz
d = pytz.utc.localize(datetime.utcnow())
print float(d.strftime('%s')) - float(d.replace(tzinfo=None).strftime('%s')) # 3600.0
Why is it off by one hour whether or not tzinfo is included? I'm assuming it has to do with DST, but... UTC does not have DST.
d.timetuple()
# time.struct_time(tm_year=2013, tm_mon=10, tm_mday=21, tm_hour=17, tm_min=44, tm_sec=40, tm_wday=0, tm_yday=294, tm_isdst=0)
d.replace(tzinfo=None).timetuple()
# time.struct_time(tm_year=2013, tm_mon=10, tm_mday=21, tm_hour=17, tm_min=44, tm_sec=40, tm_wday=0, tm_yday=294, tm_isdst=-1)
So, the difference is tm_isdst is 0 or -1. Both seem very "No DST-ish".
Just not thrilled with the workaround.
Update:
After reading some docs (http://docs.python.org/2/library/time.html#time.mktime) It appears mktime() outputs "localtime" not UTC as I had thought. Which confuses everything.

.strftime('%s') is not supported by Python. Do not use it.
On systems where it works, it interprets the datetime object as time in local timezone i.e., datetime.now().strftime('%s') might return value near time.time().
To find out utc offset or whether DST is in effect for a given local time, you could call d.utcoffset(), d.dst() where d is a datetime object with pytz timezone.
>>> import pytz
>>> d = datetime.now(pytz.utc)
>>> d.utcoffset()
datetime.timedelta(0)
>>> d.dst()
datetime.timedelta(0)
As expected UTC offset is zero for UTC timezone and there is no DST transitions so .dst() is always zero all year round.

Related

Python convert timestamp to unix

I know these questions have been asked before but I'm struggling to convert a timestamp string to a unix time and figuring out whether the datetime objects are naive or aware
For example, to convert the time "2021-05-19 12:51:47" to unix:
>>> from datetime import datetime as dt
>>> dt_obj = dt.strptime("2021-05-19 12:51:47", "%Y-%m-%d %H:%M:%S")
>>> dt_obj
datetime.datetime(2021, 5, 19, 12, 51, 47)
is dt_obj naive or aware and how would you determine this? The methods on dt_obj such as timetz, tzinfo, and tzname don't seem to indicate anything - does that mean that dt_obj is naive?
Then to get unix:
>>> dt_obj.timestamp()
1621421507.0
However when I check 1621421507.0 on say https://www.unixtimestamp.com then it tells me that gmt for the above is Wed May 19 2021 10:51:47 GMT+0000, ie 2 hours behind the original timestamp?
since Python's datetime treats naive datetime as local time by default, you need to set the time zone (tzinfo attribute):
from datetime import datetime, timezone
# assuming "2021-05-19 12:51:47" represents UTC:
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47").replace(tzinfo=timezone.utc)
Or, as #Wolf suggested, instead of setting the tzinfo attribute explicitly, you can also modify the input string by adding "+00:00" which is parsed to UTC;
dt_obj = datetime.fromisoformat("2021-05-19 12:51:47" + "+00:00")
In any case, the result
dt_obj.timestamp()
# 1621428707.0
now converts as expected on https://www.unixtimestamp.com/:
As long as you don't specify the timezone when calling strptime, you will produce naive datetime objects. You may pass time zone information via %z format specifier and +00:00 added to the textual date-time representation to get a timezone aware datetime object:
from datetime import datetime
dt_str = "2021-05-19 12:51:47"
print(dt_str)
dt_obj = datetime.strptime(dt_str+"+00:00", "%Y-%m-%d %H:%M:%S%z")
print(dt_obj)
print(dt_obj.timestamp())
The of above script is this:
2021-05-19 12:51:47
2021-05-19 12:51:47+00:00
1621428707.0
datetime.timestamp()
Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion.
So using this does automatically apply yours machine current timezone, following recipe is given to calculate timestamp from naive datetime without influence of timezone:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

Python get local timezone offset in hours [duplicate]

In Python, how do you find what UTC time offset the computer is set to?
time.timezone:
import time
print -time.timezone
It prints UTC offset in seconds (to take into account Daylight Saving Time (DST) see time.altzone:
is_dst = time.daylight and time.localtime().tm_isdst > 0
utc_offset = - (time.altzone if is_dst else time.timezone)
where utc offset is defined via: "To get local time, add utc offset to utc time."
In Python 3.3+ there is tm_gmtoff attribute if underlying C library supports it:
utc_offset = time.localtime().tm_gmtoff
Note: time.daylight may give a wrong result in some edge cases.
tm_gmtoff is used automatically by datetime if it is available on Python 3.3+:
from datetime import datetime, timedelta, timezone
d = datetime.now(timezone.utc).astimezone()
utc_offset = d.utcoffset() // timedelta(seconds=1)
To get the current UTC offset in a way that workarounds the time.daylight issue and that works even if tm_gmtoff is not available, #jts's suggestion to substruct the local and UTC time can be used:
import time
from datetime import datetime
ts = time.time()
utc_offset = (datetime.fromtimestamp(ts) -
datetime.utcfromtimestamp(ts)).total_seconds()
To get UTC offset for past/future dates, pytz timezones could be used:
from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone() # local timezone
d = datetime.now(tz) # or some other local date
utc_offset = d.utcoffset().total_seconds()
It works during DST transitions, it works for past/future dates even if the local timezone had different UTC offset at the time e.g., Europe/Moscow timezone in 2010-2015 period.
gmtime() will return the UTC time and localtime() will return the local time so subtracting the two should give you the utc offset.
From https://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html
The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).
So, despite the name gmttime, the function returns UTC.
I like:
>>> strftime('%z')
'-0700'
I tried JTS' answer first, but it gave me the wrong result. I'm in -0700 now, but it was saying I was in -0800. But I had to do some conversion before I could get something I could subtract, so maybe the answer was more incomplete than incorrect.
the time module has a timezone offset, given as an integer in "seconds west of UTC"
import time
time.timezone
You can use the datetime and dateutil libraries to get the offset as a timedelta object:
>>> from datetime import datetime
>>> from dateutil.tz import tzlocal
>>>
>>> # From a datetime object
>>> current_time = datetime.now(tzlocal())
>>> current_time.utcoffset()
datetime.timedelta(seconds=7200)
>>> current_time.dst()
datetime.timedelta(seconds=3600)
>>>
>>> # From a tzlocal object
>>> time_zone = tzlocal()
>>> time_zone.utcoffset(datetime.now())
datetime.timedelta(seconds=7200)
>>> time_zone.dst(datetime.now())
datetime.timedelta(seconds=3600)
>>>
>>> print('Your UTC offset is {:+g}'.format(current_time.utcoffset().total_seconds()/3600))
Your UTC offset is +2
hours_delta = (time.mktime(time.localtime()) - time.mktime(time.gmtime())) / 60 / 60
Create a Unix Timestamp with UTC Corrected Timezone
This simple function will make it easy for you to get the current time from a MySQL/PostgreSQL database date object.
def timestamp(date='2018-05-01'):
return int(time.mktime(
datetime.datetime.strptime( date, "%Y-%m-%d" ).timetuple()
)) + int(time.strftime('%z')) * 6 * 6
Example Output
>>> timestamp('2018-05-01')
1525132800
>>> timestamp('2018-06-01')
1527811200
Here is some python3 code with just datetime and time as imports. HTH
>>> from datetime import datetime
>>> import time
>>> def date2iso(thedate):
... strdate = thedate.strftime("%Y-%m-%dT%H:%M:%S")
... minute = (time.localtime().tm_gmtoff / 60) % 60
... hour = ((time.localtime().tm_gmtoff / 60) - minute) / 60
... utcoffset = "%.2d%.2d" %(hour, minute)
... if utcoffset[0] != '-':
... utcoffset = '+' + utcoffset
... return strdate + utcoffset
...
>>> date2iso(datetime.fromtimestamp(time.time()))
'2015-04-06T23:56:30-0400'
This works for me:
if time.daylight > 0:
return time.altzone
else:
return time.timezone

Python: Converting string to timestamp with microseconds

I would like to convert string date format to timestamp with microseconds
I try the following but not giving expected result:
"""input string date -> 2014-08-01 04:41:52,117
expected result -> 1410748201.117"""
import time
import datetime
myDate = "2014-08-01 04:41:52,117"
timestamp = time.mktime(datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timetuple())
print timestamp
> 1410748201.0
Where did the milliseconds go?
There is no slot for the microseconds component in a time tuple:
>>> import time
>>> import datetime
>>> myDate = "2014-08-01 04:41:52,117"
>>> datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timetuple()
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=1, tm_hour=4, tm_min=41, tm_sec=52, tm_wday=4, tm_yday=213, tm_isdst=-1)
You'll have to add those manually:
>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> time.mktime(dt.timetuple()) + (dt.microsecond / 1000000.0)
1406864512.117
The other method you could follow is to produce a timedelta() object relative to the epoch, then get the timestamp with the timedelta.total_seconds() method:
epoch = datetime.datetime.fromtimestamp(0)
(dt - epoch).total_seconds()
The use of a local time epoch is quite deliberate since you have a naive (not timezone-aware) datetime value. This method can be inaccurate based on the history of your local timezone however, see J.F. Sebastian's comment. You'd have to convert the naive datetime value to a timezone-aware datetime value first using your local timezone before subtracting a timezone-aware epoch.
As such, it is easier to stick to the timetuple() + microseconds approach.
Demo:
>>> dt = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
>>> epoch = datetime.datetime.fromtimestamp(0)
>>> (dt - epoch).total_seconds()
1406864512.117
In Python 3.4 and later you can use
timestamp = datetime.datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f").timestamp()
This doesn't require importing the time module. It also uses less steps so it should be faster. For older versions of python the other provided answers are probably your best option.
However, the resulting timestamp will interpret myDate in local time, rather than UTC, which may cause issues if myDate was given in UTC
Where did the milliseconds go?
It is the easy part. .timetuple() call drops them. You could add them back using .microsecond attribute. The datetime.timestamp() method from the standard library works that way for naive datetime objects:
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
return _time.mktime((self.year, self.month, self.day,
self.hour, self.minute, self.second,
-1, -1, -1)) + self.microsecond / 1e6
else:
return (self - _EPOCH).total_seconds()
It is enough if possible ~1 hour errors could be ignored in your case. I assume that you want microseconds and therefore you can't ignore ~1 hour time errors silently.
To convert the local time given as a string to the POSIX timestamp correctly is a complex task in general. You could convert the local time to UTC and then get the timestamp from UTC time.
There are two main issues:
local time may be non-existent or ambiguous e.g. during DST transitions the same time may occur twice
UTC offset for the local timezone may be different in the past and therefore a naive: local time minus epoch in local time formula may fail
Both can be solved using the tz database (pytz module in Python):
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone() # get pytz timezone corresponding to the local timezone
naive_d = datetime.strptime(myDate, "%Y-%m-%d %H:%M:%S,%f")
# a) raise exception for non-existent or ambiguous times
d = tz.localize(naive_d, is_dst=None)
## b) assume standard time, adjust non-existent times
#d = tz.normalize(tz.localize(naive_d, is_dst=False))
## c) assume DST is in effect, adjust non-existent times
#d = tz.normalize(tz.localize(naive_d, is_dst=True))
timestamp = d - datetime(1970, 1, 1, tzinfo=pytz.utc)
The result is timestamp -- a timedelta object, you can convert it to seconds, milliseconds, etc.
Also different systems may behave differently around/during leap seconds. Most application can ignore that they exist.
In general, it might be simpler to store POSIX timestamps in addition to the local time instead of trying to guess it from the local time.

How to add two offset in python?

I am getting a offset form the pytz library from the following line:
offset = datetime.datetime.now(pytz.timezone(timezone)).strftime('%z')
First i pass the US/Eastern in timezone variable
and then i pass the Asia/Kolkata in timezone variable which prints the following value
local_utc = -0400
user_utc = +0530
After getting these values i converted it from string to int by following code:
local_utc = int(local_utc)
user_urc = int(user_utc)
Apart from this i have a timetuple also:
hour, minute,days = (timezone_tuple.tm_hour, timezone_tuple.tm_min,
timezone_tuple.tm_mday)
I want to add the difference of local_utc and user_utc to above tuple such as -0400: 04 such as hour and 00 as minutes.
For example: difference will be : 0930. And 09 will be add to timezone_tuple.tm_hour and 30 will be add to timezone_tuple.tm_min
I didn't found any situation. how can it be possible?
Is there any way to do with spilit method
Your post showed how to find local_utc and user_utc as integers. You could just take the difference local_utc-user_utc to determine the relative offset.
However, datetime, time and pytz should give you all the tools you need to manipulate times without having to parse offsets and do such calculations "manually".
For example,
import pytz
import datetime as dt
import time
eastern = pytz.timezone('US/Eastern')
kolkata = pytz.timezone('Asia/Kolkata')
naive_timetuple = time.localtime(0)
print(naive_timetuple)
# time.struct_time(tm_year=1969, tm_mon=12, tm_mday=31, tm_hour=19, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=365, tm_isdst=0)
Above, I defined a naive timetuple. Below, I "localize" it to US/Eastern time -- that is, make it a timezone-aware datetime:
naive_datetime = dt.datetime(*naive_timetuple[:6])
print(naive_datetime)
# 1969-12-31 19:00:00
localized_datetime = eastern.localize(naive_datetime)
print(localized_datetime)
# 1969-12-31 19:00:00-05:00
Now to convert a timezone-aware datetime to any other timezone, use the astimezone method:
kolkata_datetime = localized_datetime.astimezone(kolkata)
print(kolkata_datetime)
# 1970-01-01 05:30:00+05:30
And if you need to convert a datetime back to a timetuple, use the timetuple method:
kolkata_timetuple = kolkata_datetime.timetuple()
print(kolkata_timetuple)
# time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Getting computer's UTC offset in Python

In Python, how do you find what UTC time offset the computer is set to?
time.timezone:
import time
print -time.timezone
It prints UTC offset in seconds (to take into account Daylight Saving Time (DST) see time.altzone:
is_dst = time.daylight and time.localtime().tm_isdst > 0
utc_offset = - (time.altzone if is_dst else time.timezone)
where utc offset is defined via: "To get local time, add utc offset to utc time."
In Python 3.3+ there is tm_gmtoff attribute if underlying C library supports it:
utc_offset = time.localtime().tm_gmtoff
Note: time.daylight may give a wrong result in some edge cases.
tm_gmtoff is used automatically by datetime if it is available on Python 3.3+:
from datetime import datetime, timedelta, timezone
d = datetime.now(timezone.utc).astimezone()
utc_offset = d.utcoffset() // timedelta(seconds=1)
To get the current UTC offset in a way that workarounds the time.daylight issue and that works even if tm_gmtoff is not available, #jts's suggestion to substruct the local and UTC time can be used:
import time
from datetime import datetime
ts = time.time()
utc_offset = (datetime.fromtimestamp(ts) -
datetime.utcfromtimestamp(ts)).total_seconds()
To get UTC offset for past/future dates, pytz timezones could be used:
from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone() # local timezone
d = datetime.now(tz) # or some other local date
utc_offset = d.utcoffset().total_seconds()
It works during DST transitions, it works for past/future dates even if the local timezone had different UTC offset at the time e.g., Europe/Moscow timezone in 2010-2015 period.
gmtime() will return the UTC time and localtime() will return the local time so subtracting the two should give you the utc offset.
From https://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html
The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).
So, despite the name gmttime, the function returns UTC.
I like:
>>> strftime('%z')
'-0700'
I tried JTS' answer first, but it gave me the wrong result. I'm in -0700 now, but it was saying I was in -0800. But I had to do some conversion before I could get something I could subtract, so maybe the answer was more incomplete than incorrect.
the time module has a timezone offset, given as an integer in "seconds west of UTC"
import time
time.timezone
You can use the datetime and dateutil libraries to get the offset as a timedelta object:
>>> from datetime import datetime
>>> from dateutil.tz import tzlocal
>>>
>>> # From a datetime object
>>> current_time = datetime.now(tzlocal())
>>> current_time.utcoffset()
datetime.timedelta(seconds=7200)
>>> current_time.dst()
datetime.timedelta(seconds=3600)
>>>
>>> # From a tzlocal object
>>> time_zone = tzlocal()
>>> time_zone.utcoffset(datetime.now())
datetime.timedelta(seconds=7200)
>>> time_zone.dst(datetime.now())
datetime.timedelta(seconds=3600)
>>>
>>> print('Your UTC offset is {:+g}'.format(current_time.utcoffset().total_seconds()/3600))
Your UTC offset is +2
hours_delta = (time.mktime(time.localtime()) - time.mktime(time.gmtime())) / 60 / 60
Create a Unix Timestamp with UTC Corrected Timezone
This simple function will make it easy for you to get the current time from a MySQL/PostgreSQL database date object.
def timestamp(date='2018-05-01'):
return int(time.mktime(
datetime.datetime.strptime( date, "%Y-%m-%d" ).timetuple()
)) + int(time.strftime('%z')) * 6 * 6
Example Output
>>> timestamp('2018-05-01')
1525132800
>>> timestamp('2018-06-01')
1527811200
Here is some python3 code with just datetime and time as imports. HTH
>>> from datetime import datetime
>>> import time
>>> def date2iso(thedate):
... strdate = thedate.strftime("%Y-%m-%dT%H:%M:%S")
... minute = (time.localtime().tm_gmtoff / 60) % 60
... hour = ((time.localtime().tm_gmtoff / 60) - minute) / 60
... utcoffset = "%.2d%.2d" %(hour, minute)
... if utcoffset[0] != '-':
... utcoffset = '+' + utcoffset
... return strdate + utcoffset
...
>>> date2iso(datetime.fromtimestamp(time.time()))
'2015-04-06T23:56:30-0400'
This works for me:
if time.daylight > 0:
return time.altzone
else:
return time.timezone

Categories