What is the best way to insert python datetime in rethinkdb? - python

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)

Related

Converting timedate string with timezone to utc python

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

Get only date from datetime generated string

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

converting local time to UTC time(datetime object)

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)

Convert datetime to unix timestamp in python

When I try to convert from UTC timestamp to normal date and add the right timezone I can't find the way to convert the time back to Unix timestamp.
What am I doing worng?
utc_dt = datetime.utcfromtimestamp(self.__modified_time)
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
utc = utc_dt.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
Central is equal to
2015-10-07 12:45:04+02:00
This is what I have when running the code, and I need to convert the time back to timestamp.
from datetime import datetime
from datetime import timedelta
from calendar import timegm
utc_dt = datetime.utcfromtimestamp(self.__modified_time)
from_zone = tz.tzutc()
to_zone = tz.tzlocal()
utc = utc_dt.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
unix_time_central = timegm(central.timetuple())
To get an aware datetime that represents time in your local timezone that corresponds to the given Unix time (self.__modified_time), you could pass the local timezone to fromtimestamp() directly:
from datetime import datetime
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone() # pytz tzinfo
central = datetime.fromtimestamp(self.__modified_time, local_timezone)
# -> 2015-10-07 12:45:04+02:00
To get the Unix time back in Python 3:
unix_time = central.timestamp()
# -> 1444214704.0
unix_time is equal to self.__modified_time (ignoring floating point errors and "right" timezones). To get the code for Python 2 and more details, see this answer.
Arrow ( http://crsmithdev.com/arrow/ ) appears to be the ultimate Python time-related library
import arrow
ts = arrow.get(1455538441)
# ts -> <Arrow [2016-02-15T12:14:01+00:00]>
ts.timestamp
# 1455538441

Convert microseconds into format string with timezone

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

Categories

Resources