py 10
py 10
Today: 2016-04-15
Yesterday: 2016-04-14
Tomorrow: 2016-04-16
The datetime module can convert a POSIX timestamp to a ITC datetime object.
import time
seconds_since_epoch=time.time() #1469182681.709
709000)
import calendar
if not m: m = 12
import datetime
import dateutil.relativedelta
d = datetime.datetime.strptime("2013-03-31", "%Y-%m-%d")
minimal libraries
Python has only limited support for parsing ISO 8601 timestamps. For strptime you need to know
exactly what
format it is in. As a complication the stringification of a datetime is an ISO 8601 timestamp, with
space as a
# '2016-07-22 09:25:59.555555'
# '2016-07-22 09:25:59'
But these 2 forms need a different format for strptime. Furthermore, strptime' does not support at
all
parsing minute timezones that have a:in it, thus2016-07-22 09:25:59+0300can be parsed, but the
There is a single-file library called iso8601 which properly parses ISO 8601 timestamps and only
them.
It supports fractions and timezones, and the T separator all with a single function:
import iso8601
iso8601.parse_date('2016-07-22 09:25:59')
iso8601.parse_date('2016-07-22 09:25:59+03:00')
iso8601.parse_date('2016-07-22 09:25:59Z')
iso8601.parse_date('2016-07-22T09:25:59.000111+03:00')
If no timezone is set, iso8601.parse_date defaults to UTC. The default zone can be changed with
default_zone
keyword argument. Notably, if this is None instead of the default, then those timestamps that do not
have an
iso8601.parse_date('2016-07-22T09:25:59', default_timezone=None)
# datetime.datetime(2016, 7, 22, 9, 25, 59)
iso8601.parse_date('2016-07-22T09:25:59Z', default_timezone=None)
datetime.now().isoformat()
# Out: '2016-07-31T23:08:20.886783'
datetime.now(tzlocal()).isoformat()
# Out: '2016-07-31T23:09:43.535074-07:00'
datetime.now(tzlocal()).replace(microsecond=0).isoformat()
# Out: '2016-07-31T23:10:30-07:00'
See ISO 8601 for more information about the ISO 8601 format.
Section 5.11: Parsing a string with a short time zone name into
Using the dateutil library as in the previous example on parsing timezone-aware timestamps, it is also
possible to
For dates formatted with short time zone names or abbreviations, which are generally ambiguous
(e.g. CST, which
could be Central Standard Time, China Standard Time, Cuba Standard Time, etc - more can be found
here) or not
necessarily available in a standard database, it is necessary to specify a mapping between time zone
abbreviation
ET = tz.gettz('US/Eastern')
CT = tz.gettz('US/Central')
MT = tz.gettz('US/Mountain')
PT = tz.gettz('US/Pacific')
dt_est
# datetime.datetime(2014, 1, 2, 4, 0, tzinfo=tzfile('/usr/share/zoneinfo/US/Eastern'))
dt_pst
It is worth noting that if using a pytz time zone with this method, it will not be properly localized:
import pytz
EST = pytz.timezone('America/New_York')
If using this method, you should probably re-localize the naive portion of the datetime after parsing:
dt_fixed = dt.tzinfo.localize(dt.replace(tzinfo=None))
of a text)
It is possible to extract a date out of a text using the dateutil parser in a "fuzzy" mode, where
components of the
print(dt)
dt is now a datetime object and you would see datetime.datetime(2047, 1, 1, 8, 21) printed.
Sometimes you want to iterate over a range of dates from a start date to some end date. You can do
it using
import datetime
day_delta = datetime.timedelta(days=1)
start_date = datetime.date.today()
print(start_date + i*day_delta)
Which produces:
2016-07-21
2016-07-22
2016-07-23
2016-07-24
2016-07-25
2016-07-26
2016-07-27
a = datetime(2016,10,06,0,0,0)
b = datetime(2016,10,01,23,59,59)
a-b
# datetime.timedelta(4, 1)
(a-b).days
#4
(a-b).total_seconds()
# 518399.0
datetime_for_string = datetime(2016,10,1,0,0)
datetime.strftime(datetime_for_string,datetime_string_format)
datetime.strptime(datetime_string, datetime