Assign a date to a time in Python - python

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.

Related

How to convert a naive time to UTC time in python?

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.

Datetime is wrong in django

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.

To convert Time from one timezone to another

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.

UTC time to GPS time converter by Python

I have a UTC time (epoch Unix time) formatted as timestamp as below.
1496224620
(Human readable value: May 31, 2017 09:57:00)
I need to convert the timestamp formatted as Unix time into GPS time format as below.
1180259838
(Human readable value: May 31, 2017 09:57:00)
I need a python program (algorithm is fine for me) to convert timestamp formatted as Unix time to timestamp formatted as GPS time.
There is one PHP program to do so. I can change from PHP code to Python code to have a Python program by myself. But I think there is also a short way (built-in function of Python) that can implement my expectation more effective.
Here is the link of PHP program
https://www.andrews.edu/~tzs/timeconv/timealgorithm.html
Just subtract 315964782 from the UNIX time.
GPS Time starts on January 5, 1980. UNIX time starts on January 1, 1970. They simply have different starting points, or Epochs. The difference between those two Epochs is the number of seconds between those two dates PLUS 18 GPS leap seconds (so far).
As others have noted, GPS leap seconds are periodically added as the Earth's rotation gradually slows.
we must update our source code manually for each time the leap second occurs (ex. next 2018, 2019,...)? Is there any feasible way to prevent this problem?
Many devices have a message that indicates the "current" number of GPS seconds in effect. My C++ NeoGPS library has an example program that requests the current number of GPS seconds from a ublox device (binary message defined here). See ublox NEO-xx specifications for more information regarding the NAV-TIMEGPS message.
Other manufacturers may have their own protocols and messages for obtaining the current GPS leap seconds.
HOWEVER:
Most GPS devices report times in UTC, with the leap seconds already included. Unless you are using a GPS time based on the start of the week (midnight Sunday), you should not need to know the GPS leap seconds.
If you are trying to convert from a "GPS time since start of week", then you would also need to know the current GPS week number to convert "GPS time of week" to UTC.
ublox devices report some fix information with a timestamp that is "GPS milliseconds since start of week." This NeoGPS file shows several methods for converting between "GPS milliseconds since start of week" and UTC.
How about:
import datetime
datetime.datetime.fromtimestamp(int("1284101485")).strftime('%B-%Y-%d %H:%M:%S')

Facebook Events and timezones, how to convert UTC datetime to what facebook expects?

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.

Categories