rattail.time

Time Utilities

rattail.time.date_range(start, end, step=1)[source]

Generator which yields all dates between start and end, inclusive.

rattail.time.first_of_month(date)[source]

Returns a date representing the first day of whichever month date falls in.

rattail.time.first_of_year(date)[source]

Returns a date representing the first day of whichever year date falls in.

rattail.time.get_monday(date)[source]

Return a datetime.date instance corresponding to Monday of the given week, according to the date parameter. Note that this assumes the week begins on Monday, so if a Sunday is passed then the previous Monday will be returned.

rattail.time.get_sunday(date)[source]

Return a datetime.date instance corresponding to Sunday of the given week, according to the date parameter.

rattail.time.last_of_month(date)[source]

Returns a date representing the last day of whichever month date falls in.

rattail.time.localtime(config, time=None, key='default', from_utc=False, tzinfo=True)[source]

Return a datetime which has been localized to a particular timezone. The timezone() function will be used to obtain the timezone to which the time value will be localized.

Parameters:
  • config – Reference to a config object.

  • time – Optional datetime.datetime instance to be localized. If not provided, the current time (“now”) is assumed.

  • tzinfo – Boolean indicating whether the result should contain tzinfo or not, i.e. whether it should be time zone “aware” (True) or “naive” (False).

  • key – Config key to be used in determining to which timezone the time should be localized.

rattail.time.make_utc(time=None, tzinfo=False)[source]

Convert a timezone-aware time back to a naive UTC equivalent. If no time is specified, the current time is assumed.

rattail.time.months_ago(date, months)[source]

Returns the “equivalent” day from a previous month. This refers to the day number within the month, so e.g. if date is 2021-02-13 and months is 3 then it should return 2020-11-13.

Note that this is not always strictly possible, for instance if date is 2021-03-31 and months is 1 then it “should” return 2021-02-31 which is of course not valid. So in this case it will find the “greatest” day number which is valid, and return that, e.g. 2021-02-28.

rattail.time.next_month(date)[source]

Returns a date object for the first day of “next” month, where the “current” month is determined by date param.

rattail.time.previous_month(date, months=1)[source]

Returns the first day of the month which is a given number of months previous to the “current” month, as determined by date.

rattail.time.timezone(config, key='default')[source]

Return a timezone object based on the definition found in config.

Parameters:
  • config – Reference to a config object.

  • key – Config key used to determine which timezone should be returned.

Returns:

A pytz.tzinfo instance, created using the Olson time zone name found in the config file.

An example of the configuration syntax which is assumed by this function:

[rattail]

# retrieve with: timezone(config)
timezone.default = America/Los_Angeles

# retrieve with: timezone(config, 'headoffice')
timezone.headoffice = America/Chicago

# retrieve with: timezone(config, 'satellite')
timezone.satellite = America/New_York

See Wikipedia for the full list of Olson time zone names.