Change time from UTC to the local timezone
I have to write a script where the user will enter a tuple of hours and minutes t(h,m) in IST(Indian).
The entered time should be changed to the local time zone of the PC. Is there a library to do so in python. The time should also be returned in a tuple of hour and minutes.
The time module provides you with some constants including time.timezone, which gives the offset of the local (non-DST) timezone in seconds west of UTC. Subtracting India's timezone from user's timezone will give you the time difference in hours, which you can use to calculate your result.
Related
How can I change this naive time to UTC time in python
The time is 2021-08-05T08:32:47.362000Z
st= "2021-08-05T08:32:47.362000Z"
dt = datetime.strptime(st,"%Y-%m-%d %H:%M:%S.%fZ")
I don't know how to parse this time in python. The above given code is what I know I don't know to parse this time.
So this time has been fetched from Django database and what I inserted is a UTC time and I got the above time as a result.
The thing is I need to get utc time.
I am in Eastern Standard Time (EST). Daylight savings time started 2 weeks ago, where clocks were turned forward 1 hour (so 5PM became 6PM). I have USE_TZ = True. TIME_ZONE is set to “EST”.
In my app, I have a form that submits a date, like 2AM. That date, still 2AM, gets saved in a model as a DateTime field: event.start = date. I have a view that renders the date, and the page shows 2AM correctly.
The problem: event.start evaluates to 3AM (EST) / 8AM (UTC), 1 hour later that what it's supposed to be! The input was 2AM, it even renders 2AM in templates, but for some reason internally event.start is 8AM (UTC) / 3AM (EST).
But for some reason django.utils.timezone.now() gives me the correct time 2AM, instead of 3AM. My OS system time also gives the correct time, 2AM. I want to schedule a job for 2AM, but it ends up getting scheduled for 3AM instead because event.start is set to 3AM for some reason!
I want to keep times in UTC. How do I deal with this?
Eastern Standard Time doesn't have daylight saving time, it is hard offset from UTC. When there are daylight savings places switch from Eastern Standard Time to Eastern Daylight Time(EDT).
More information: https://www.timeanddate.com/time/zones/est
Using Olson identifier is usually better solution. They are the identifiers used in IANA timezone database. They are location based, so when the location changes TimeZone (including daylight savings) everything still works.
Example of a place that changes EST <-> EDT:
America/New_York
Try changing TIME_ZONE from EST to Olson identifier for your desired location.
A Python script running on a server in NYC receives a stream of live data from a websocket API where only the time is given, eg: 8:21:56. The provided time is in the timezone Asia/Chongqing which is UTC +08:00. The local server is in the timezone America/New_York which is UTC -05:00.
This means that the dates in both timezones are different for 12-13 hours every day depending on daylight savings.
Question: Knowing that my server is in a different timezone, how can I find the date needed to convert the time into an appropriate datetime? Eg: If the local date on the server is 2015-12-05, convert 8:21:56 to 2015-12-06 7:36:56.000Z in the UTC timezone.
America/New_York like many other timezones may have different utc offsets at different dates.
To get the date in one time zone (Asia/Chongqing) corresponding to a given time ('8:21:56') in that time zone (Asia/Chongqing) assuming you know the date in another time zone (America/New_York), you should create an algorithm similar to pytz's .localize() method where the difficult part is to convert local time to UTC that is not always possible -- the rest is straightforward (if you know UTC date/time and the zone id such as Asia/Chongqing then it is easy (utc_dt.astimezone(tz).date()) to get date in Asia/Chongqing.
You could simplify the algorithm if you need to support only a fixed number of timezones in a given date range.
In Google App Engine, I used nowTime = datetime.datetime.now() to get the system time. However, I found it is different from the computer system time. For example, nowTime is 2012-12-20 14:44:30.910192, but my computer system time is 2012-12-20 22:44. There is an eight-hour difference. Is it because of the time zone? Where does Google App Engine SDK get time from? Thanks.
See http://timezones.appspot.com/ - GAE time zones will always be in UTC, which is why you are seeing the 8-hour difference. Per the site:
The runtime's TZ environment variable is set to UTC, and can't be
changed. Timestamps returned by e.g. time.time() and
datetime.datetime.now() will always be in UTC. Similarly, datetime
properties in the datastore will always be stored and returned as UTC.
You can change the time zone of a datetime in memory with the
astimezone() method. If datetime's tzinfo member isn't set, you'll
first need to set it to a UTC tzinfo with the replace() method.
You can also see it documented here, with an example of how to do special handling.
My application needs to create facebook events. Everything works fine, but I can't get the timezones correct. The start/end dates are all wrong. Facebook's Event API docs say this:
Note: The start_time and end_time are the times that were input by the event creator, converted to UTC after assuming that they were in Pacific time (Daylight Savings or Standard, depending on the date of the event), then converted into Unix epoch time.
(source)
I can't figure out what that means.
My web application is a python (django) site. Given a datetime object which has the start/end time in UTC, what is the magical incantation of pytz calls to get the correct time to send to facebook?
It means that if the user enters "12 am" you're supposed to assume its "12 am pacific time", convert that to UTC so its "8 am GMT" and turn that into a unix epoch.
You can do it like this:
import datetime, calendar
date_local = datetime.datetime.now() # some date in some timezone
date_utc = date_local.utctimetuple()
unix_time = calendar.timegm(date_utc) # to unix time
"Unix epoch time" simply means "number of seconds since January 1, 1970 (not counting leap seconds)".
By the way, that Facebook Event API description is so bizarre, I can't believe it is right as described. What they seem to be asking for is:
The time that was input by the event creator;
Interpreted as if a local time in the Pacific time zone, with the daylight saving rules for that zone in effect;
Converted to UTC.
I live in the timezone UTC+0. So if I schedule an event at 2010-11-09 12:00:00 UTC, the time that actually gets submitted to Facebook is (the Unix time corresponding to) 2010-11-09 20:00:00 UTC. How can that be right? Maybe I've misunderstood.