Date/Time Utils

TimeZoneHelper

class djem.utils.dt.TimeZoneHelper(tz)[source]

A simple helper class that provides shortcuts for getting the current date and the current datetime for a known local timezone.

tz should be a valid timezone name string (as accepted by the pytz.timezone function) or a pytz tzinfo instance (as returned by the pytz.timezone function).

tz

The pytz tzinfo instance representing the timezone used by this TimeZoneHelper instance.

name

The name of the timezone represented by this TimeZoneHelper instance, as a string. Equivalent to tz.zone, where tz is the instance's tz attribute.

now()[source]

Return the current datetime in the local timezone.

today()[source]

Return the current date in the local timezone.

Warning

Be careful when dealing with local times. Django recommends you “use UTC in the code and use local time only when interacting with end users”, with the conversion from UTC to local time usually only being performed in templates. And the pytz documentation notes “The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans”. See the Django timezone documentation and the pytz documentation.