Parse a date in a specific timezone with Python - python

How can I take this date string:
"2015-01-01"
and, assuming it is in a specific timezone (say, "US-Mountain"), convert it to a POSIX timestamp?
Like so:
magic_parse_function("2015-01-01", pytz.timezone("US-Mountain")) -> 1420095600
I've spent quite some time scouring the docs and this site, playing with aware/unaware datetime objects, and am hoping for a not-too-crazy solution. The following does not work, the last two lines of output are identical, and they should be 3600 seconds apart:
import datetime
import time
import pytz
timestring = "2015-01-01"
pacific = pytz.timezone("US/Pacific")
mountain = pytz.timezone("US/Mountain")
(year, month, day) = timestring.split('-')
year = int(year)
month = int(month)
day = int(day)
unaware = datetime.datetime(year, month, day, 0, 0, 0, 0)
# aware_pacific = pacific.localize(unaware)
# aware_mountain = mountain.localize(unaware)
aware_mountain = unaware.replace(tzinfo=mountain)
aware_pacific = unaware.replace(tzinfo=pacific)
print time.mktime(aware_pacific.timetuple())
print time.mktime(aware_mountain.timetuple())

There are three steps:
Convert the date string into a naive datetime object:
from datetime import datetime
dt = datetime(*map(int ,'2015-01-01'.split('-')))
Get a timezone-aware datetime object:
import pytz # $ pip install pytz
aware = pytz.timezone("US/Mountain").localize(dt, is_dst=None)
is_dst=None raises an exception for ambiguous or non-existing times. Here're more details about what is is_dst flag and why do you need it, see "Can I just always set is_dst=True?" section
Get POSIX timestamp:
timestamp = aware.timestamp()
.timestamp() is available since Python 3.3+. See multiple solutions for older Python versions.

Related

Python count start date + days

I am making a program where I input start date to dataStart(example 21.10.2000) and then input int days dateEnd and I convert it to another date (example 3000 = 0008-02-20)... Now I need to count these dates together, but I didn't managed myself how to do that. Here is my code.
from datetime import date
start=str(input("type start date (DD.MM.YYYY)"))
end=int(input("how many days from it?"))
dataStart=start.split(".")
days=int(dataStart[0])
months=int(dataStart[1])
years=int(dataStart[2])
endYears=0
endMonths=0
endDays=0
dateStart = date(years, months, days)
while end>=365:
end-=365
endYears+=1
else:
while end>=30:
end-=30
endMonths+=1
else:
while end>=1:
end-=1
endDays+=1
dateEnd = date(endYears, endMonths, endDays)
For adding days into date, you need to user datetime.timedelta
start=str(input("type start date (DD.MM.YYYY)"))
end=int(input("how many days from it?"))
date = datetime.strptime(start, "%d.%m.%Y")
modified_date = date + timedelta(days=end)
print(datetime.strftime(modified_date, "%d.%m.%Y"))
You may use datetime.timedelta to add certain units of time to your datetime object.
See the answers here for code snippets: Adding 5 days to a date in Python
Alternatively, you may wish to use the third-party dateutil library if you need support for time additions in units larger than weeks. For example:
>>> from datetime import datetime
>>> from dateutil import relativedelta
>>> one_month_later = datetime(2017, 5, 1) + relativedelta.relativedelta(months=1)
>>> one_month_later
>>> datetime.datetime(2017, 6, 1, 0, 0)
It will be easier to convert to datetime using datetime.datetime.strptime and for the part about adding days just use datetime.timedelta.
Below is a small snippet on how to use it:
import datetime
start = "21.10.2000"
end = 8
dateStart = datetime.datetime.strptime(start, "%d.%m.%Y")
dateEnd = dateStart + datetime.timedelta(days=end)
dateEnd.date() # to get the date format of the endDate
If you have any doubts please look at the documentation python3/python2.

Convert local time to UNIX

