wdmmg.lib.times - Convert dates to persistable date range structures

Create datastructures for times and dates that should be used to save dates and times in the database, especially for wdmmg.model.Entries.times

Like most parts of our data models date and date ranges are persisted denormalized. We save both single dates and date ranges in a a special kind of date range datastructure that conains

  • the daterange as a single string
  • the start and the end date as
    • a datetime object
    • a string
    • year, month and day as seperate stings

This works like this:

>>> from wdmmg.lib.times import for_year
>>> for_year(2011)
{'from': {'day': '20110301',
          'month': '201103',
          'parsed': datetime.datetime(2011, 3, 1, 0, 0),
          'year': '2011'},
 'to': {'day': '20110331',
        'month': '201103',
        'parsed': datetime.datetime(2011, 3, 31, 0, 0),
        'year': '2011'},
 'unparsed': '2011-03'}
wdmmg.lib.times.timespan(unparsed, from_date, to_date)

Create a datastructure to save a span between tow datetimes in the database.

unparsed
The timespan as a string
from_date
The start date as a datetime
to_date
The end date as a datetime

returns: a dict representing the timespan that can be saved into the database

wdmmg.lib.times.for_year(year)

Create a timespan() datastructure for a year

year
A year as a str or int

returns: see timespan()

wdmmg.lib.times.for_datestrings(date1, date2=None)

Create a timespan() for a date or between a start and a end date, given as strings.

date1
start date as a string in a format that can be read by from_datestring()
date2
end date as a string in a format that can be read by from_datestring(), or None if start and end date are identical.

returns: see timespan()

Previous topic

wdmmg.lib.loader.Loader - a class to use in data loader

Next topic

wdmmg.lib.view - Database Views

This Page