1.1.2.5. gnr.core.gnrdate – Utilities for date and time management

Last page update: November 23, 2011

Classes:

External methods:

Complete reference:

1.1.2.5.1. TimeInterval

1.1.2.5.2. TimePeriod

1.1.2.5.4. gnr.core.gnrdate - The complete reference list

class gnr.core.gnrdate.TimeInterval(start=None, stop=None, minutes=None)

A span of time (start, end).

You can create TimeIntervals from datetime.time objects or strings:

>>> from datetime import time
>>> TimeInterval(time(8,30),time(10,30))
TimeInterval('8:30-10:30')
>>> TimeInterval('8:30-10:30')
TimeInterval('8:30-10:30')
>>> start = time(8,30)
>>> stop = time(10,30)
>>> tup = (start,stop)
>>> TimeInterval(start,stop) == TimeInterval( tup )
True
>>> TimeInterval((start, '10:30'))
TimeInterval('8:30-10:30')

You can also create them supplying their length and their start or stop time:

>>> TimeInterval(start='8:30',minutes=60)
TimeInterval('8:30-9:30')
>>> TimeInterval(stop='9:30',minutes=60)
TimeInterval('8:30-9:30')

As you can see, str() and repr() are both implemented in a sensible way.

Several operators are available:

Operator Meaning
a < b a ends before b starts
a <= b a starts before or when b starts
a > b b ends before a starts
a >= b b starts before or when a ends
a == b a.start == b.start and a.stop == b.stop
a in b a overlaps b

All of these operators accept as their second parameter a TimeInterval or a string (or a tuple of datetime.time or strings)

>>> a = TimeInterval('8:30-10:30')
>>> b = TimeInterval('10:00-12:00')
>>> a < b
False
>>> a <= b
True
>>> a == b
False
>>> a == a
True
>>> a in b
True
static cmp(one, other)

Compare two TimeIntervals

It guarantees total order (while the other TimeInterval’s comparison operators do not)

minutes

The duration of this TimeInterval in minutes from the start

overlaps(other)

Checks if other attribute overlaps with this interval

Returns a constant describing the relationship between self and other: TimeInterval.xxx where xxx is NO_OVERLAP, FULLY_CONTAINED, FULLY_CONTAINS, COVER_RIGHT or COVER_LEFT

Parameters:other – a TimeInterval or a string represting it
static sorted(iterable)

Sort TimeIntervals.

Guarantees total order.

>>> ti = TimeInterval('9:00-10:00')
>>> tp = TimePeriod('8:00-12:00')
>>> tp.remove(ti)
>>> tp
TimePeriod('8:00-9:00, 10:00-12:00')
>>> lst = [ti] + tp.intervals
>>> TimeInterval.sorted(lst)
[TimeInterval('8:00-9:00'), TimeInterval('9:00-10:00'), TimeInterval('10:00-12:00')]
class gnr.core.gnrdate.TimePeriod(*intervals)

A non-overlapping set of TimeIntervals.

You can create a TimePeriod by calling the constructor with:

  • zero or more TimeIntervals, or their string representations;
  • a comma separated string of TimeInterval’s string representations.

You can add or remove intervals to a TimePeriod.

>>> p = TimePeriod('8:00-12:00','16:00-20:00')
>>> for i in ('8:00-9:00','9:30-10:00','10:00-11:30','16:00-16:30','17:00-18:00','18:00-19:00','19:00-20:00'): p.remove(i)
>>> str(p)
'9:00-9:30, 11:30-12:00, 16:30-17:00'

If you attach custom attributes to your TimeIntervals (or if you derive your own subclasses), they are preserved when intervals are sliced:

>>> iv1 = TimeInterval('8:00-12:00')
>>> iv1.name = 'morning'
>>> iv2 = TimeInterval('16:00-20:00')
>>> iv2.name = 'afternoon'
>>> p = TimePeriod(iv1,iv2)
>>> for i in ('8:00-9:00','9:30-10:00','10:00-11:30','16:00-16:30','17:00-18:00','18:00-19:00','19:00-20:00'): p.remove(i)
>>> str(p)
'9:00-9:30, 11:30-12:00, 16:30-17:00'
>>> [i.name for i in p.intervals]
['morning', 'morning', 'afternoon']

TimePeriod also supports len(), iter() and getitem operations

add(item)

Add the new TimeInterval or a TimePeriod.

If it overlaps with any existing interval in this TimePeriod, they’ll be merged

Parameters:item – TimeInterval or TimePeriod or string
intervals

Returns the intervals in this TimePeriod

remove(item)

Remove a TimeInterval or a TimePeriod.

Overlapping intervals will be adjusted

Parameters:item – TimeInterval or TimePeriod
gnr.core.gnrdate.dateLastYear(d)

TODO

Parameters:d – the date
gnr.core.gnrdate.dateRange(dstart, dstop)

Return an iterator over a range of dates.

It works like the range() builtin, so it will return [dstart,dstart+1,...,dstop-1]

Parameters:
  • dstart – the start date
  • dstop – the end date
>>> a = datetime.date(year=2010,month=10,day=8)
>>> b = datetime.date(year=2010,month=10,day=12)
>>> for date in dateRange(a,b):
...     print date
... 
2010-10-08
2010-10-09
2010-10-10
2010-10-11
gnr.core.gnrdate.dayIterator(period, wkdlist=None, locale=None, workdate=None, asDate=True)

TODO

Parameters:
  • period – TODO
  • wkdlist – TODO
  • locale – the current locale (e.g: en, en_us, it)
  • workdate – the workdate
  • asDate – boolean. TODO
