Package 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]
integer
_CalculateDOWDelta(self, wd, wkdy, offset, style, currentDayStyle)
Based on the style and currentDayStyle determine what day-of-week value is to be returned.
source code
 
_UnitsTrapped(self, s, m, key) source code
object
__init__(self, constants=None)
Default constructor for the Calendar class.
source code
struct_time
_buildTime(self, source, quantity, modifier, units)
Take quantity, modifier and unit strings and convert them into values.
source code
integer
_convertUnitAsWords(self, unitText)
Converts text units into their number value
source code
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.
source code
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.
source code
datetime
_evalString(self, datetimeString, sourceTime=None)
Calculate the datetime based on flags set by the parse() routine
source code
int
_quantityToInt(self, quantity)
Convert a quantity, either spelled-out or numeric, to an integer
source code
tuple
evalRanges(self, datetimeString, sourceTime=None)
Evaluate the datetimeString text and determine if it represents a date or time range.
source code
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.
source code
tuple or None
nlp(self, inputString, sourceTime=None)
Utilizes parse() after making judgements about what datetime information belongs together.
source code
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.
source code
tuple
parseDT(self, datetimeString, sourceTime=None, tzinfo=None)
datetimeString is as .parse, sourceTime has the same semantic meaning as .parse, but now also accepts datetime objects.
source code
struct_time
parseDate(self, dateString, sourceTime=None)
Parse short-form date strings:
source code
struct_time
parseDateText(self, dateString, sourceTime=None)
Parse long-form date strings:
source code
Method Details [hide private]

_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

__init__(self, constants=None)
(Constructor)

source code 

Default constructor for the Calendar class.

Parameters:
  • constants (object) - Instance of the class Constants
Returns: object
Calendar instance

_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

_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

_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

_quantityToInt(self, quantity)

source code 

Convert a quantity, either spelled-out or numeric, to an integer

Parameters:
  • quantity (string) - quantity to parse to int
Returns: int
the quantity as an integer, defaulting to 0

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

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

nlp(self, inputString, sourceTime=None)

source code 

Utilizes parse() after making judgements about what datetime information belongs together.

It makes logical groupings based on proximity and returns a parsed datetime for each matched grouping of datetime text, along with location info within the given inputString.

Parameters:
  • inputString (string) - natural language text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple or None
tuple of tuples in the format (parsed_datetime as datetime.datetime, flags as int, start_pos as int, end_pos as int, matched_text as string) or None if there were no matches

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

parseDT(self, datetimeString, sourceTime=None, tzinfo=None)

source code 

datetimeString is as .parse, sourceTime has the same semantic meaning as .parse, but now also accepts datetime objects. tzinfo accepts a tzinfo object. It is advisable to use pytz.

Parameters:
  • datetimeString (string) - date/time text to evaluate
  • sourceTime (struct_time, datetime, date, time) - time value to use as the base
  • tzinfo (tzinfo) - Timezone to apply to generated datetime objs.
Returns: tuple
tuple of datetime object and an int of the return code

see .parse for return code details.

parseDate(self, dateString, sourceTime=None)

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, sourceTime=None)

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