Last page update: November 23, 2011
Classes:
External methods:
Complete reference:
These methods don’t belong to any class.
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
Compare two TimeIntervals
It guarantees total order (while the other TimeInterval’s comparison operators do not)
The duration of this TimeInterval in minutes from the start
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 |
|---|
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')]
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 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 |
|---|
Returns the intervals in this TimePeriod
Remove a TimeInterval or a TimePeriod.
Overlapping intervals will be adjusted
| Parameters: | item – TimeInterval or TimePeriod |
|---|
TODO
| Parameters: | d – the date |
|---|
Return an iterator over a range of dates.
It works like the range() builtin, so it will return [dstart,dstart+1,...,dstop-1]
| Parameters: |
|
|---|
>>> 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
TODO
| Parameters: |
|
|---|
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: |
|---|
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'
Parse a string representing a date or a period. Return datetime.date or tuple(year,month) or None
| Parameters: |
|
|---|
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
this week, next week, last week (can be translated in all languages)
the name of a quarter: e.g. Q1 or 1st quarter
an iso date: e.g. 2008-04-28
various separators are admitted: 28-4-08, 28/4/08, 28 4 08
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)
Return datetime.date of the last day of the month. If date is given, then year and month are readed from date.
| Parameters: |
|
|---|
Return datetime.date of the first day of the month. If date is given, then year and month are readed from date.
| Parameters: |
|
|---|
Convert two dates to a string in the specified locale that decodeDatePeriod() will understand
| Parameters: |
|
|---|
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
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
Convert a date or datetime to a date. Return it
| Parameters: | date_or_datetime – the date or datetime to convert in a date |
|---|
Convert a time, datetime or a string (HH:MM:SS or HH:MM) to a time.
| Parameters: | t – the time to convert |
|---|
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 |
|---|