gnr.core.gnrdate.decodeDatePeriod(datestr, workdate=None, locale=None, returnDate=False, dtype='D')

Parse a string representing a date or a period and returns a string of one or two dates in iso format separated by ;. See doc of decodeOneDate() for details on possible formats of a single date

Parameters:
  • datestr – the string representing a date or a period
  • workdate – the workdate
  • locale – the current locale (e.g: en, en_us, it)
  • returnDate – boolean. If True, return the following tuple: (dateStart, dateEnd)
  • dtype – the dtype

The input datestr can be:

  • two dates separated by ;: e.g. 22 4, 2008;28 4, 2008

  • two dates separated by `` to ``

  • two dates in form from date to date (can be translated and supports synonimous, e.g. between date and date)

    • if a period is given as starting date, the start date of period is kept
    • if a period is given as end date, the end date of period is kept
    • if no year is specified, the year is relative to workdate, keeping all periods in the past (e.g. if working date is 2008-04-28, december is interpreted as december 2007)
    • if a year is specified for the end date (or period) a relative year is calculated for the starting period (e.g. from december to march 06: returns '2005-12-01;2006-03-31')
  • a starting date in form from date, if date is a period starting date is keep: e.g. april returns '2008-04-01;'

  • an end date in form to date, if date is a period end date is keep: e.g. april returns ';2008-04-30'

  • a single expression representing a period: e.g. 2007 returns '2007-01-01;2007-12-31'

  • a single expression representing a single date: e.g. today returns '2008-04-28'

gnr.core.gnrdate.decodeOneDate(datestr, workdate=None, months=None, days=None, quarters=None, locale=None, isEndPeriod=False)

Parse a string representing a date or a period. Return datetime.date or tuple(year,month) or None

Parameters:
  • datestr – the string to be interpreted
  • workdate – the workdate
  • months – names of months according to locale (just for caching)
  • days – names of weekdays according to locale (just for caching)
  • quarters – names of quarters according to locale (just for caching)
  • locale – the current locale (e.g: en, en_us, it)
  • isEndPeriod – if the string represents a period, return the end date (default return the start date)

Special keywords like today or the name of a month can be translated in all languages and support synonimous. (e.g: this month; e.g: month)

The input string can be:

  • a year: e.g. 2007 or 07

  • today, yesterday, tomorrow (can be translated in all languages)
    • you can specify a number of days to add to today: e.g. ‘today + 3’ or ‘today - 15’
  • this week, next week, last week (can be translated in all languages)

  • this month, next month, last month (can be translated in all languages )
    • can be specified a number of months to add to current month: e.g. ‘this month + 3’ or ‘this month - 24’
  • the name of a quarter: e.g. Q1 or 1st quarter

  • the name of a month: e.g. april or apr
    • can be specified a year after the month: e.g. apr 07 or april 2007
    • returns a tuple (year, month): if year is not specified in datestr, year is returned None
  • the name of a weekday: e.g. monday or mon
    • the date returned is the date of the given weekday in this week (relative to workdate)
  • an iso date: e.g. 2008-04-28

  • a date formatted according to locale (see babel doc): e.g. 4 28, 2008 (en_us) or 28-4-08 (it)

    various separators are admitted: 28-4-08, 28/4/08, 28 4 08

gnr.core.gnrdate.minutes_to_time(mins)

Returns a datetime.time given the number of minutes since midnight

Parameters:mins – int - number of minutes
>>> minutes_to_time(480)
datetime.time(8, 0)
gnr.core.gnrdate.monthEnd(year=None, month=None, date=None)

Return datetime.date of the last day of the month. If date is given, then year and month are readed from date.

Parameters:
  • year – The year you specify
  • month – The month you specify
  • date – TODO
gnr.core.gnrdate.monthStart(year=None, month=None, date=None)

Return datetime.date of the first day of the month. If date is given, then year and month are readed from date.

Parameters:
  • year – The year you specify
  • month – The month you specify
  • date – TODO
gnr.core.gnrdate.periodCaption(dateFrom=None, dateTo=None, locale=None)

Convert two dates to a string in the specified locale that decodeDatePeriod() will understand

Parameters:
  • dateFrom – the beginning period date
  • dateTo – the ending period date
  • locale – the current locale (e.g: en, en_us, it)
gnr.core.gnrdate.seconds_to_text(seconds)

Convert number of seconds to a string

>>> print seconds_to_text(0)
0s
>>> print seconds_to_text(10)
10s
>>> print seconds_to_text(60)
1m
>>> print seconds_to_text(3600)
1h
>>> print seconds_to_text(3672)
1h 1m 12s
gnr.core.gnrdate.time_to_minutes(t)

Return the total minutes from midnight to the given datetime.datetime object

Parameters:t – the datetime.datetime object
>>> from gnr.core.gnrdate import time_to_minutes as t
>>> import datetime
>>> now = datetime.datetime(2011, 10, 8, 15, 26, 32)
>>> t(now)
926
gnr.core.gnrdate.toDate(date_or_datetime)

Convert a date or datetime to a date. Return it

Parameters:date_or_datetime – the date or datetime to convert in a date
gnr.core.gnrdate.toTime(t)

Convert a time, datetime or a string (HH:MM:SS or HH:MM) to a time.

Parameters:t – the time to convert
gnr.core.gnrdate.yearDecode(datestr)

returns the year number as an int from a string of 2 or 4 digits: if 2 digits is given century is added

Parameters:datestr – the string including year