Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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.

Files for datetime_periods, version 1.1.1
Filename, size File type Python version Upload date Hashes
Filename, size datetime_periods-1.1.1.tar.gz (5.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page