Python date object Convert to UTC timestamp at midnight - python

I have python date object like
datetime.date(2020, 3, 29) # d1, django DateField in database
Now i want to get UTC timestamp of that date object d1 at midnight
I searched a lot but nothing works.
I tried
d2 = datetime.combine(d1, datetime.min.time())
d2.timestamp() // This is localtimezone i want in UTC
Not sure why i need to add time

assuming your date object d is in UTC, you could simply add midnight time (00:00:00) and timezone info UTC and then apply timestamp() method to get the desired timestamp. Example:
import datetime as dt
d = dt.date(2020, 3, 29)
ts_utc = dt.datetime.combine(d, dt.time(0,0,0), tzinfo=dt.timezone.utc).timestamp()
print(ts_utc)
# 1585440000.0
check = dt.datetime.utcfromtimestamp(ts_utc)
print(check)
# 2020-03-29 00:00:00
Note: it is important to set the timezone, otherwise, the timestamp() method assumes that the input is in the timezone that the machine you run this code on is set to!

Related

Add year/month/day to an UNIX timestamp with just hour/min/sec in python

The range of my timestamp is from 34200 to 57600, so it covers a part of one day from 9:30 AM). I want to add a specific year/month/day to this timestamp. How can I do that in python? Suppose that my timestamp is 34201.054427731004. I want a timestamp with information about year/month/day (for example, 3/2/2017) as the output. So here, the UNIX output is a full timestamp not just the hour/mean/sec.
Make a datetime object with the date, which will initialise hours and minutes to 0, and add a timedelta with the seconds to it:
from datetime import datetime, timedelta
date = datetime(2017, 2, 3) + timedelta(seconds=34201)
print(date.timestamp())

Compute midnight timestamp in UTC +0 using DateTime python 3.5

I need to scrap an online database which contain +/- 24h of data at fixed interval using an API query which contain a timestamp. Because i don't know where the server is choose something simple like midnigth UTC.
I found lot of documentation on SO to compute UTC aware of local zone. I'm actually using this protocole to get actual UTC Date :
import datetime
myDate = datetime.datetime.now(datetime.timezone.utc)
print("TZ INFO = ", myDate.tzinfo) # return UTC+00:00
print("DATE ", myDate) # return 2017-07-08 14:14:24.137003+00:00
print("ISO DATE = ", myDate.timestamp()) # return 1499523264.137003
First question, why the timestamp() returned take in account the local timezone : 1499523264.137003 is equal to ~16h15, so UTC +2 corresponding to France Zone. Why timestamp() doesn't return only the UTC + 0 timestamp ? How can i get an UTC + 0 timestamp ?
Second question, i try to generate a midnight date to query the API, so like i saw on many post on SO, i try to use the replace() function :
myDate = myDate.replace(hour=0, minute=0, second=0,microsecond=0).astimezone(pytz.utc)
print (myDate) # return 2017-07-08 00:00:00+00:00
But when i try to print (myDate.timestamp()) return another time a UTC + 2 timestamp, so 2AM of 2017-07-08. How can i get midnight UTC + 0 timestamp easily ?
I would suggest using the pendulum module since it makes timezone and date calculations easy to perform.
pendulum is aware of daylight savings time schemes, as indicated here for London and Paris. It can also provide the UTC time shorn of an adjustment for daylight savings time. When you need to provide an adjustment to UTC you can simply using the replace method in conjunction with UTC.
>>> import pendulum
>>> pendulum.create(2017,7,9,0,0,0,0,'Europe/London')
<Pendulum [2017-07-09T00:00:00+01:00]>
>>> pendulum.create(2017,7,9,0,0,0,0,'Europe/Paris')
<Pendulum [2017-07-09T00:00:00+02:00]>
>>> pendulum.create(2017,7,9,0,0,0,0,'UTC')
<Pendulum [2017-07-09T00:00:00+00:00]>
>>> t = pendulum.create(2017,7,9,0,0,0,0,'UTC')
>>> t.replace(hour=+2)
<Pendulum [2017-07-09T02:00:00+00:00]>

How to view MySQL formatted date and time when querying through Python?

So I have my Python file linked to the MySQL database, and I want to query for all the values in a couple tables, which includes a DATE entry, and a TIME entry. Now when I do this in MySQL, I get nicely formatted values like this: 2017-07-26, 12:30:00
However, when I run the python file that queries the table, I instead get these values: datetime.date(2017, 7, 26), datetime.timedelta(0, 45000))
How do I fix this so running the Python file returns the Date and Time values in the same format I get when I query in MySQL?
datetime.date(2017, 7, 26) is the Python object that represents the date, and datetime.timedelta(0, 45000) is a duration (here, 0 day and 45000 seconds) that we have to add to the date.
Python represents the combined date and time in datetime.datetime objects. We create one from the date and duration, then format it:
import datetime
date = datetime.date(2017, 7, 26)
delta = datetime.timedelta(0, 45000)
# We convert the date into a datetime object (same date, at midnight)
# and add the delta
date_and_time = datetime.datetime.combine(date, datetime.time()) + delta
mysql_formatted_date = datetime.datetime.strftime(date_and_time, "%Y-%m-%d, %H:%M:%S")
print(mysql_formatted_date)
#2017-07-26, 12:30:00
You can use strftime to convert a datetime object into any format you like.
e.g. for your case you can use -
value.strftime('%Y-%m-%d, %H:%M:%S')

