xoutil.datetime - Basic date and time types

Extends the standard datetime module.

  • Python’s datetime.strftime doesn’t handle dates previous to 1900. This module define classes to override date and datetime to support the formatting of a date through its full proleptic Gregorian date range.

Based on code submitted to comp.lang.python by Andrew Dalke, copied from Django and generalized.

You may use this module as a drop-in replacement of the standard library datetime module.

xoutil.datetime.new_date()[source]

Generate a safe date from a legacy datetime date object.

xoutil.datetime.new_datetime()[source]

Generate a safe “datetime” from a “datetime.date” or “datetime.datetime” object.

xoutil.datetime.strfdelta()[source]

Format a timedelta using a smart pretty algorithm.

Only two levels of values will be printed.

>>> def t(h, m):
...     return timedelta(hours=h, minutes=m)

>>> strfdelta(t(4, 56)) == '4h 56m'
True
xoutil.datetime.strftime()[source]
xoutil.datetime.get_month_first()[source]

Given a reference date, returns the first date of the same month. If ref is not given, then uses current date as the reference.

xoutil.datetime.get_month_last()[source]

Given a reference date, returns the last date of the same month. If ref is not given, then uses current date as the reference.

xoutil.datetime.is_full_month()[source]

Returns true if the arguments comprises a whole month.

xoutil.datetime.daterange([start, ]stop[, step])[source]

Returns an iterator that yields each date in the range of [start, stop), not including the stop.

If start is given, it must be a date (or datetime) value; and in this case only stop may be an integer meaning the numbers of days to look ahead (or back if stop is negative).

If only stop is given, start will be the first day of stop’s month.

step, if given, should be a non-zero integer meaning the numbers of days to jump from one date to the next. It defaults to 1. If it’s positive then stop should happen after start, otherwise no dates will be yielded. If it’s negative stop should be before start.

As with range, stop is never included in the yielded dates.