Python: Date and Time Data

The standard library for manipulating dates and times is `datetime <https://docs.python.org/3/library/datetime.html>`__. We will use the more full-featured `arrow <https://github.com/crsmithdev/arrow/>`__ library to illustrate concepts for working with date/time data, but the concepts are generally also applicable to the use of datetime.

Note that pandas also has a core set of functions for working with time series data.

In [1]:
import arrow
In [2]:
from datetime import datetime

A readable display for date and time

In [177]:
fmt = 'ddd hh:mm:ss A, DD-MMM-YYYY'

Creation

Local time now

In [178]:
local = arrow.now()
local
Out[178]:
<Arrow [2017-07-01T14:56:21.373285-04:00]>
In [179]:
local.format()
Out[179]:
'2017-07-01 14:56:21-04:00'
In [180]:
local.format(fmt)
Out[180]:
'Sat 02:56:21 PM, 01-Jul-2017'

UTC

Coordinated Universal Time (UTC) is the basis for civil time today. This 24-hour time standard is kept using highly precise atomic clocks combined with the Earth’s rotation. In practice, UTC shares the same current time as Greenwich Mean Time (GMT).

In [181]:
utc = arrow.utcnow()
In [182]:
utc.format()
Out[182]:
'2017-07-01 18:56:22+00:00'

Creation from timestamps

In [187]:
import time

ts = time.time()
ts
Out[187]:
1498935385.06168
In [188]:
arrow.get(ts).format(fmt)
Out[188]:
'Sat 06:56:25 PM, 01-Jul-2017'

Creation from strings

In [189]:
fisher_birthday = arrow.get('Fisher was born on October 21, 1956', 'MMMM DD, YYYY')
fisher_birthday.format('dddd, DD MMM YYYY')
Out[189]:
'Sunday, 21 Oct 1956'

From Unix date command

In [190]:
ts = ! date
ts
Out[190]:
['Sat Jul  1 14:56:26 EDT 2017']
In [191]:
tt = arrow.get(ts[0], 'ddd MMM  D hh:mm:ss ZZZ YYYY')
tt.format(fmt)
Out[191]:
'Sat 02:56:26 PM, 01-Jul-2017'

Creation from values

In [192]:
santa_is_coming = arrow.get(2017, 12, 24, 23, 59, 59)
santa_is_coming.format(fmt)
Out[192]:
'Sun 11:59:59 PM, 24-Dec-2017'

Conversion between time zones

In [183]:
utc.to('local').format(fmt)
Out[183]:
'Sat 02:56:22 PM, 01-Jul-2017'
In [184]:
hawaii = utc.to('US/Hawaii')
hawaii.format(fmt)
Out[184]:
'Sat 08:56:22 AM, 01-Jul-2017'
In [185]:
singapore = utc.to('Asia/Singapore')
singapore.format(fmt)
Out[185]:
'Sun 02:56:22 AM, 02-Jul-2017'
In [186]:
paris = utc.to('Europe/Paris')
paris.format(fmt)
Out[186]:
'Sat 08:56:22 PM, 01-Jul-2017'

Shifting

In [193]:
current = arrow.now()
current.format(fmt)
Out[193]:
'Sat 02:56:28 PM, 01-Jul-2017'
In [213]:
homework_due = current.shift(weeks=1, hours=5)
homework_due.format(fmt)
Out[213]:
'Sat 07:59:32 PM, 08-Jul-2017'

Replacing

In [195]:
last_year = current.replace(year=2016)
last_year.format(fmt)
Out[195]:
'Fri 02:56:28 PM, 01-Jul-2016'

Time differences

In [196]:
past = current.replace(hours=-4)
In [214]:
current - past
Out[214]:
datetime.timedelta(0, 14584, 145507)
In [197]:
print(current - past)
4:00:00

Ranges and iteration

In [198]:
current.span('day')
Out[198]:
(<Arrow [2017-07-01T00:00:00-04:00]>,
 <Arrow [2017-07-01T23:59:59.999999-04:00]>)
In [199]:
[t.format(fmt) for t in current.span('day')]
Out[199]:
['Sat 12:00:00 AM, 01-Jul-2017', 'Sat 11:59:59 PM, 01-Jul-2017']
In [200]:
[t.format(fmt) for t in current.span('month')]
Out[200]:
['Sat 12:00:00 AM, 01-Jul-2017', 'Mon 11:59:59 PM, 31-Jul-2017']
In [202]:
for t in arrow.Arrow.range('day', *current.span('week')):
    print(t.format(fmt))
Mon 12:00:00 AM, 26-Jun-2017
Tue 12:00:00 AM, 27-Jun-2017
Wed 12:00:00 AM, 28-Jun-2017
Thu 12:00:00 AM, 29-Jun-2017
Fri 12:00:00 AM, 30-Jun-2017
Sat 12:00:00 AM, 01-Jul-2017
Sun 12:00:00 AM, 02-Jul-2017
In [203]:
start = arrow.now()
end = start.replace(hours=6)
for t in arrow.Arrow.range('hour', start, end):
    print(t.format(fmt))
Sat 02:58:06 PM, 01-Jul-2017
Sat 03:58:06 PM, 01-Jul-2017
Sat 04:58:06 PM, 01-Jul-2017
Sat 05:58:06 PM, 01-Jul-2017
Sat 06:58:06 PM, 01-Jul-2017
Sat 07:58:06 PM, 01-Jul-2017
Sat 08:58:06 PM, 01-Jul-2017

Generating readable strings

Show a human readable difference between two times. By default, the difference is from the current time.

In [207]:
local.humanize()
Out[207]:
'4 minutes ago'
In [210]:
homework_due.humanize(arrow.now())
Out[210]:
'in 6 days'
In [211]:
homework_due.humanize(arrow.now(), locale='zh')
Out[211]:
'6天后'
In [212]:
homework_due.humanize(arrow.now(), locale='fr')
Out[212]:
'dans 6 jours'

Conversion between arrow and datetime

Arrow

In [3]:
t1 = arrow.now()
In [4]:
t1
Out[4]:
<Arrow [2017-07-01T13:40:57.541580-04:00]>

arrow -> datetime

In [5]:
t1.datetime
Out[5]:
datetime.datetime(2017, 7, 1, 13, 40, 57, 541580, tzinfo=tzlocal())
In [6]:
t1.timestamp
Out[6]:
1498930857

Compare with direct datetime call

In [17]:
t2 = datetime.now()
In [8]:
t2
Out[8]:
datetime.datetime(2017, 7, 1, 13, 40, 57, 571316)

Full compatibility with datetime

In [9]:
datetime.timestamp(t2)
Out[9]:
1498930857.571316
In [11]:
datetime.timestamp(t1.datetime)
Out[11]:
1498930857.54158