I'm working on a Python application that fires notifications at certain times. I started working on this project in Italy and the program worked fine, but now that I'm back in the U.S., the time is completely wrong. There's a four hour time difference between the actual time and the time that is shown, which is weird, because Italy is 6 hours ahead.
For example, the time that datetime.utcnow() shows is 2016-05-10T18:55:47.920001Z but the time is actually 2016-05-10T14:55:47.920001Z
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
I tried using pytz to set the time zone (which gives me the correct time), but the API that I'm sending the current time to (Google Calendar), won't accept it. This time isn't accepted but the line of code above is, but the time is wrong.
eastern = pytz.timezone("America/New_York")
now = datetime.datetime.now(tz=eastern).isoformat() + 'Z' # 'Z' indicates UTC time
How can I get datetime.datetime.utcnow() to reflect my current time?
.utcnow() returns time in UTC. It returns the exact same value wherever you are. UTC time is the same in Italy and in US.
To generate rfc3339 timestamp: .utcnow().isoformat() + 'Z' is correct.
datetime.now(eastern).isoformat() + 'Z' is wrong. Use just datetime.now(eastern).isoformat():
>>> from datetime import datetime
>>> import pytz
>>> eastern = pytz.timezone('America/New_York')
>>> datetime.utcnow().isoformat()+'Z', datetime.now(eastern).isoformat()
('2016-05-11T15:25:18.857833Z', '2016-05-11T11:25:18.857860-04:00')
Both return the same time (if we ignore microseconds). Today, 15:25UTC is the same time as 11:25 in New York:
<local time> == <utc time> + <utc offset>
11:25 == 15:25 + (-04:00)
Try:
from datetime import datetime, timezone
# 'Z' indicates UTC time
now = datetime.now(timezone.utc).astimezone().isoformat() + 'Z'
print(now)
Related
How would I get the current timestamp in python of India?
I tried time.ctime() and datetime.utcnow() also datetime.now()
but they all return a different time than here it is in india.
The codes above return the time that not match the current time on my computer. and the time in my computer is definitely correct.
from pytz import timezone
from datetime import datetime
ind_time = datetime.now(timezone("Asia/Kolkata")).strftime('%Y-%m-%d %H:%M:%S.%f')
print(ind_time)
>>> "2020-08-28 11:56:37.010822"
You can use timedelta object in datetime module:
since the Indian Standard Time (IST) is 5.5 hours ahead of Coordinated Universal Time (UTC), we can shift the UTC time to 5hrs and 30 mins.
import datetime as dt
dt_India_naive = dt.datetime.utcnow() + dt.timedelta(hours=5, minutes=30)
dt_India_aware = dt.datetime.now(dt.timezone(dt.timedelta(hours=5, minutes=30)))
dt_UTC_naive = dt.datetime.utcnow()
dt_UTC_aware = dt.datetime.now(dt.timezone.utc)
max_len = len(max(['UTC Time', 'Indian Time'], key=len))
print(f"{'UTC Time' :<{max_len}} - {dt_UTC_aware:%d-%b-%y %H:%M:%S}")
print(f"{'Indian Time':<{max_len}} - {dt_India_aware:%d-%b-%y %H:%M:%S}")
# Both offset-naive and offset-aware will provide same results in this sitatuiion
Result:-
UTC Time - 20-Feb-23 03:29:12
Indian Time - 20-Feb-23 08:59:12
You can do it with pytz:
import datetime,pytz
dtobj1=datetime.datetime.utcnow() #utcnow class method
print(dtobj1)
dtobj3=dtobj1.replace(tzinfo=pytz.UTC) #replace method
#print(pytz.all_timezones) => To see all timezones
dtobj_india=dtobj3.astimezone(pytz.timezone("Asia/Calcutta")) #astimezone method
print(dtobj_india)
result:
2020-08-28 06:01:13.833290
2020-08-28 11:31:13.833290+05:30
How can I compute the time differential between two time zones in Python? That is, I don't want to compare TZ-aware datetime objects and get a timedelta; I want to compare two TimeZone objects and get an offset_hours. Nothing in the datetime library handles this, and neither does pytz.
Here is a solution using the Python library Pytz which solves the issue of ambiguous times at the end of daylight saving time.
from pytz import timezone
import pandas as pd
def tz_diff(date, tz1, tz2):
'''
Returns the difference in hours between timezone1 and timezone2
for a given date.
'''
date = pd.to_datetime(date)
return (tz1.localize(date) -
tz2.localize(date).astimezone(tz1))\
.seconds/3600
The examples below calculate the difference in hours between UTC and Australia time for the first of January and first of June respectively. Notice how daylight savings are taken into consideration.
utc = timezone('UTC')
aus = timezone('Australia/Sydney')
tz_diff('2017-01-01', utc, aus)
# 11.0
tz_diff('2017-06-01', utc, aus)
# 10.0
Thanks
The first thing you have to know is that the offset between two time zones depends not only on the time zones in question, but on the date you're asking about. For example, the dates on which Daylight Savings Time began and ended changed in the US in 2007. While fundamental time zone logistics change only infrequently in any single location, the rate of change globally is impossible to ignore. Therefore, you have to incorporate the date in question into your function.
Having completed the necessary preface, the actual function isn't too hard to write if you take advantage of the pendulum library. It should look something like this:
import pendulum
def tz_diff(home, away, on=None):
"""
Return the difference in hours between the away time zone and home.
`home` and `away` may be any values which pendulum parses as timezones.
However, recommended use is to specify the full formal name.
See https://gist.github.com/pamelafox/986163
As not all time zones are separated by an integer number of hours, this
function returns a float.
As time zones are political entities, their definitions can change over time.
This is complicated by the fact that daylight savings time does not start
and end on the same days uniformly across the globe. This means that there are
certain days of the year when the returned value between `Europe/Berlin` and
`America/New_York` is _not_ `6.0`.
By default, this function always assumes that you want the current
definition. If you prefer to specify, set `on` to the date of your choice.
It should be a `Pendulum` object.
This function returns the number of hours which must be added to the home time
in order to get the away time. For example,
```python
>>> tz_diff('Europe/Berlin', 'America/New_York')
-6.0
>>> tz_diff('Europe/Berlin', 'Asia/Kabul')
2.5
```
"""
if on is None:
on = pendulum.today()
diff = (on.set(tz=home) - on.set(tz=away)).total_hours()
# what about the diff from Tokyo to Honolulu? Right now the result is -19.0
# it should be 5.0; Honolulu is naturally east of Tokyo, just not so around
# the date line
if abs(diff) > 12.0:
if diff < 0.0:
diff += 24.0
else:
diff -= 24.0
return diff
As stated in the documentation, you may not get a stable result for this between any two given locations as you sweep across the days of the year. However, implementing a variant which chooses the median result over the days of the current year is an exercise left for the reader.
Here's another solution:
from datetime import datetime
from pytz import timezone
from dateutil.relativedelta import relativedelta
utcnow = timezone('utc').localize(datetime.utcnow()) # generic time
here = utcnow.astimezone(timezone('US/Eastern')).replace(tzinfo=None)
there = utcnow.astimezone(timezone('Asia/Ho_Chi_Minh')).replace(tzinfo=None)
offset = relativedelta(here, there)
offset.hours
Here what we're doing is converting a time to two different time zones. Then, we remove the time zone information so that when you calculate the difference between the two using relativedelta, we trick it into thinking that these are two different moments in time instead of the same moment in different time zones.
The above result will return -11, however this amount can change throughout the year since US/Eastern observes DST and Asia/Ho_Chi_Minh does not.
I created two functions to deal with timezone.
import datetime
import pytz
def diff_hours_tz(from_tz_name, to_tz_name, negative=False):
"""
Returns difference hours between timezones
res = diff_hours_tz("UTC", "Europe/Paris") : 2
"""
from_tz = pytz.timezone(from_tz_name)
to_tz = pytz.timezone(to_tz_name)
utc_dt = datetime.datetime.now(datetime.timezone.utc)
dt_from = dt_to = datetime.datetime.utcnow()
dt_from = from_tz.localize(dt_from)
dt_to = to_tz.localize(dt_to)
from_d = dt_from - utc_dt
if from_d.days < 0:
return diff_hours_tz(to_tz_name, from_tz_name, True)
dt_delta = dt_from - dt_to
negative_int = -1 if negative else 1
return int(dt_delta.seconds/3600)*negative_int
def dt_tz_to_tz(dt, from_tz_name, to_tz_name):
"""
Apply difference hours between timezones to a datetime object
dt_new = dt_tz_to_tz(datetime.datetime.now(), "UTC", "Europe/Paris")
"""
hours = diff_hours_tz(from_tz_name, to_tz_name)
return dt+datetime.timedelta(hours=hours)
# Usage example
res = diff_hours_tz("Europe/Paris", "America/New_York")
# Result : -6
res = diff_hours_tz("UTC", "Europe/Paris")
# Result : 2
now = datetime.datetime.now()
# Result : 2019-06-18 15:10:31.720105
dt_new = dt_tz_to_tz(now, "UTC", "Europe/Paris")
# Result : 2019-06-18 17:10:31.720105
dt_new = dt_tz_to_tz(now, "Europe/Paris", "America/New_York")
# Result : 2019-06-18 09:10:31.720105
dt_new = dt_tz_to_tz(now, "America/New_York", "Europe/Paris")
# Result : 2019-06-18 21:10:31.720105
I hope it will help !
Here is a code snippet to get the difference between UTC and US/Eastern, but it should work for any two timezones.
# The following algorithm will work no matter what is the local timezone of the server,
# but for the purposes of this discussion, let's assume that the local timezone is UTC.
local_timestamp = datetime.now()
# Assume that utc_timestamp == 2019-01-01 12:00.
utc_timestamp = pytz.utc.localize(local_timestamp)
# If it was 12:00 in New York, it would be 20:00 in UTC. So us_eastern_timestamp is a UTC
# timestamp with the value of 2019-01-01 20:00.
us_eastern_timestamp = timezone("US/Eastern").localize(local_timestamp).astimezone(pytz.utc)
# delta is a Python timedelta object representing the interval between the two timestamps,
# which, in our example, is -8 hours.
delta = utc_timestamp - us_eastern_timestamp
# In the last line, we convert the timedelta into an integer representing the number of
# hours.
print round(delta.total_seconds() / 60.0 / 60.0)
(tz_from.localize(date) - tz_to.localize(date)).seconds/3600.0
Where tz_from and tz_to are the starting and ending timezones. You must specify a particular date.
from datetime import datetime
from zoneinfo import ZoneInfo
dt = datetime.now() # 2020-09-13
tz0, tz1 = "Europe/Berlin", "US/Eastern" # +2 vs. -4 hours rel. to UTC
utcoff0, utcoff1 = dt.astimezone(ZoneInfo(tz0)).utcoffset(), dt.astimezone(ZoneInfo(tz1)).utcoffset()
print(f"hours offset between {tz0} -> {tz1} timezones: {(utcoff1-utcoff0).total_seconds()/3600}")
>>> hours offset between Europe/Berlin -> US/Eastern timezones: -6.0
a way to do this with Python 3.9's standard library.
I receive time in the format 12:00 and I also have a timezone in the format <DstTzInfo 'Europe/Paris' PMT+0:09:00 STD>. I want to see, if this date is already passed current time in that timezone.
My current timezone may be different from the timezone I receive, so I use the following:
tz = pytz.timezone('Europe/Paris')
d = parse("14:39").replace(tzinfo=tz)
now = datetime.datetime.now(tz)
print (d - now).total_seconds()
Even though the current time in Paris is 14:39, I still get 6600 seconds. I also tried now = datetime.datetime.replace(tzinfo=tz), but I get -3600 seconds.
What I want is having time in a specified timezone, check whether it passed or not.
What about this:
from datetime import datetime
from dateutil import tz
tz = tz.gettz('Europe/Paris')
now = datetime.now().replace(tzinfo=tz)
d = datetime.strptime("{0}/{1}/{2} 14:39".format(now.year,now.month,now.day), "%Y/%m/%d %H:%M").replace(tzinfo=tz)
print (d-now).total_seconds()
I keep the current time in now, in the Europe/Paris timezone, and also create d in the same timezone. If their difference is negative the date is already passed current time in that timezone.
I am using the following code to get the time:
import time
time = time.asctime()
print(time)
I end up with the following result:
'Tue Feb 25 12:09:09 2014'
How can I get Python to print just the hour?
You can use datetime:
>>> import datetime as dt
>>> dt.datetime.now().hour
9
Or, rather than now() you can use today():
>>> dt.datetime.today().hour
9
Then insert into any string desired:
>>> print('The hour is {} o\'clock'.format(dt.datetime.today().hour))
The hour is 9 o'clock
Note that datetime.today() and datetime.now() are both using your computer's notion of local time zone (ie, a 'naive' datetime object).
If you want to use time zone info, it is not so trivial. You can either be on Python 3.2+ and use datetime.timezone or use the third party pytz. I am assuming your computer's timezone is fine, and a naive (non time zone datetime object) is fairly easy to use.
import time
print (time.strftime("%H"))
time.asctime() will create a string, so extracting the hours part is hard. Instead, get a proper time.struct_time object, which exposes the components directly:
t = time.localtime() # gives you an actual struct_time object
h = t.tm_hour # gives you the hour part as an integer
print(h)
You can do it in one step if that's all you need the hour for:
print(time.localtime().tm_hour)
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.