diff options
author | demigod <devnull@localhost> | 2009-07-31 04:03:07 +0300 |
---|---|---|
committer | demigod <devnull@localhost> | 2009-07-31 04:03:07 +0300 |
commit | 18595f205c0f6d5b99510eba983e9437c350bee1 (patch) | |
tree | 1063cfbadefc235143322d06a91b3691bcb42a91 /src/apscheduler/util.py | |
parent | 069b0bcd20b9badc25bf96512c6da8ac78f7b5aa (diff) | |
download | apscheduler-18595f205c0f6d5b99510eba983e9437c350bee1.tar.gz |
Miscellaneous fixes and improvements towards the 1.0 release
Diffstat (limited to 'src/apscheduler/util.py')
-rw-r--r-- | src/apscheduler/util.py | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/src/apscheduler/util.py b/src/apscheduler/util.py index 9f845a4..d54db86 100644 --- a/src/apscheduler/util.py +++ b/src/apscheduler/util.py @@ -1,13 +1,14 @@ -from time import mktime +from datetime import date, datetime, timedelta from calendar import monthrange, weekday -__all__ = ('min_values', 'max_values', 'asint', 'get_actual_maximum', - 'get_date_field', 'timedelta_seconds', 'time_difference') +__all__ = ('MIN_VALUES', 'MAX_VALUES', 'asint', 'get_actual_maximum', + 'get_date_field', 'convert_to_datetime', 'timedelta_seconds', + 'time_difference', 'datetime_ceil') -min_values = {'year': 1970, 'month': 1, 'day': 1, 'day_of_week': 0, +MIN_VALUES = {'year': 1970, 'month': 1, 'day': 1, 'day_of_week': 0, 'hour': 0, 'minute': 0, 'second': 0} -max_values = {'year': 2 ** 63, 'month': 12, 'day:': 31, 'day_of_week': 6, +MAX_VALUES = {'year': 2 ** 63, 'month': 12, 'day:': 31, 'day_of_week': 6, 'hour': 23, 'minute': 59, 'second': 59} @@ -15,6 +16,7 @@ def asint(text): """ Safely converts a string to an integer, returning None if the string is None. + @type text: str @rtype: int """ @@ -22,48 +24,81 @@ def asint(text): return int(text) -def get_actual_maximum(date, fieldname): +def get_actual_maximum(dateval, fieldname): """ Retrieves the maximum applicable value for the given datetime field. - @type date: datetime + + @type dateval: datetime @type fieldname: str @rtype: int """ if fieldname == 'day': - return monthrange(date.year, date.month)[1] - return max_values[fieldname] + return monthrange(dateval.year, dateval.month)[1] + return MAX_VALUES[fieldname] -def get_date_field(date, fieldname): +def get_date_field(dateval, fieldname): """ Extracts the value of the specified field from a datetime object. - @type date: datetime + + @type dateval: datetime @type fieldname: str @rtype: int """ - if fieldname == 'day_of_week': - return weekday(date.year, date.month, date.day) - return getattr(date, fieldname) + return weekday(dateval.year, dateval.month, dateval.day) + return getattr(dateval, fieldname) + + +def convert_to_datetime(dateval): + """ + Converts a date object to a datetime object. + If an actual datetime object is passed, it is returned unmodified. + + @type dateval: date + @rtype: datetime + """ + if isinstance(dateval, datetime): + return dateval + elif isinstance(dateval, date): + return datetime.fromordinal(dateval.toordinal()) + raise TypeError('Expected date, got %s instead' % type(dateval)) def timedelta_seconds(delta): """ Converts the given timedelta to seconds. + @type delta: timedelta + @rtype: float """ return delta.days * 24 * 60 * 60 + delta.seconds + \ - delta.microseconds / 100000.0 + delta.microseconds / 1000000.0 def time_difference(date1, date2): """ Returns the time difference in seconds between the given two datetime objects. The difference is calculated as: date1 - date2. + @param date1: the later datetime @type date1: datetime @param date2: the earlier datetime @type date2: datetime + @rtype: float """ - - return mktime(date1.timetuple()) - mktime(date2.timetuple()) + if date1 >= date2: + return timedelta_seconds(date1 - date2) + return -timedelta_seconds(date2 - date1) + + +def datetime_ceil(dateval): + """ + Rounds the given datetime object upwards. + + @type dateval: datetime + """ + if dateval.microsecond > 0: + return dateval + timedelta(seconds=1, + microseconds=-dateval.microsecond) + return dateval |