1. cro haco
  2. datetimes

Overview

HTTPS SSH

Requirements

  • Python 2.5 or later.

Install

$ pip install datetimes

History

1.0.x

  • First release.

Usage

Basic Generator Example

generator that generate the datetime object at regular intervals.

>>> import datetimes
>>> from datetime import datetime, timedelta
>>> start = datetime(2000, 1, 1)
>>> interval = timedelta(days=1, hours=6)
>>> # start(datetime), interval(timedelta)
>>> gen = datetimes.generator(start, interval)
>>> gen.next()
datetime.datetime(2000, 1, 1, 0, 0)
>>> gen.next()
datetime.datetime(2000, 1, 2, 6, 0)
>>> gen.next()
datetime.datetime(2000, 1, 3, 12, 0)

Basic Range Example

generate list object that includes regularly generated datetime objects element.

>>> import datetimes
>>> from datetime import datetime, timedelta
>>> # start<datetime>, end<datetime>, interval<timedelta>
>>> datetimes.range(
...     datetime(2000, 1, 1),
...     datetime(2000, 1, 4),
...     timedelta(days=1, hours=6)
... )
[
 datetime.datetime(2000, 1, 1, 0, 0),
 datetime.datetime(2000, 1, 2, 6, 0),
 datetime.datetime(2000, 1, 3, 12, 0)
]

Range with callback Example

possible to specify the function to apply for each element.

>>> import datetimes
>>> from datetime import datetime, timedelta
>>> def callback(dt):
...     return dt.strftime('%Y-%m-%dT%H:%M:%S.%f')
...
>>> datetimes.range(
...     datetime(2000, 1, 1),
...     datetime(2000, 1, 4),
...     timedelta(days=1, hours=6),
...     callback=callback
... )
[
 '2000-01-01T00:00:00.000000',
 '2000-01-02T06:00:00.000000',
 '2000-01-03T12:00:00.000000'
]

noise arguments

possible to specify the gap between the actual time as noise parameters. allow to specify the noise parameters are "datetimes.generator" and "datetimes.range" functions. noise-arguments must be specified in the kwargs format. and not required. the available keys are same with timedelta-args. specifically, it is the following.

  • days
  • hours
  • minute
  • seconds
  • microseconds
>>> import datetimes
>>> from datetime import datetime, timedelta
>>> start = datetime(2000, 1, 1)
>>> delta = timedelta(days=1)
>>> # **noise(timedelta args)
>>> gen = datetimes.generator(start, delta, hours=2)
>>> gen.next()
# +1 hour noise
datetime.datetime(2000, 1, 1, 1, 0)
>>> gen.next()
# no gap(noise)
datetime.datetime(2000, 1, 2, 0, 0)
>>> gen.next()
# -2 hour noise
datetime.datetime(2000, 1, 2, 22, 0)

if you want to specify upper and lower limit, then specify the tuple object like (FROM, TO).

>>> import datetimes
>>> from datetime import datetime, timedelta
>>> start = datetime(2000, 1, 1)
>>> delta = timedelta(days=1)
>>> gen = datetimes.generator(start, delta, hours=(3, 5), minutes=(30, 59))
>>> gen.next()
# 3 hour noise, 44 minutes noise.
datetime.datetime(2000, 1, 1, 3, 44)
>>> gen.next()
# 5 hour noise, 56 minutes noise.
datetime.datetime(2000, 1, 2, 5, 56)
>>> gen.next()
# 4 hour noise, 38 minutes noise.
datetime.datetime(2000, 1, 3, 4, 38)

Choice Example

randomly generate a datetime object at a specified interval.

>>> import datetimes
>>> from datetime import datetime
>>> start = datetime(2000, 1, 1)
>>> end = datetime(2010, 12, 31)
>>> datetimes.choice(start, end)
datetime.datetime(2010, 1, 29, 22, 36, 27, 351477)
>>> datetimes.choice(start, end)
datetime.datetime(2009, 8, 26, 15, 47, 32, 524671)
>>> datetimes.choice(start, end)
datetime.datetime(2000, 10, 20, 21, 49, 58, 80108)