Convert datetime to UTC - python

I have a date string like - 2015-01-05T10:30:47-0800,
It looks to me that this is some timezone because of the offset. How can I get a date string which is in the UTC timezone from the above date string.
I tried the following -
datestring = '2015-01-05T10:30:47-0800'
from dateutil import parser
d = parser.parse(datestring) # datetime.datetime(2015, 1, 5, 10, 30, 47, tzinfo=tzoffset(None, -28800))
import pytz
d.astimezone(pytz.timezone('UTC')) # datetime.datetime(2015, 1, 5, 18, 30, 47, tzinfo=<UTC>)
EDIT -
The above code returns the correct answer. My bad!

Try this:
>>> import dateutil.parser
>>> d = dateutil.parser.parse('2015-01-05T10:30:47-0800')
>>> d.astimezone(dateutil.tz.tzutc())
datetime.datetime(2015, 1, 5, 18, 30, 47, tzinfo=tzutc())

Related

naive datetime object shows different time when converted utc

I am trying to understand how datetime objects behave in python
>>> import pytz
>>> from datetime import datetime
>>> now = datetime.now()
>>> now
datetime.datetime(2022, 10, 11, 22, 9, 14, 169110)
>>> now.tzinfo
# even though there is no tzinfo, when converted to utc, it shows 20:09
>>> now.astimezone(pytz.utc)
datetime.datetime(2022, 10, 11, 20, 9, 14, 169110, tzinfo=<UTC>)
>>> pytz.utc.localize(now)
datetime.datetime(2022, 10, 11, 22, 9, 14, 169110, tzinfo=<UTC>)
I am trying to understand the reason of the change in the time with line now.astimezone(pytz.utc) since the datetime object was naive in the first place.

How to update Python datetime object with timezone offset given in such format: "+0300"?

Given a datetime.datetime object like that:
datetime.datetime(2022, 2, 22, 9, 24, 20, 386060)
I get client timezone offset in such format: "+0300" and need to represent the datetime.datetime object considering this offset.
For example, the object above should look like this:
datetime.datetime(2022, 2, 22, 12, 24, 20, 386060)
IIUC, you have a datetime object that represents UTC and want to convert to a UTC offset of 3 hours. You can do that like
import datetime
dt = datetime.datetime(2022, 2, 22, 9, 24, 20, 386060)
# assuming this is UTC, we need to set that first
dt = dt.replace(tzinfo=datetime.timezone.utc)
# now given the offset
offset = "+0300"
# we can convert like
converted = dt.astimezone(datetime.datetime.strptime(offset, "%z").tzinfo)
>>> converted
datetime.datetime(2022, 2, 22, 12, 24, 20, 386060, tzinfo=datetime.timezone(datetime.timedelta(seconds=10800)))

Add seconds and microseconds to datetime if they are not set

I have following datetime:
dt = datetime.datetime(2021, 10, 15, 0, 0, tzinfo=datetime.timezone.utc)
How can I add seconds and microsends to it with python code? So it has the same structure as:
datetime.datetime(2021, 10, 18, 15, 31, 21, 436248, tzinfo=datetime.timezone.utc)
You can use timedelta for that. For example
from datetime import timedelta
dt = datetime.datetime(2021, 10, 15, 0, 0, tzinfo=datetime.timezone.utc)
if not dt.second:
dt = dt + timedelta(seconds=21)
if not dt.microsecond:
dt = dt + timedelta(microseconds=23)
I am not particularly sure what you want to do but you can do it like this with datetime, if you're trying to get the time this instant:
from datetime import datetime
#get seconds/microseconds
now = datetime.now()
seconds = int(now.strftime("%S"))
microseconds = int(now.strftime("%f"))
datetime.datetime(2021, 10, 15, 31, seconds, microseconds, tzinfo=datetime.timezone.utc)

Datetime - 10 Hours

Consider:
now = datetime.datetime.now()
now
datetime.datetime(2009, 11, 6, 16, 6, 42, 812098)
How would I create a new datetime object (past) and minus n values from the hours?
Use timedelta in the datetime module:
import datetime
now = datetime.datetime.now()
past = now - datetime.timedelta(hours=10)
Use a timedelta object.
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 11, 6, 16, 35, 50, 593000)
>>> ten_hours = datetime.timedelta(hours=10)
>>> now + ten_hours
datetime.datetime(2009, 11, 7, 2, 35, 50, 593000)
>>> now - ten_hours
datetime.datetime(2009, 11, 6, 6, 35, 50, 593000)
Use a timedelta object.
from datetime import datetime
back = datetime.now() - timedelta(hours=10)

Python, Convert 9 tuple UTC date to MySQL datetime format

I am parsing RSS feeds with the format as specified here: http://www.feedparser.org/docs/date-parsing.html
date tuple (2009, 3, 23, 13, 6, 34, 0, 82, 0)
I am a bit stumped at how to get this into the MySQL datetime format (Y-m-d H:M:S)?
tup = (2009, 3, 23, 13, 6, 34, 0, 82, 0)
import datetime
d = datetime.datetime(*(tup[0:6]))
#two equivalent ways to format it:
dStr = d.isoformat(' ')
#or
dStr = d.strftime('%Y-%m-%d %H:%M:%S')

Categories

Resources