Im using the pytz module to translate a date in America/Los_Angeles timezone to utc by the code below :
TZ = 'America/Los_Angeles'
from = pytz.timezone(TZ)
utc = from.localize(original_date).astimezone(pytz.utc)
Now,i want to test if utc value is actually in UTC format or not. How to do that with pytz or datetime ?
Please Help
Thank You
utc.tzinfo == pytz.utc # returns True if utc in UTC
Example:
now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc # returns True
now = now.astimezone(pytz.timezone('America/Los_Angeles'))
now.tzinfo == pytz.utc # returns False
The accepted answer will not work for anything else as pytz objects. As pytz is actually pretty bad at doing conversions[1] (e.g. properly doing daylight savings etc) it is probably better to do a cross-implementation check.
now = datetime.datetime.now(pytz.utc)
if now.tzinfo:
now.utcoffset().total_seconds() == 0 # returns true
[1] https://pendulum.eustace.io/blog/a-faster-alternative-to-pyz.html
You can do it simply like this:
from datetime import datetime, timezone
lunch_time = datetime.now(timezone.utc)
if lunch_time.format('%Z') == 'UTC':
print("Eat food")
This will also work with a naive time object because lunch_time.format('%Z') will return an empty string. This method will also work with pytz or any other module because you are simply checking the timezone as string not as an object (the accepted answer won't work with the above timezone module case, only pytz).
from datetime import datetime
import pytz
dinner_time = datetime.now(pytz.timezone('UTC'))
if dinner_time.format('%Z') == 'UTC':
print("Hungry!")
Note: This will also eliminate the possibility of the timezone being GMT timezone rather than UTC timezone. The other answer now.utcoffset().total_seconds() == 0 will be True for GMT which may not be what you want.
The %Z specifier is documented here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Related
I am trying to compare two date fields for the time. Here is what I am doing. My output when I print. I can see it is the same, but comparison result is False! Any pointers to what I am doing wrong?
for appt in appointment_detail:
print(appt['start_time'])
print(slot_time)
print(slot_time == appt['start_time'])
Result:
2018-09-22 11:00:00+00:00
2018-09-22 11:00:00
False
Thanks
Since one DateTime is timezone aware and one is not, they are not equal
To compare or to check equivalence, you have to convert both to standard unit/format
Here I'm removing timezone info of both datetime.
wo_tz_slot_time = slot_time.replace(tzinfo=None)
wo_tz_appt_start_time = appt['start_time'].replace(tzinfo=None)
print(wo_tz_slot_time == wo_tz_appt_start_time)
You compare a datetime with time zone with a datetime that unaware about time zone, you can add time zone to unaware datetime like bellow:
import pytz
from datetime import datetime
time_zone_str = '2018-09-22 11:00:00+00:00'
date_time_str = '2018-09-22 11:00:00'
time_zone = datetime.strptime(''.join(time_zone_str.rsplit(':', 1)), '%Y-%m-%d %H:%M:%S%z')
date_time = datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
date_time_with_time_zone = pytz.utc.localize(date_time)
print(date_time_with_time_zone == time_zone)
more info on python timezone.
import datetime
import pytz # install from pip
US_PACIFIC_TIMEZONE = pytz.timezone("US/Pacific")
dt = datetime.datetime.utcnow().replace(tzinfo=US_PACIFIC_TIMEZONE)
print(dt == dt.replace(tzinfo=US_PACIFIC_TIMEZONE)) # True
dt = datetime.datetime.now(tz=US_PACIFIC_TIMEZONE)
print(dt == dt.replace(tzinfo=US_PACIFIC_TIMEZONE)) # False
So it looks like datetime.datetime.now(tz=..) isn't set to the timezone I specify...
It looks like the timezone is set when using datetime.now, but it's off by an hour-zone.
Why is this?
The only correct formula in your question is:
dt = datetime.now(US_PACIFIC_TIMEZONE)
US_PACIFIC_TIMEZONE may have different utc offsets at different dates e.g., due to DST transitions. You shouldn't use .replace() method (or tzinfo constructor parameter) with such pytz timezones. Here's an explanation on why you should not use replace() with pytz timezones that have a variable utc offset.
I understand how to convert a string to a datetime object, but what about a string that has a different time zone? for example "10/07/2011 04:22 CEST"
EST can mean two different timezones: European Summer Time, or Eastern Standard Time. So datetime strings such as 08/07/2011 04:22 EST are ambiguous -- there's no sure-fire way to correctly convert such strings to a timezone-aware datetime.
If you are willing to just make a stab at a possibly correct answer, then
you can generate a mapping between abbreviations like EST and timezone names, make a random choice among the valid timezones, and
then use that timezone to build a timezone-aware datetime:
import dateutil.tz as dtz
import pytz
import datetime as dt
import collections
import random
timezones = collections.defaultdict(list)
for name in pytz.common_timezones:
timezone = dtz.gettz(name)
try:
now = dt.datetime.now(timezone)
except ValueError:
# dt.datetime.now(dtz.gettz('Pacific/Apia')) raises ValueError
continue
abbrev = now.strftime('%Z')
timezones[abbrev].append(name)
date_string, tz_string = '10/07/2011 04:22 CEST'.rsplit(' ', 1)
date = dt.datetime.strptime(date_string, '%m/%d/%Y %H:%M')
print(date)
# 2011-10-07 04:22:00
tz = pytz.timezone(random.choice(timezones[tz_string]))
print(tz)
# Europe/Oslo
date = tz.localize(date)
print(date)
# 2011-10-07 04:22:00+02:00
You should be able to use strptime with a %Z in your format string, but be aware of this note from the Python documentation (http://docs.python.org/library/datetime.html#strftime-strptime-behavior):
"%Z -- If tzname() returns None, %Z is replaced by an empty string. Otherwise %Z is replaced by the returned value, which must be a string. The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common."
Can you put the timezone into offset form and use %z instead?
I want to get the default timezone (PST) of my system from Python. What's the best way to do that? I'd like to avoid forking another process.
This should work:
import time
time.tzname
time.tzname returns a tuple of two strings: The first is the name of the local non-DST timezone, the second is the name of the local DST timezone.
Example return: ('MST', 'MDT')
Gives a UTC offset like in ThomasH's answer, but takes daylight savings into account.
>>> import time
>>> offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
>>> offset / 60 / 60 * -1
-9
The value of time.timezone or time.altzone is in seconds West of UTC (with areas East of UTC getting a negative value). This is the opposite to how we'd actually like it, hence the * -1.
time.localtime().tm_isdst will be zero if daylight savings is currently not in effect (although this may not be correct if an area has recently changed their daylight savings law).
EDIT: marr75 is correct, I've edited the answer accordingly.
I found this to work well:
import datetime
tz_string = datetime.datetime.now(datetime.timezone.utc).astimezone().tzname()
For me this was able to differentiate between daylight savings and not.
From Python 3.6 you can do:
tz_string = datetime.datetime.now().astimezone().tzname()
Or
tz_string = datetime.datetime.now().astimezone().tzinfo
Reference with more detail: https://stackoverflow.com/a/39079819/4549682
Check out the Python Time Module.
from time import gmtime, strftime
print(strftime("%z", gmtime()))
Pacific Standard Time
The code snippets for calculating offset are incorrect, see http://bugs.python.org/issue7229.
The correct way to handle this is:
def local_time_offset(t=None):
"""Return offset of local zone from GMT, either at present or at time t."""
# python2.3 localtime() can't take None
if t is None:
t = time.time()
if time.localtime(t).tm_isdst and time.daylight:
return -time.altzone
else:
return -time.timezone
This is in all likelihood, not the exact question that the OP asked, but there are two incorrect snippets on the page and time bugs suck to track down and fix.
For Python 3.6+ this can be easily achieved by following code:
import datetime
local_timezone = datetime.datetime.utcnow().astimezone().tzinfo
print(local_timezone)
But with Python < 3.6 calling astimezone() on naive datetime doesn't work. So we've to do it in a slightly different way.
So for Python 3.x,
import datetime
local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
print(local_timezone)
Sample Output:
On Netherlands Server(Python 3.6.9):
CEST
On Bangladesh Server(Python 3.8.2):
+06
More details can be found on this thread.
To obtain timezone information in the form of a datetime.tzinfo object, use dateutil.tz.tzlocal():
from dateutil import tz
myTimeZone = tz.tzlocal()
This object can be used in the tz parameter of datetime.datetime.now():
from datetime import datetime
from dateutil import tz
localisedDatetime = datetime.now(tz = tz.tzlocal())
or the tz parameter of datetime object via datetime.datetime.astimezone():
from datetime import datetime
from dateutil import tz
unlocalisedDatetime = datetime.now()
localisedDatetime = unlocalisedDatetime.astimezone(tz = tz.tzlocal())
Getting offset from UTC as timedelta:
from datetime import datetime, timezone
now = datetime.now()
now.replace(tzinfo=timezone.utc) - now.astimezone(timezone.utc)
Or like this (more obscure but also works):
datetime.now(timezone.utc).astimezone().tzinfo.utcoffset(None)
Both solutions give the same result. For example: datetime.timedelta(seconds=7200)
import tzlocal
tz_info = tzlocal.get_localzone() # 'US/Central' or 'Asia/Calcutta'
dt = datetime.now() # 2023-01-15 15:17:24.412430
print(tz_info.localize(dt) # 2023-01-15 15:17:24.412430-06:00
with tzlocal we will be able to get the local timezone.
How do I convert a datetime or date object into a POSIX timestamp in python? There are methods to create a datetime object out of a timestamp, but I don't seem to find any obvious ways to do the operation the opposite way.
import time, datetime
d = datetime.datetime.now()
print time.mktime(d.timetuple())
For UTC calculations, calendar.timegm is the inverse of time.gmtime.
import calendar, datetime
d = datetime.datetime.utcnow()
print calendar.timegm(d.timetuple())
Note that Python now (3.5.2) includes a built-in method for this in datetime objects:
>>> import datetime
>>> now = datetime.datetime(2020, 11, 18, 18, 52, 47, 874766)
>>> now.timestamp() # Local time
1605743567.874766
>>> now.replace(tzinfo=datetime.timezone.utc).timestamp() # UTC
1605725567.874766 # 5 hours delta (I'm in UTC-5)
In python, time.time() can return seconds as a floating point number that includes a decimal component with the microseconds. In order to convert a datetime back to this representation, you have to add the microseconds component because the direct timetuple doesn't include it.
import time, datetime
posix_now = time.time()
d = datetime.datetime.fromtimestamp(posix_now)
no_microseconds_time = time.mktime(d.timetuple())
has_microseconds_time = time.mktime(d.timetuple()) + d.microsecond * 0.000001
print posix_now
print no_microseconds_time
print has_microseconds_time
Best conversion from posix/epoch to datetime timestamp and the reverse:
this_time = datetime.datetime.utcnow() # datetime.datetime type
epoch_time = this_time.timestamp() # posix time or epoch time
this_time = datetime.datetime.fromtimestamp(epoch_time)
It depends
Is your datetime object timezone aware or naive?
Timezone Aware
If it is aware it's simple
from datetime import datetime, timezone
aware_date = datetime.now(tz=timezone.utc)
posix_timestamp = aware_date.timestamp()
as date.timestamp() gives you "POSIX timestamp"
NOTE: more accurate to call it an epoch/unix timestamp as it may not be POSIX compliant
Timezone Naive
If it's not timezone aware (naive), then you'd need to know what timezone it was originally in so we can use replace() to convert it into a timezone aware date object. Let's assume that you've stored/retrieved it as UTC Naive. Here we create one, as an example:
from datetime import datetime, timezone
naive_date = datetime.utcnow() # this date is naive, but is UTC based
aware_date = naive_date.replace(tzinfo=timezone.utc) # this date is no longer naive
# now we do as we did with the last one
posix_timestamp = aware_date.timestamp()
It's always better to get to a timezone aware date as soon as you can to prevent issues that can arise with naive dates (as Python will often assume they are local times and can mess you up)
NOTE: also be careful with your understanding of the epoch as it is platform dependent