I have a a basic string + another string which reresents timezone. I'm trying to return the UTC equivalent..
timezone = "America/New_York"
scheduleDate = "2021-09-21 21:00:00"
dtimestamp = datetime.datetime.strptime(scheduleDate, '%Y-%m-%d %H:%M:%S').astimezone(
pytz.timezone(timezone)).timestamp()
sdate = datetime.datetime.utcfromtimestamp(dtimestamp).strftime('%Y-%m-%dT%H:%M:%SZ')
print(sdate)
This works, but not sure if there's a better approach for this. I haven't looked too closely at the pytz docs before.
from datetime import datetime
import pytz
timezone = "America/New_York"
scheduleDate = "2021-09-21 21:00:00"
# Convert to naive datetime
dt_naive: datetime = datetime.strptime(scheduleDate, '%Y-%m-%d %H:%M:%S')
# localize datetime with ET timezone
dt: datetime = pytz.timezone(timezone).localize(dt_naive)
# normalize datetime to UTC time
dt_utc: datetime = pytz.utc.normalize(dt)
print(str(dt_utc))
Related
I have a timestamp that was created with datetime module and now I need to convert '2020-10-08T14:52:49.387077+00:00' into 08/OCT/2020?
Can datetime do this? I have tried strptime but I think the +00:00 at the end is causing errors.
Use fromisoformat and strftime method from datetime package in Python 3.7:
from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.fromisoformat(time).strftime("%d/%b/%Y")
print(new_time)
Or with strptime:
from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.strptime(time.split('T')[0], "%Y-%m-%d").strftime("%d/%b/%Y")
print(new_time)
Output:
08/Oct/2020
I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.
I am trying to convert the local time to "UTC" time.
Followed this guide: How do I convert local time to UTC in Python?
But issue here is with the type of date which we giving here.
import pytz, datetime
local = pytz.timezone ("America/Los_Angeles")
naive = datetime.datetime.strptime ("2001-2-3 10:11:12", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive, is_dst=None)
utc_dt = local_dt.astimezone (pytz.utc)
In the above code input is "2001-2-3 10:11:12" (string), But in my case it will be a datetime object.
begin = begin.replace(hour=0, minute=0, second=0, microsecond=0)
Someone let me know how we can achieve the conversion here.
Your string format needs a bit of a modification. You just need the leading zeros in your month and day:
import pytz, datetime
local = pytz.timezone("America/Los_Angeles")
naive = datetime.datetime.strptime ("2001-02-03 10:11:12", "%Y-%m-%d %H:%M:%S")
local_dt = local.localize(naive)
utc_dt = local_dt.astimezone(pytz.utc)
If your input (begin) is not a time string but it is a naive (no timezone info) datetime object already then drop datetime.strptime() line (that parses the time string into datetime object) from the example. To convert a given naive datetime object that represents local time to utc:
import pytz # $ pip install pytz
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone()
local_dt = local_timezone.localize(begin, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)
I need to convert a value in microseconds to the format '%Y-%m-%d %H:%M:%S' and apply timezone information to adjust the output. I tried:
datatime.datetime.fromtimestamp(timestamp).strftime('format')
but that is not timezone aware. How do I apply timezone information when converting from microseconds to a date and time string?
To convert the timestamp ("seconds since the epoch") to time in UTC as a naive datetime object:
#!/usr/bin/env python
from datetime import datetime
timestamp = 1422025533
utc_time = datetime.utcfromtimestamp(timestamp)
print("Time is = %s" % utc_time)
# -> Time is = 2015-01-23 15:05:33
If you want to get the value in a specific timezone as an aware datetime object; you should use pytz module:
#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz
tz = pytz.timezone('Europe/London')
timestamp = 1422025533
london_time = datetime.fromtimestamp(timestamp, tz)
print("Time is = %s" % london_time.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
# -> Time is = 2015-01-23 15:05:33 GMT+0000
I think Delorean is the right lib you should use.
Delorean is a library for clearing up the inconvenient truths that arise dealing with datetimes in
from datetime import datetime
from pytz import timezone
EST = "US/Eastern"
UTC = "UTC"
d = datetime.utcnow()
utc = timezone(UTC)
est = timezone(EST)
d = utc.localize(d)
d = est.normalize(d)
print d
RethinkDB is a wonderfull and very handy NoSQL Database engine. I looking for the best way to insert Python datetime objects. RethinkDB strores UTC timestamps, so I found a solution to convert my datetime object in the right format.
I use this litle function to convert my datetime object in somethink RethinkDB understand :
import calendar
from datetime import datetime
import rethinkdb as r
def datetime_to_epoch_time(dt):
timestamp = calendar.timegm(dt.utctimetuple())
return r.epoch_time(timestamp)
title = u'foobar'
published_at = '2014-03-17 14:00'
# firts I convert 2014-03-17 14:00 to datetime
dt = datetime.strptime(published_at, '%Y-%m-%d %H:%M')
# then I store the result
r.table('stories').insert({
'title': title,
'published_at': datetime_to_epoch_time(dt),
}).run()
My current timezone is CET (GMT + 2 hours)
Is this a good solution for storing my dates in rethinkdb or a better solution exists ?
Thanks for your help
An example with Pytz :
from datetime import datetime
import pytz
import rethinkdb as r
# Init
r.connect('localhost', 28015).repl()
if 'test' in r.db_list().run():
r.db_drop('test').run()
r.db_create('test').run()
r.db('test').table_create('stories').run()
paris = pytz.timezone('Europe/Paris')
r.table('stories').insert({
'title': u'Foobar',
'published_at': paris.localize(datetime.strptime(
'2014-03-17 14:00', '%Y-%m-%d %H:%M'
), is_dst=False)
}).run()
for document in r.table("stories").run():
print(document['published_at'])
print(type(document['published_at']))
dt.utctimetuple() doesn't convert a naive dt to UTC timezone i.e., if published_at is not in UTC already then it returns the wrong result.
If published_at is in local timezone and therefore dt is in local timezone:
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
tz = get_localzone()
aware_dt = tz.localize(dt, is_dst=None)
timestamp = (aware_dt - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
# ... r.epoch_time(timestamp)