Skip to main content

Easily create time periods from timestamps

Project description

https://travis-ci.org/gaqzi/datetime_periods.png

This module aims to help you create time periods from timestamps.

Pass in a datetime.datetime() object and a period name and it’ll return the beginning and end of that period.

Documentation available on Read the Docs.

Installation:

You can install from PyPi!

pip install datetime_periods

period usage

Pass in a datetime.datetime() object and a period name and it’ll return the beginning and end of that period.

>>> from datetime_periods import period
>>> period(datetime(2012, 4, 2, second=12), 'minute')
[datetime(2012, 4, 2), datetime(2012, 4, 2, 0, 0, 59)]
>>> period(datetime(2012, 4, 2), 'hour')
[datetime(2012, 4, 2, 0), datetime(2012, 4, 2, 0, 59, 59)]
>>> period(datetime(2012, 4, 2), 'day')
[datetime(2012, 4, 2), datetime(2012, 4, 2, 23, 59, 59)]
>>> period(datetime(2012, 4, 2), 'week')
[datetime(2012, 4, 2), datetime(2012, 4, 8, 23, 59, 59)]
>>> period(datetime(2012, 4, 15), 'month')
[datetime(2012, 4, 1), datetime(2012, 4, 30, 23, 59, 59)]
>>> period(datetime(2012, 4, 2), 'quarter')
[datetime(2012, 4, 1), datetime(2012, 6, 30, 23, 59, 59)]
>>> period(datetime(2012, 9, 1), 'half_year')
[datetime(2012, 7, 1), datetime(2012, 12, 31, 23, 59, 59)]
>>> period(datetime(2012, 7, 1), 'year')
[datetime(2012, 1, 1), datetime(2012, 12, 31, 23, 59, 59)]

TimeRange usage

The TimeRange class takes two times, start and stop, and creates datetime objects from them that is smart about when a date should roll over to the following day.

This class can also act like a 2 length list where index 0=start, 1=stop time. This to allow the class to be used for argument expansion and as an iterator.

>>> from datetime_periods import TimeRange
>>> tr = TimeRange('17:00', '23:00', '2013-12-25')
>>> tr.start
datetime(2013, 12, 25, 17)
>>> tr.stop
datetime(2013, 12, 25, 23)
>>> tr = TimeRange('17:00', '04:00', '2013-12-25')
>>> tr.start
datetime(2013, 12, 25, 17)
>>> tr.stop
datetime(2013, 12, 26, 4)
>>> tr[0] == tr.start
True
>>> tr[1] == tr.stop
True

Sugar

The sugar module has sugar functions for all variants available.

Sugar functions for entire period:

  • period_second

  • period_minute

  • period_hour

  • period_day

  • period_week

  • period_month

  • period_quarter

  • period_half_year

  • period_year

Sugar functions for beginning of period:

  • period_beginning_second

  • period_beginning_minute

  • period_beginning_hour

  • period_beginning_day

  • period_beginning_week

  • period_beginning_month

  • period_beginning_quarter

  • period_beginning_half_year

  • period_beginning_year

Sugar functions for end of period:

  • period_end_second

  • period_end_minute

  • period_end_hour

  • period_end_day

  • period_end_week

  • period_end_month

  • period_end_quarter

  • period_end_half_year

  • period_end_year

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datetime_periods-1.1.1.tar.gz (5.0 kB view details)

Uploaded Source

File details

Details for the file datetime_periods-1.1.1.tar.gz.

File metadata

File hashes

Hashes for datetime_periods-1.1.1.tar.gz
Algorithm Hash digest
SHA256 4e22beb8ea176f398cddd5f18520a907ff85cb161690f81c8c9d7839fcfbdf0e
MD5 7a2840ab6122c1d4cdc9db16c3cada71
BLAKE2b-256 a32e873f967d231b74a0a2b88b4f0fd1d1e8d294e16c44790ccb7de412f75528

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page