Skip to main content

A few conveniences to do with dates and times.

Project description

A few conveniences to do with dates and times.

Latest release 20250724:

  • unixtime2datetime: default tz now UTC.
  • Some doc updates.

There are some other PyPI modules providing richer date handling than the stdlib datetime module. This module mostly contains conveniences used in my other code; you're welcome to it, but it does not pretend to be large or complete.

Short summary:

  • datetime2unixtime: Convert a timezone aware datetime to a UNIX timestamp. WARNING: a naive datetime is assumed to be in UTC.
  • isodate: Return a date in ISO8601 YYYY-MM-DD format, or YYYYMMDD if not dashed.
  • localdate2unixtime: Convert a localtime date into a UNIX timestamp.
  • tzinfoHHMM: tzinfo class based on +HHMM / -HHMM strings.
  • unixtime2datetime: Convert a a UNIX timestamp to a datetime in the timezone tz. Note: the default timezone is UTC, not the local timezone.
  • UNIXTimeMixin: A mixin for classes with a .unixtime attribute, a float storing a UNIX timestamp.

Module contents:

  • datetime2unixtime(dt): Convert a timezone aware datetime to a UNIX timestamp. WARNING: a naive datetime is assumed to be in UTC.

  • isodate(when=None, dashed=True): Return a date in ISO8601 YYYY-MM-DD format, or YYYYMMDD if not dashed.

    Modern Pythons have a datetime.isoformat method, you should use that.

  • localdate2unixtime(d): Convert a localtime date into a UNIX timestamp.

  • class tzinfoHHMM(datetime.tzinfo): tzinfo class based on +HHMM / -HHMM strings.

  • unixtime2datetime(unixtime, *, tz: datetime.tzinfo = datetime.timezone.utc): Convert a a UNIX timestamp to a datetime in the timezone tz. Note: the default timezone is UTC, not the local timezone.

  • class UNIXTimeMixin: A mixin for classes with a .unixtime attribute, a float storing a UNIX timestamp.

UNIXTimeMixin.as_datetime(self, tz: datetime.tzinfo = datetime.timezone.utc): Return self.unixtime as a datetime with the timezone tz (default UTC).

UNIXTimeMixin.datetime: The unixtime as a UTC datetime.

Release Log

Release 20250724:

  • unixtime2datetime: default tz now UTC.
  • Some doc updates.

Release 20230210:

  • Drop Python 2 support.
  • Make timezones mandatory where previously they were assumed.

Release 20210306: Initial release, used by cs.sqltags.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs_dateutils-20250724.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs_dateutils-20250724-py2.py3-none-any.whl (3.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cs_dateutils-20250724.tar.gz.

File metadata

  • Download URL: cs_dateutils-20250724.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cs_dateutils-20250724.tar.gz
Algorithm Hash digest
SHA256 7b9ebdcdd03044aba4a55ec772c602447d8a7249a111d746f3b61e25b818a0a5
MD5 564a245e2290b3fb8f70098d56f2b36a
BLAKE2b-256 50091aeb4969c31c54452419e408d3607bc03e29df043449f30d8d099d649598

See more details on using hashes here.

File details

Details for the file cs_dateutils-20250724-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cs_dateutils-20250724-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b35fe82c6ff04418730b4795679c254f9744c58051cfcd3e942d410882d8bbb6
MD5 5fcca5ab353cfe42b2eaa5d47a1e83a3
BLAKE2b-256 565e420e5a7d0667386082e84b53f570b57644d4083942207309bc5e1e65d724

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page