Python datetime.datetime from time.structtime difference

I use feedparser to grab the entries from some RSS feeds.
The entries have a published_parsed field which are parsed by feedparser into time.structtime.
I use this function to convert the time.structtime into a datetime.datetime:
def publishParsedToDatetime(structTime):
return datetime.datetime.fromtimestamp(time.mktime(structTime))
Input (structtime):
time.struct_time(tm_year=2015, tm_mon=8, tm_mday=1, tm_hour=20, tm_min=28, tm_sec=33, tm_wday=5, tm_yday=213, tm_isdst=0)
Output (datetime):
2015-08-01 21:28:33
I see a problem which could be timezone related, there is 1 hour difference between the structtime and the datetime values.
The structtime value is UTC.
But the datetime.datetime value is neither UTC, nor my current timezone (CET, Central European Time, we observe Summertime, so we have UTC + 2hrs at the moment).
How can this be explained?
Actually, as explained in the documentation for datetime.fromtimestamp, it converts to local time by default:
Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive
The 1 hour difference can then be explained by the field tm_isdst=0 tells it to not use daylight savings (despite your local time zone using it).
To see this more clearly, we construct two test cases
import time, datetime
# this is how your time object was constructed before
tm_isdst = 0
t = time.mktime((2015, 8, 1, 20, 28, 33, 5, 213, tm_isdst))
print("Old conversion: {0}".format(datetime.datetime.fromtimestamp(t)))
# this is what happens if you let mktime "divine" a time zone
tm_isdst = -1
t = time.mktime((2015, 8, 1, 20, 28, 33, 5, 213, tm_isdst))
print("New conversion: {0}".format(datetime.datetime.fromtimestamp(t)))
The output of this is as follows:
Old conversion: 2015-08-01 21:28:33
New conversion: 2015-08-01 20:28:33
The problem then, you see, is that the structTime object being passed to your publishParsedToDatetime has tm_isdst=0 but the time stamp you wanted to parse was for a DST time zone.
As you have already noted in another comment, the proper solution to this is probably to always use UTC in your back-end code, and only do time zone handling when showing the time to the user, or when reading user input.
calendar.timegm takes a UTC timetuple as input and returns its timestamp.
In contrast, time.mktime takes a local timetuple as input and returns its (UTC) timestamp. All timestamps represent seconds since the Epoch, 1970-1-1 00:00:00 UTC.
utcfromtimestamp takes a timestamp as input and converts it to a naive
(i.e. timezone-unaware) UTC datetime.
fromtimestamp takes the same timestamp and converts it to the corresponding
naive local datetime.
Since your timetuples (e.g. structTime) are UTC timetuples, you should use calendar.timegm, not time.mktime, to find the correct timestamp.
Once you have the correct timestamp, fromtimestamp will return the corresponding naive local datetime.
import time
import calendar
import datetime as DT
timetuple = (2015, 8, 1, 20, 28, 33, 5, 213, 0)
timestamp = calendar.timegm(timetuple)
naive_local_date = DT.datetime.fromtimestamp(timestamp)
print('Naive local: {}'.format(naive_local_date))
yields
Naive local: 2015-08-01 22:28:33

django python date time set to midnight

I have a date time of my django object but it can be any time of the day. It can be at any time through the day, but I need to set my time to 00:00:00 (and another date to 23:59:59 but the principle will be the same)
end_date = lastItem.pub_date
currently the end date is 2002-01-11 12:34:56
What do I need to do to get this to change it to 00:00:00?
i tried:
end_date.hour = '00'
but got: 'datetime.datetime' object attribute 'time' is read-only
Using datetimes's "combine" with the time.min and time.max will give both of your datetimes.
For example:
from datetime import date, datetime, time
pub_date = date.today()
min_pub_date_time = datetime.combine(pub_date, time.min)
max_pub_date_time = datetime.combine(pub_date, time.max)
Result with pub_date of 6/5/2013:
min_pub_date_time -> datetime.datetime(2013, 6, 5, 0, 0)
max_pub_date_time -> datetime.datetime(2013, 6, 5, 23, 59, 59, 999999)
Try this:
import datetime
pub = lastItem.pub_date
end_date = datetime.datetime(pub.year, pub.month, pub.day)
Are you sure you don't want to use dates instead of datetimes? If you're always setting the time to midnight, you should consider using a date. If you really want to use datetimes, here's a function to get the same day at midnight:
def set_to_midnight(dt):
midnight = datetime.time(0)
return datetime.datetime.combine(dt.date(), midnight)
For tz-aware dates it should be:
datetime.combine(dt.date(), datetime.min.time(), dt.tzinfo)
datetime instance attributes like year, month, day, hour, etc are read-only, so you just have to create a new datetime object and assign it to end_date.

Categories

Resources