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
Related
I have date in the as string in the following format: 202001010000
I am trying to convert this to UNIX format and get the result in UTC
I did:
import datetime
stime = "202001010000"
print(int(datetime.datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=datetime.timezone.utc).timestamp()))
and this is giving me the output in UNIX, but in CEST format.
With the above code I get: 1577836800 but I want the output to be 1577833200
What is the mistake I am doing?
You're setting time zone to UTC when converting to datetime. But since your input represents time in Germany you want a time zone that is active there. EX:
from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9+, can use backports.zoneinfo for older versions
stime = "202001010000"
# stime represents time in Germany so we use time zone
time_zone = ZoneInfo('Europe/Berlin')
# to datetime, with tz set:
dtobj = datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=time_zone)
# unix time
ts = dtobj.timestamp()
print(ts)
# 1577833200.0
# back to datetime, again specify time zone
dtobj = datetime.fromtimestamp(ts, tz=time_zone)
print(dtobj)
# 2020-01-01 00:00:00+01:00
Note that if the input represents the same time zone your OS is configured to use, this works correctly without setting a time zone. But I think it's better to be explicit here, to avoid confusion if you e.g. run this script on a machine configured to use another time zone.
What you're trying to get is 7 hours behind and you cannot do that from your start date. You must push your start date back 1 day and push your hours forward 17. This code will work for you
import datetime
stime = "201912310000"
my_date = datetime.datetime.strptime(stime, "%Y%m%d%H%M")
my_date_utc = my_date.replace(hour=17)
my_timestamp = my_date_utc.timestamp()
print(int(my_timestamp))
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 want to write a small python which needs to generate a simple precise timestamp each day ( the script will be run each day) at a particular hour say 1pm. I wrote something like this:
from datetime import datetime
now = datetime.utcnow() # Current time
then = datetime(1970,1,1) # 0 epoch time
ts = now - then
ts = ts.days * 24 * 3600 + ts.seconds
print ts
This is good, but i want to pass now for the time_field that i on daily basis. How do I do this?
You can use the time.mktime function:
>>> import datetime
>>> import time
>>> dt = datetime.datetime.today().replace(hour=13, minute=0, second=0, microsecond=0)
>>> print(dt)
2016-04-06 13:00:00
>>> time.mktime(dt.timetuple())
1459944000.0
If you need to be timezone-aware, use a timezone-aware datetime's utctimetuple method and the time.gmtime function.
(edited to show how to create datetime.datetime for a specific time)
date command in shell gives us epoch time also.
date +%s
So write your python script as
import subprocess
p = subprocess.Popen(['date', '+%s'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = p.communicate()
print out
and then call it daily with the same logic you are using now.
To generate a POSIX timestamp that corresponds to 1pm:
#!/usr/bin/env python3
from datetime import datetime
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone()
now = datetime.now(local_timezone)
one_pm = local_timezone.localize(datetime(now.year, now.month, now.day, 13), # 1pm
is_dst=None) # assert that there is no DST transition at 1pm
posix_time = one_pm.timestamp()
If there is no datetime.timestamp() method on your Python version then it is easy to implement it for a timezone-aware datetime object such as one_pm:
def posix_timestamp(aware, epoch=datetime(1970, 1, 1)):
utc = aware.replace(tzinfo=None) - aware.utcoffset() # convert to utc
return (utc - epoch).total_seconds()
If there is no DST transition at 1pm and mktime() uses a correct timezone definition on a given platform then to get Unix time that corresponds to 1pm:
#!/usr/bin/env python
import time
unix_time = time.mktime(time.localtime()[:3] + (13, 0, 0) + (-1,)*3)
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)