As a part of my Master's project I collect data with an RTK receiver and for matching some x,y,z,time data with scientific measurements I need to convert some GPS time ( GPS Week & Milliseconds since the beginning of the GPS week) to UTC (hh:mm:ss:ms).
Can someone instruct me?
For example:
data example to convert
Cheers
Can you try the following:
from datetime import datetime, timedelta
import pytz
def gps_datetime(time_week, time_ms, leap_seconds=18):
gps_epoch = datetime(1980, 1, 6, tzinfo=pytz.utc)
return gps_epoch + timedelta(weeks=time_week,
milliseconds=time_ms,
seconds=-leap_seconds)
# from your example
print(gps_datetime(2193, 377638200))
Output:
datetime.datetime(2022, 1, 20, 8, 53, 58, 200000, tzinfo=<UTC>)
Related
I have time in J2000 format, ie. seconds after noon on 1-1-2000, which I want to convert to UTC time in an array with the format [year-month-day hour:min:sec.millisec]. Is there some function in AstroPy or something similar to do the conversion?
Input:
Time in J2000: 559889620.293 seconds
Desired Output:
Time in UTC: 2017-09-28 16:53:40.293
If you want to use astropy you could use Time and TimeDelta from astropy.time:
>>> from astropy.time import TimeDelta, Time
>>> from astropy import units as u
>>> (Time(2000, format='jyear') + TimeDelta(559889620.293*u.s)).iso
'2017-09-28 16:53:35.293'
The Time(2000, format='jyear') is a good alternative if you don't want to remember what the baseline for julian dates (noon on 1.1.2000) is.
You can simply add a timedelta to a base datetime.
import datetime
datetime.datetime(2000, 1, 1, 12, 0) + datetime.timedelta(seconds=559889620.293)
returns:
datetime.datetime(2017, 9, 28, 16, 53, 40, 293000)
The offset is constant, so you can just add it:
>>> OFFSET = datetime(2000,1,1,12) - datetime(1970,1,1)
>>> datetime.utcfromtimestamp(559889620.293) + OFFSET
datetime.datetime(2017, 9, 28, 16, 53, 40, 293000)
I have a case in my Django :
Variable 't' get data from database. I print 't' variable and the result is 'datetime.time(16, 59, 59)'. It means at 16:59:59.
I want to combine today's date with these time. Does anyone know?
The result that I want is (for example) : 'datetime.datetime(2014, 10, 22, 16, 59, 59)' which is combine from today's date is '2014-10-22' and specified time like '16:59:59'.
Thank you.
Use datetime.combine:
from datetime import datetime, date, time
datetime.combine(date(2014, 10, 22), time(16, 59, 59)).
its what that datetime.combine is for and date.today() returns todays date :
>>> from datetime import datetime ,date , time
>>> datetime.combine(date.today(), time(16, 59, 59))
datetime.datetime(2014, 10, 22, 16, 59, 59)
I have numerous UTC time stamps in the following format:
2012-04-30T23:08:56+00:00
I want to convert them to python datetime objects but am having trouble.
My code:
for time in data:
pythondata[i]=datetime.strptime(time,"%y-%m-%dT%H:%M:%S+00:00")
I get the following error:
ValueError: time data '2012-03-01T00:05:55+00:00' does not match format '%y-%m-%dT%H:%M:%S+00:00'
It looks like I have the proper format, so why doesn't this work?
Change the year marker in your time format string to %Y:
time = '2012-03-01T00:05:55+00:00'
datetime.strptime(time, "%Y-%m-%dT%H:%M:%S+00:00")
# => datetime.datetime(2012, 3, 1, 0, 5, 55)
See strftime() and strptime() behavior.
I highly recommend python-dateutil library, it allows conversion of multiple datetime formats from raw strings into datetime objects with/without timezone set
>>> from dateutil.parser import parse
>>> parse('2012-04-30T23:08:56+00:00')
datetime.datetime(2012, 4, 30, 23, 8, 56, tzinfo=tzutc())
I use connect the sqlite3 of chrome 'History' file for analysis. But there are data for last_visit_time, which is timestamp, but I don't know how to change it to real time.
I test like this:
2012-04-18 23:22:11.084300 (utctime)
2012-04-19 01:22:11.084300 (myPC time)
12,979,264,931,952,304 (the time stamp in the data)
I printed the utctime and clicked a website, almost at the same time. So I get the statistic like above. 12,979,264,931,952,304 is the long int, so normal way to convert is not possible.
How can I convert the timestamp to a date?
The timestamp it stores is the number of microseconds since midnight UTC on January 1st, 1601. To convert this to the current time, you can add the number of microseconds you have to the epoch date, with help from this answer, like this:
>>> import datetime
>>> epoch_start = datetime.datetime(1601, 1, 1)
>>> delta = datetime.timedelta(microseconds=12979264931952304)
>>> epoch_start + delta
datetime.datetime(2012, 4, 18, 23, 22, 11, 952304)
To convert to your local timezone, you can use this method (note that I am currently UTC - 4, while it looks like you are UTC + 2):
>>> from dateutil import tz
>>> from_zone = tz.tzutc()
>>> to_zone = tz.tzlocal()
>>> utc_time = (epoch_start + delta).replace(tzinfo=from_zone)
>>> utc_time.astimezone(to_zone)
datetime.datetime(2012, 4, 18, 19, 22, 11, 952304, tzinfo=tzlocal())
I'm currently working on the backend for a calendaring system that returns naive Python datetimes. The way the front end works is the user creates various calendar events, and the frontend returns the naive version of the event they created (for example, if the user selects October 5, 2020 from 3:00pm-4:00pm, the frontend returns datetime.datetime(2020, 10, 5, 15, 0, 0) as the start and datetime.datetime(2011, 10, 5, 16, 0, 0) as the end.
What I need to do is to take the naive datetime and convert it into UTC for storage in a database. Each user of the system has already specified their timezone preference, so the naive datetime is considered to be of the same timezone as their timezone preference. Obviously the datetimes need to be stored relative to UTC so that if users change their timezone, existing events will still render at the correct time that they scheduled them.
The frontend is outside my control, so I can't change the data that I'm receiving. The database design is also outside my control, so I can't change what data is being stored and how.
Here is the approximate approach I have taken so far:
import pytz
def convert_to_UTC(naive_datetime, user_tz_preference):
user_datetime = naive_datetime.replace(tzinfo=user_tz_preference)
utc_datetime = user_datetime.astimezone(pytz.utc)
The problem I ran into is related to Daylight Savings Time:
>>> from datetime import datetime
>>> import pytz
>>> user_tz_preference = pytz.timezone('US/Pacific')
>>> naive_datetime = datetime(2011, 10, 26, 12, 0, 0)
>>> user_datetime = naive_datetime.replace(tzinfo=user_tz_preference)
>>> user_datetime
datetime.datetime(2011, 10, 26, 12, 0, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>)
>>> received_utc = user_datetime.astimezone(pytz.utc)
>>> received_utc
datetime.datetime(2011, 10, 26, 20, 0, tzinfo=<UTC>)
>>> expected_utc = datetime(2011, 10, 26, 19, 0, tzinfo=pytz.utc)
>>> expected_utc == received_utc
False
Notice that using 'replace' sets the timezone to PST instead of PDT regardless of the date, which gives it a UTC offset of 8 hours instead of the expected 7 hours DST offset, so the time ends up being saved incorrectly.
What options do I have for converting the naive datetime to the correct PDT (or other timezone-relative DST) tzinfo?
(Also, please note that not all users live in a timezone that observes DST, or may live in a timezone that switches over at different times, so in order to do a solution like a timedelta correction before saving, I would need to know if the timezone supports DST, and on which dates it switches over).
Pytz's localize function can do this: http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
from datetime import datetime
import pytz
tz = pytz.timezone('US/Pacific')
naive_dt = datetime(2020, 10, 5, 15, 0, 0)
utc_dt = tz.localize(naive_dt, is_dst=None).astimezone(pytz.utc)
# -> 2020-10-05 22:00:00+00:00
With zoneinfo from Python 3.9's standard lib:
from datetime import datetime
from zoneinfo import ZoneInfo
naive_datetime = datetime(2011, 10, 26, 12, 0, 0)
user_tz_preference = ZoneInfo("America/Los_Angeles") # former US/Pacific
# it is safe to replace the tzinfo:
user_datetime = naive_datetime.replace(tzinfo=user_tz_preference)
# ...or set it directly:
user_datetime = datetime(2011, 10, 26, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
# astimezone works as before:
utc_datetime = user_datetime.astimezone(ZoneInfo("UTC"))
print(repr(user_datetime))
# datetime.datetime(2011, 10, 26, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific'))
print(user_datetime.isoformat())
# 2011-10-26T12:00:00-07:00
print(utc_datetime.isoformat())
# 2011-10-26T19:00:00+00:00
docs