It is 2022/06/28 actually 28th of June 2022; I noticed when I try to get the current time from Python console two different results are possible the Eastern Time (Toronto, Montreal and New York). So what is the difference between these two parameters? I am going to answer the question:
"EST" is not accurate if you want to get the current time in New York because it represents Eastern Standard Time (UTC-05:00), which is one hour behind Eastern Daylight Time (UTC-04:00). Due to daylight savings, New York will observe either EST or EDT depending on the time of year.
"US/Eastern" is preferable to "EST" as it represents the Eastern Time Zone in the United States and will account for any shifts due to daylight savings. However, the zone representing "US/Eastern" has been renamed to "America/New_York", and is maintained for backwards compatibility.
The first way to get the current time in Toronto is:
from datetime import datetime
from pytz import timezone
tz = timezone('EST')
print(datetime.now(tz))
The output is the following:
2022-06-28 16:23:23.333585-05:00
The second way to get the current time in Toronto is:
from datetime import datetime
from pytz import timezone
tz = timezone('US/Eastern')
print(datetime.now(tz))
The output is the following:
2022-06-28 17:24:42.944669-04:00
Conclusion:
If you use "EST" it is sometimes 1 hour ahead of the true time. I recommend you usually use 'US/Eastern'.
Related
These two lines of code:
print(datetime.now().astimezone().tzname())
print(time.strftime('%Z'))
output: Eastern Daylight Time
I need something that outputs EDT and that doesn't require a timezone input.
Can someone explain to me, how to check whether a given time in "hh:mm" format falls in between a given range.
Say, given time is 10:30 A.M IST and my range is between 10:00 A.M and 11:00 A.M. So given time falls in the range.
Is there any package in python to do this in the easiest way?
Would be happy if anyone can help with this :)
The simple way is just to use datetime.time and compare in an if statement:
import datetime
hhmm = "10:30"
current_time = datetime.datetime.strptime(hhmm, "%H:%M").time()
if datetime.time(10) <= current_time <= datetime.time(11):
print("Time is between 10am and 11am")
else:
print("Time is not between 10am and 11am")
The timezone info is removed from the datetime object when .time() is called on it - if you input a literal time without a timezone, this isn't an issue, while if you do have a timezone then as long as the datetime is transformed (via .astimezone(zoneinfo.ZoneInfo('IST'))) into the timezone you want, you should just be able to compare with the literal 10am and 11am.
See also strptime() behavior, if your input format is more complicated than the above. It's possible to accommodate for AM/PM, as well as timezone.
Has anyone converted this kind of times before?
2020-10-12T01:00:00-07:00 to 2020-10-12T09:00:00-07:00
equals
Monday, October 12, 2020 at 10:00 AM – 6:00 PM UTC+02
to datetime objects?
2020-10-12T01:00:00-07:00
<--date--> <-time-><zone>
This means 1am on October 12th, 2020, in the time zone 7 hours west of UTC (running through the middle of the US, basically).
It's actually one of the ISO8601 formats, used for date/time data interchange.
I believe the dateutil.parser() library can handle this in Python.
I have a datetime object created from which I subtract 13 days as follow:
(date.today()-timedelta(days=13)).strftime('%Y-%m-%d')
The strangeness occurs when I execute the code at 6AM and 8:30AM. At 6AM, the resulting string is returned as (if today is 2012-02-29):
2012-02-15
which is 14 days before the current! However, running the same line at 8:30AM, the resulting string is returned as:
2012-02-16
Which then correct. So far I have not been able to figure out what the difference is between the small period of time. I use timezone naive datetime objects, if that is important. I would like to know what could cause this change in the resulting string date.
Many thanks.
EDIT: (based on eumiro's suggestion below)
datetime.datetime.now() returns:
>>> datetime.datetime(2012, 2, 29, 10, 46, 20, 659862)
And the timezone is Europe/Vienna on the server and in the django application that runs the line of code.
I also tried running a similar line to the one you suggested:
(pytz.timezone(settings.TIME_ZONE).localize(datetime.now(), is_dst=True) - \
timedelta(days=13)).strftime('%Y-%m-%d')
But with the same results... which is why I think I don't think it has much to do with timezones also. But at the same time not sure where else to look.
You live somewhere in America? This is the place where the timezones are around 6-8 hours behind the UTC and that's the time of UTC midnight.
What does datetime.datetime.now() return?
If you want to get the real local time, use this (replace "America/New_York" with your timezone):
from datetime import datetime, timedelta
import pytz
now = datetime.datetime.now(pytz.timezone("America/New_York"))
dt = (now - timedelta(days=13)).strftime('%Y-%m-%d')
and it should return the same correct values from midnight until midnight.
Unfortunately DST is poorly supported in Python.
Even pytz is not perfect, but can be made to work with hacks.
You have to decide what it means to subtract 2 days from 10th, 1p.m., either 2 calendar days or 48 hours or 172800 seconds.
I am in the US and I want to get the UTC time (without the effect of Daylight Saving Time) for a piece of code:
localtime = strftime("%m%d%y%H%M", gmtime())
Right now that code give me time in Greenwich, England. I know that I have a time offset of -8 (GMT -8). How can I get the UTC time to a specific timezone? (using Python library, not casting the hour to integer, - 8, and than convert it back)
Try the datetime module:
datetime.datetime.utcnow() + datetime.timedelta(hours=-8)
BTW, UTC doesn't have timezones, it's Universal Co-ordinated Time, it's the same everywhere.
Just use the localtime() rather than gmtime() function:
localtime = time.strftime("%m%d%y%H%M", time.localtime())
From the python time module:
"Like gmtime() but converts to local time. If secs is not provided or None, the current time as returned by time() is used. The dst flag is set to 1 when DST applies to the given time."
To see if DST applies:
time.daylight
Supporting timezones at whatever level of detail is required is up to the application. The rules for time adjustment across the world are more political than rational, and there is no standard suitable for every application.
There is a useful third party module for that purpose: http://pytz.sourceforge.net/