Package parsedatetime :: Module parsedatetime :: Class Calendar
[hide private]
[frames] | no frames]

Class Calendar

source code

A collection of routines to input, parse and manipulate date and times. The text can either be 'normal' date values or it can be human readable.

Instance Methods [hide private]
object __init__(self, constants=None)
Default constructor for the Calendar class.
integer _convertUnitAsWords(self, unitText)
Converts text units into their number value
struct_time _buildTime(self, source, quantity, modifier, units)
Take quantity, modifier and unit strings and convert them into values.
struct_time parseDate(self, dateString)
Parse short-form date strings:
struct_time parseDateText(self, dateString)
Parse long-form date strings:
tuple evalRanges(self, datetimeString, sourceTime=None)
Evaluate the datetimeString text and determine if it represents a date or time range.
integer _CalculateDOWDelta(self, wd, wkdy, offset, style, currentDayStyle)
Based on the style and currentDayStyle determine what day-of-week value is to be returned.
tuple _evalModifier(self, modifier, chunk1, chunk2, sourceTime)
Evaluate the modifier string and following text (passed in as chunk1 and chunk2) and if they match any known modifiers calculate the delta and apply it to sourceTime.
tuple _evalModifier2(self, modifier, chunk1, chunk2, sourceTime)
Evaluate the modifier string and following text (passed in as chunk1 and chunk2) and if they match any known modifiers calculate the delta and apply it to sourceTime.
datetime _evalString(self, datetimeString, sourceTime=None)
Calculate the datetime based on flags set by the parse() routine
tuple parse(self, datetimeString, sourceTime=None)
Splits the given datetimeString into tokens, finds the regex patterns that match and then calculates a struct_time value from the chunks.
datetime inc(self, source, month=None, year=None)
Takes the given source date, or current date if none is passed, and increments it according to the values passed in by month and/or year.

Method Details [hide private]

__init__(self, constants=None)
(Constructor)

source code 
Default constructor for the Calendar class.
Parameters:
Returns: object
Calendar instance

_convertUnitAsWords(self, unitText)

source code 

Converts text units into their number value

Five = 5 Twenty Five = 25 Two hundred twenty five = 225 Two thousand and twenty five = 2025 Two thousand twenty five = 2025
Parameters:
  • unitText (string) - number text to convert
Returns: integer
numerical value of unitText

_buildTime(self, source, quantity, modifier, units)

source code 
Take quantity, modifier and unit strings and convert them into values. After converting, calcuate the time and return the adjusted sourceTime.
Parameters:
  • source (time) - time to use as the base (or source)
  • quantity (string) - quantity string
  • modifier (string) - how quantity and units modify the source time
  • units (string) - unit of the quantity (i.e. hours, days, months, etc)
Returns: struct_time
struct_time of the calculated time

parseDate(self, dateString)

source code 
Parse short-form date strings:
   '05/28/2006' or '04.21'
Parameters:
  • dateString (string) - text to convert to a datetime
Returns: struct_time
calculated struct_time value of dateString

parseDateText(self, dateString)

source code 
Parse long-form date strings:
   'May 31st, 2006'
   'Jan 1st'
   'July 2006'
Parameters:
  • dateString (string) - text to convert to a datetime
Returns: struct_time
calculated struct_time value of dateString

evalRanges(self, datetimeString, sourceTime=None)

source code 
Evaluate the datetimeString text and determine if it represents a date or time range.
Parameters:
  • datetimeString (string) - datetime text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of: start datetime, end datetime and the invalid flag

_CalculateDOWDelta(self, wd, wkdy, offset, style, currentDayStyle)

source code 
Based on the style and currentDayStyle determine what day-of-week value is to be returned.
Parameters:
  • wd (integer) - day-of-week value for the current day
  • wkdy (integer) - day-of-week value for the parsed day
  • offset (integer) - offset direction for any modifiers (-1, 0, 1)
  • style (integer) - normally the value set in Constants.DOWParseStyle
  • currentDayStyle (integer) - normally the value set in Constants.CurrentDOWParseStyle
Returns: integer
calculated day-of-week

_evalModifier(self, modifier, chunk1, chunk2, sourceTime)

source code 
Evaluate the modifier string and following text (passed in as chunk1 and chunk2) and if they match any known modifiers calculate the delta and apply it to sourceTime.
Parameters:
  • modifier (string) - modifier text to apply to sourceTime
  • chunk1 (string) - first text chunk that followed modifier (if any)
  • chunk2 (string) - second text chunk that followed modifier (if any)
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of: remaining text and the modified sourceTime

_evalModifier2(self, modifier, chunk1, chunk2, sourceTime)

source code 
Evaluate the modifier string and following text (passed in as chunk1 and chunk2) and if they match any known modifiers calculate the delta and apply it to sourceTime.
Parameters:
  • modifier (string) - modifier text to apply to sourceTime
  • chunk1 (string) - first text chunk that followed modifier (if any)
  • chunk2 (string) - second text chunk that followed modifier (if any)
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of: remaining text and the modified sourceTime

_evalString(self, datetimeString, sourceTime=None)

source code 

Calculate the datetime based on flags set by the parse() routine

Examples handled:
   RFC822, W3CDTF formatted dates
   HH:MM[:SS][ am/pm]
   MM/DD/YYYY
   DD MMMM YYYY
Parameters:
  • datetimeString (string) - text to try and parse as more "traditional" date/time text
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: datetime
calculated struct_time value or current struct_time if not parsed

parse(self, datetimeString, sourceTime=None)

source code 

Splits the given datetimeString into tokens, finds the regex patterns that match and then calculates a struct_time value from the chunks.

If sourceTime is given then the struct_time value will be calculated from that value, otherwise from the current date/time.

If the datetimeString is parsed and date/time value found then the second item of the returned tuple will be a flag to let you know what kind of struct_time value is being returned:
   0 = not parsed at all
   1 = parsed as a C{date}
   2 = parsed as a C{time}
   3 = parsed as a C{datetime}
Parameters:
  • datetimeString (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of: modified sourceTime and the result flag

inc(self, source, month=None, year=None)

source code 

Takes the given source date, or current date if none is passed, and increments it according to the values passed in by month and/or year.

This routine is needed because Python's timedelta() function does not allow for month or year increments.
Parameters:
  • source (struct_time) - struct_time value to increment
  • month (integer) - optional number of months to increment
  • year (integer) - optional number of years to increment
Returns: datetime
source incremented by the number of months and/or years