How to convert a naive time to UTC time in python? - 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.

Related

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.

python schedule at server time synced with my country

I'm writing a Python script, and I need to use the Python schedule module.
I want to execute a job every day at midnight, so I wrote something like
schedule.every().day.at("00:00")
Problem is that I want to run at my midnight, because I'm uploading this script to a server and I don't know its location and hence its timezone.
How could I achieve my goal?
from time import gmtime, strftime
print strftime("%z", gmtime())
Pacific Standard Time
import time
time.tzname
it 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.
Schedule doesn't support timezones, a pull-request that included the initial changes to support that was rejected (the source for that can be found here.
So either look at those changes, or run something at 00:00 that emails you a message, so you can deduct how much the offset from that server is to yours.
If you do so check on a regular basis especially late October/March, so you can determine if the server is subject to daylight saving changes for its localtime, and adjust accordingly.

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')

Assign a date to a time in 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.

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