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

Class Calendar

source code

object --+
         |
        Calendar

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, version=1)
Default constructor for the Calendar class.
source code
 
context(*args, **kwds) source code
integer
_convertUnitAsWords(self, unitText)
Converts text units into their number value.
source code
struct_time
_buildTime(self, source, quantity, modifier, units)
Take quantity, modifier and unit strings and convert them into values.
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
tuple
evalRanges(self, datetimeString, sourceTime=None)
Evaluate the datetimeString text and determine if it represents a date or time range.
source code
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
int
_quantityToReal(self, quantity)
Convert a quantity, either spelled-out or numeric, to a float
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
datetime
_evalDT(self, datetimeString, sourceTime)
Calculate the datetime from known format like RFC822 or W3CDTF
source code
 
_evalUnits(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseUnits()
source code
 
_evalQUnits(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseQUnits()
source code
 
_evalDateStr(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseDateStr()
source code
 
_evalDateStd(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseDateStd()
source code
 
_evalDayStr(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseDaystr()
source code
 
_evalWeekday(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseWeekday()
source code
 
_evalTimeStr(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseTimeStr()
source code
 
_evalMeridian(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseMeridian()
source code
 
_evalTimeStd(self, datetimeString, sourceTime)
Evaluate text passed by _partialParseTimeStd()
source code
 
_UnitsTrapped(self, s, m, key) source code
tuple
_partialParseModifier(self, s, sourceTime)
test if giving s matched CRE_MODIFIER, used by parse()
source code
tuple
_partialParseUnits(self, s, sourceTime)
test if giving s matched CRE_UNITS, used by parse()
source code
tuple
_partialParseQUnits(self, s, sourceTime)
test if giving s matched CRE_QUNITS, used by parse()
source code
tuple
_partialParseDateStr(self, s, sourceTime)
test if giving s matched CRE_DATE3, used by parse()
source code
tuple
_partialParseDateStd(self, s, sourceTime)
test if giving s matched CRE_DATE, used by parse()
source code
tuple
_partialParseDayStr(self, s, sourceTime)
test if giving s matched CRE_DAY, used by parse()
source code
tuple
_partialParseWeekday(self, s, sourceTime)
test if giving s matched CRE_WEEKDAY, used by parse()
source code
tuple
_partialParseTimeStr(self, s, sourceTime)
test if giving s matched CRE_TIME, used by parse()
source code
tuple
_partialParseMeridian(self, s, sourceTime)
test if giving s matched CRE_TIMEHMS2, used by parse()
source code
tuple
_partialParseTimeStd(self, s, sourceTime)
test if giving s matched CRE_TIMEHMS, used by parse()
source code
tuple
parseDT(self, datetimeString, sourceTime=None, tzinfo=None, version=None)
datetimeString is as .parse, sourceTime has the same semantic meaning as .parse, but now also accepts datetime objects.
source code
tuple
parse(self, datetimeString, sourceTime=None, version=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
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, version=None)
Utilizes parse() after making judgements about what datetime information belongs together.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]
  currentContext

Inherited from object: __class__

Method Details [hide private]

__init__(self, constants=None, version=1)
(Constructor)

source code 

Default constructor for the Calendar class.

Parameters:
Returns: object
Calendar instance
Overrides: object.__init__

context(*args, **kwds)

source code 
Decorators:
  • @contextlib.contextmanager

_convertUnitAsWords(self, unitText)

source code 

Converts text units into their number value.

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

source code 

Parse short-form date strings:

   '05/28/2006' or '04.21'
Parameters:
  • dateString (string) - text to convert to a datetime
  • sourceTime (struct_time) - struct_time value to use as the base
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
  • sourceTime (struct_time) - struct_time value to use as the base
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

_quantityToReal(self, quantity)

source code 

Convert a quantity, either spelled-out or numeric, to a float

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

_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) - text chunk that preceded modifier (if any)
  • chunk2 (string) - 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

_evalDT(self, datetimeString, sourceTime)

source code 

Calculate the datetime from known format like RFC822 or W3CDTF

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

_partialParseModifier(self, s, sourceTime)

source code 

test if giving s matched CRE_MODIFIER, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseUnits(self, s, sourceTime)

source code 

test if giving s matched CRE_UNITS, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseQUnits(self, s, sourceTime)

source code 

test if giving s matched CRE_QUNITS, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseDateStr(self, s, sourceTime)

source code 

test if giving s matched CRE_DATE3, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseDateStd(self, s, sourceTime)

source code 

test if giving s matched CRE_DATE, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseDayStr(self, s, sourceTime)

source code 

test if giving s matched CRE_DAY, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseWeekday(self, s, sourceTime)

source code 

test if giving s matched CRE_WEEKDAY, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseTimeStr(self, s, sourceTime)

source code 

test if giving s matched CRE_TIME, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseMeridian(self, s, sourceTime)

source code 

test if giving s matched CRE_TIMEHMS2, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

_partialParseTimeStd(self, s, sourceTime)

source code 

test if giving s matched CRE_TIMEHMS, used by parse()

Parameters:
  • s (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
Returns: tuple
tuple of remained date/time text, datetime object and an boolean value to describ if matched or not

parseDT(self, datetimeString, sourceTime=None, tzinfo=None, version=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.
  • version (integer) - style version, default will use Calendar parameter version value
Returns: tuple
tuple of: modified sourceTime and the result flag/context

see .parse for return code details.

parse(self, datetimeString, sourceTime=None, version=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:

   If C{version} equals to L{VERSION_FLAG_STYLE}, the second item of
   the returned tuple will be a flag to let you know what kind of
   C{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}

   If C{version} equals to L{VERSION_CONTEXT_STYLE}, the second value
   will be an instance of L{pdtContext}
Parameters:
  • datetimeString (string) - date/time text to evaluate
  • sourceTime (struct_time) - struct_time value to use as the base
  • version (integer) - style version, default will use Calendar parameter version value
Returns: tuple
tuple of: modified sourceTime and the result flag/context

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 (float or integer) - optional number of months to increment
  • year (float or integer) - optional number of years to increment
Returns: datetime
source incremented by the number of months and/or years

nlp(self, inputString, sourceTime=None, version=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
  • version (integer) - style version, default will use Calendar parameter version value
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

Property Details [hide private]

currentContext

Get Method:
unreachable.currentContext(self)