I'm new to python and I'm trying to get the actual minutes passed every day since 7:00.
I am using mktime to get now_date1 and now_date2 in seconds, and then the plan it's to subtract and divide by 60 to get the minutes.
But I get the following error:
AttributeError: 'str' object has no attribute 'timetuple'
It's this the correct approach?
Here it's the code
import time
import pytz
from datetime import datetime
from time import mktime as mktime
now_date = datetime.now(pytz.timezone('Europe/Bucharest'))
now_date1 = now_date.strftime('%H:%M:%S')
now_date2 = now_date.strftime('7:00:00')
# Convert to Unix timestamp
d1_ts = time.mktime(now_date1.timetuple())
strftime returns a string. Not what you want.
You were pretty close, but there's no need to put time in the mix. Just modify your code like this and use time delta from datetime (inspired by How to calculate the time interval between two time strings):
import pytz
from datetime import datetime
now_date = datetime.now(pytz.timezone('Europe/Bucharest'))
from datetime import datetime
FMT = '%H:%M:%S'
now_date1 = now_date.strftime(FMT)
now_date2 = now_date.strftime('7:00:00')
tdelta = datetime.strptime(now_date1, FMT) - datetime.strptime(now_date2, FMT)
print(tdelta)
I get: 6:40:42 which seems to match since it's 12:42 here.
To get the result in minutes just do:
tdelta.seconds//60
(note that the dates have only correct hour/time/seconds, the year, month, etc.. are 1900 ... since they're not used)
I think something like this might work:
import time
import datetime
from time import mktime as mktime
#current time
now_date = datetime.datetime.now()
#time at 7am
today = datetime.date.today()
now_date2 = datetime.datetime(today.year, today.month, today.day, 7, 0, 0, 0)
#difference in minutes
(now_date - now_date2).days * 24 * 60

Convert datetime format to unix timestamp in Python [duplicate]

I have dt = datetime(2013,9,1,11), and I would like to get a Unix timestamp of this datetime object.
When I do (dt - datetime(1970,1,1)).total_seconds() I got the timestamp 1378033200.
When converting it back using datetime.fromtimestamp I got datetime.datetime(2013, 9, 1, 6, 0).
The hour doesn't match. What did I miss here?
solution is
import time
import datetime
d = datetime.date(2015,1,5)
unixtime = time.mktime(d.timetuple())
If you want to convert a python datetime to seconds since epoch you should do it explicitly:
>>> import datetime
>>> datetime.datetime(2012, 04, 01, 0, 0).strftime('%s')
'1333234800'
>>> (datetime.datetime(2012, 04, 01, 0, 0) - datetime.datetime(1970, 1, 1)).total_seconds()
1333238400.0
In Python 3.3+ you can use timestamp() instead:
>>> import datetime
>>> datetime.datetime(2012, 4, 1, 0, 0).timestamp()
1333234800.0
What you missed here is timezones.
Presumably you've five hours off UTC, so 2013-09-01T11:00:00 local and 2013-09-01T06:00:00Z are the same time.
You need to read the top of the datetime docs, which explain about timezones and "naive" and "aware" objects.
If your original naive datetime was UTC, the way to recover it is to use utcfromtimestamp instead of fromtimestamp.
On the other hand, if your original naive datetime was local, you shouldn't have subtracted a UTC timestamp from it in the first place; use datetime.fromtimestamp(0) instead.
Or, if you had an aware datetime object, you need to either use a local (aware) epoch on both sides, or explicitly convert to and from UTC.
If you have, or can upgrade to, Python 3.3 or later, you can avoid all of these problems by just using the timestamp method instead of trying to figure out how to do it yourself. And even if you don't, you may want to consider borrowing its source code.
(And if you can wait for Python 3.4, it looks like PEP 341 is likely to make it into the final release, which means all of the stuff J.F. Sebastian and I were talking about in the comments should be doable with just the stdlib, and working the same way on both Unix and Windows.)
Rather than this expression to create a POSIX timestamp from dt,
(dt - datetime(1970,1,1)).total_seconds()
Use this:
int(dt.strftime("%s"))
I get the right answer in your example using the second method.
EDIT: Some followup... After some comments (see below), I was curious about the lack of support or documentation for %s in strftime. Here's what I found:
In the Python source for datetime and time, the string STRFTIME_FORMAT_CODES tells us:
"Other codes may be available on your platform.
See documentation for the C library strftime function."
So now if we man strftime (on BSD systems such as Mac OS X), you'll find support for %s:
"%s is replaced by the number of seconds since the Epoch, UTC (see mktime(3))."
Anyways, that's why %s works on the systems it does. But there are better solutions to OP's problem (that take timezones into account). See #abarnert's accepted answer here.
For working with UTC timezones:
time_stamp = calendar.timegm(dt.timetuple())
datetime.utcfromtimestamp(time_stamp)
You've missed the time zone info (already answered, agreed)
arrow package allows to avoid this torture with datetimes; It is already written, tested, pypi-published, cross-python (2.6 — 3.xx).
All you need: pip install arrow (or add to dependencies)
Solution for your case
dt = datetime(2013,9,1,11)
arrow.get(dt).timestamp
# >>> 1378033200
bc = arrow.get(1378033200).datetime
print(bc)
# >>> datetime.datetime(2013, 9, 1, 11, 0, tzinfo=tzutc())
print(bc.isoformat())
# >>> '2013-09-01T11:00:00+00:00'
If your datetime object represents UTC time, don't use time.mktime, as it assumes the tuple is in your local timezone. Instead, use calendar.timegm:
>>> import datetime, calendar
>>> d = datetime.datetime(1970, 1, 1, 0, 1, 0)
>>> calendar.timegm(d.timetuple())
60
def dt2ts(dt, utc=False):
if utc:
return calendar.timegm(dt.timetuple())
if dt.tzinfo is None:
return int(time.mktime(dt.timetuple()))
utc_dt = dt.astimezone(tz.tzutc()).timetuple()
return calendar.timegm(utc_dt)
If you want UTC timestamp :time.mktime just for local dt .Use calendar.timegm is safe but dt must the utc zone so change the zone to utc. If dt in UTC just use calendar.timegm.
def datetime_to_epoch(d1):
"""
January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch
:param d1: input date
:return: seconds since unix epoch
"""
if not d1.tzinfo:
raise ValueError("date is missing timezone information")
d2 = datetime(1970, 1, 1, tzinfo=timezone.utc)
time_delta = d1 - d2
ts = int(time_delta.total_seconds())
return ts
def epoch_to_datetime_string(timestamp, tz_name="UTC", **kwargs):
"""
method to convert unix timestamp to date time string
:param ts: 10 digit unix timestamp in seconds
:param tz_name: timezone name
:param kwargs: formatter=<formatter-string>
:return: date time string in timezone
"""
naive_date = datetime.fromtimestamp(timestamp)
aware_date = naive_date.astimezone(pytz.timezone(tz_name))
formatter = kwargs.pop("formatter", "%d %b %Y %H:%M:%S")
return aware_date.strftime(formatter)
Well, when converting TO unix timestamp, python is basically assuming UTC, but while converting back it will give you a date converted to your local timezone.
See this question/answer;
Get timezone used by datetime.datetime.fromtimestamp()
This class will cover your needs, you can pass the variable into ConvertUnixToDatetime & call which function you want it to operate based off.
from datetime import datetime
import time
class ConvertUnixToDatetime:
def __init__(self, date):
self.date = date
# Convert unix to date object
def convert_unix(self):
unix = self.date
# Check if unix is a string or int & proceeds with correct conversion
if type(unix).__name__ == 'str':
unix = int(unix[0:10])
else:
unix = int(str(unix)[0:10])
date = datetime.utcfromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S')
return date
# Convert date to unix object
def convert_date(self):
date = self.date
# Check if datetime object or raise ValueError
if type(date).__name__ == 'datetime':
unixtime = int(time.mktime(date.timetuple()))
else:
raise ValueError('You are trying to pass a None Datetime object')
return type(unixtime).__name__, unixtime
if __name__ == '__main__':
# Test Date
date_test = ConvertUnixToDatetime(datetime.today())
date_test = date_test.convert_date()
print(date_test)
# Test Unix
unix_test = ConvertUnixToDatetime(date_test[1])
print(unix_test.convert_unix())
import time
from datetime import datetime
time.mktime(datetime.now().timetuple())

How to convert tomorrows (at specific time) date to a timestamp

How can i actually create a timestamp for the next 6 o'clock, whether that's today or tomorrow?
I tried something with datetime.datetime.today() and replace the day with +1 and hour = 6 but i couldnt convert it into a timestamp.
Need your help
To generate a timestamp for tomorrow at 6 AM, you can use something like the following. This creates a datetime object representing the current time, checks to see if the current hour is < 6 o'clock or not, creates a datetime object for the next 6 o'clock (including adding incrementing the day if necessary), and finally converts the datetime object into a timestamp
from datetime import datetime, timedelta
import time
# Get today's datetime
dtnow = datetime.now()
# Create datetime variable for 6 AM
dt6 = None
# If today's hour is < 6 AM
if dtnow.hour < 6:
# Create date object for today's year, month, day at 6 AM
dt6 = datetime(dtnow.year, dtnow.month, dtnow.day, 6, 0, 0, 0)
# If today is past 6 AM, increment date by 1 day
else:
# Get 1 day duration to add
day = timedelta(days=1)
# Generate tomorrow's datetime
tomorrow = dtnow + day
# Create new datetime object using tomorrow's year, month, day at 6 AM
dt6 = datetime(tomorrow.year, tomorrow.month, tomorrow.day, 6, 0, 0, 0)
# Create timestamp from datetime object
timestamp = time.mktime(dt6.timetuple())
print(timestamp)
To get the next 6 o'clock while handling timezones that observe Daylight saving time (DST) correctly:
from datetime import datetime, time, timedelta
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
DAY = timedelta(1)
local_timezone = get_localzone()
now = datetime.now(local_timezone)
naive_dt6 = datetime.combine(now, time(6))
while True:
try:
dt6 = local_timezone.localize(naive_dt6, is_dst=None)
except pytz.NonExistentTimeError: # no such time today
pass
except pytz.AmbiguousTimeError: # DST transition (or similar)
dst = local_timezone.localize(naive_dt6, is_dst=True)
std = local_timezone.localize(naive_dt6, is_dst=False)
if now < min(dst, std):
dt6 = min(dst, std)
break
elif now < max(dst, std):
dt6 = max(dst, std)
break
else:
if now < dt6:
break
naive_dt6 += DAY
Once you have an aware datetime object that represents the next 6 o'clock in the local timezone, it is easy to get the timestamp:
timestamp = dt6.timestamp() # in Python 3.3+
Or on older Python versions:
timestamp = (dt6 - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
See Converting datetime.date to UTC timestamp in Python.
The solution works even if any of the following happens:
python (e.g., time.mktime() calls) has no access to a historical timezone database on a given system (notably: Windows)—pytz provides a portable access to the tz database
there is a DST transition between now and the next X hour (where X is 6am in your case) or if the UTC offset for the local timezone has changed for any other reason—"naive datetime object + relativedelta" solution would fail silently to find the correct number of seconds but timezone-aware datetime objects could enable to find the right time difference
the nominal next X hour (today or tomorrow) does not exist or ambiguous in the local time zone (most often, it happens during DST transitions—every year in many timezones). Solutions using dateutil tzinfos or pytz-based solutions that use .localize() without is_dst=None would fail silently. The application should handle NonExistentTimeError and AmbiguousTimeError exceptions explicitly in this case
the current time is after the first time an ambiguous X hour happens in the local timezone but before the second time the X hour happens —"rrule + return min(localize(ndt, is_dst=True), localize(ndt, is_dst=False))" solution would fail silently. The min/max code in the AmbiguousTimeError clause above handles it correctly.

How to, with python, take a UTC datetime, autodetect users timezone, and convert the datetime to desired format?

There are a ton of questions about UTC datetime conversions and there doesn't seems to be a consensus of a "best way".
According to this: http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ , pytz is the best way to go. he shows converting to timezone like this datetime.datetime.utcnow().replace(tzinfo=pytz.utc) but he doesn't say how to get the user's timezone...
This guy https://stackoverflow.com/a/7465359/523051 says "localize adjusts for Daylight Savings Time, replace does not"
Everyone I see using pytz is supplying their own timezone (users_timezone = timezone("US/Pacific")), which I don't understand because you can't know if that's where your viewer is...
This guy https://stackoverflow.com/a/4771733/523051 has a way to auto-detect the timezones, but this is using the dateutil library, and not pytz, as is recommended by both Armin Ronacher and the official python docs ( http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior , just above that anchor in yellow box)
All I need is the most simplest, future-proof, all daylight savings time/etc considered way to take my datetime.utcnow() stamp (2012-08-25 10:59:56.511479), convert it the user's timezone. And show it like this:
Aug 25 - 10:59AM
and if the year is not the current year, I'd like to say
Aug 25 '11 - 10:59AM
alright, here it is (also, my first contribution to SO :))
it does require 2 external libraries which may throw some off...
from datetime import datetime
from dateutil import tz
import pytz
def standard_date(dt):
"""Takes a naive datetime stamp, tests if time ago is > than 1 year,
determines user's local timezone, outputs stamp formatted and at local time."""
# determine difference between now and stamp
now = datetime.utcnow()
diff = now - dt
# show year in formatting if date is not this year
if (diff.days / 365) >= 1:
fmt = "%b %d '%y # %I:%M%p"
else:
fmt = '%b %d # %I:%M%p'
# get users local timezone from the dateutils library
# http://stackoverflow.com/a/4771733/523051
users_tz = tz.tzlocal()
# give the naive stamp timezone info
utc_dt = dt.replace(tzinfo=pytz.utc)
# convert from utc to local time
loc_dt = utc_dt.astimezone(users_tz)
# apply formatting
f = loc_dt.strftime(fmt)
return f

Categories

Resources