Skip to main content

django-unixtimestampfield

https://img.shields.io/travis/myyang/django-unixtimestampfield.svg https://img.shields.io/pypi/v/django-unixtimestampfield.svg https://coveralls.io/repos/myyang/django-unixtimestampfield/badge.svg?service=github

Provide a custom field that is stored as float (UTC POSIX timestamp) and used as datetime instance.

Requirements and Compatibility

Database that supports Float type is compatible.

Currently tested with metrics:

Django/Python

3.5

3.6

3.7

3.8

3.9

3.10

3.11

3.12

2.2.x

v

v

v

3.0.x

v

v

v

v

4.0.x

v

v

v

v

v

5.0.x

v

v

v

v

  • Note: for Python2 and Django1.X, please use v0.3.9 or previous version.

Install

pip install django-unixtimestampfield

Usage

Used in model as following:

from django.db import models

from unixtimestampfield.fields import UnixTimeStampField

class ModelA(models.Model):

     created = UnixTimeStampField(auto_now_add=True)
     modified = UnixTimeStampField(auto_now=True)
     str_ini = UnixTimeStampField(default='0.0')
     float_ini = UnixTimeStampField(default=0.0)
     int_ini = UnixTimeStampField(default=0.0)
     dt_ini = UnixTimeStampField(default=timezone.now)
     num_field = UnixTimeStampField(use_numeric=True, default=0.0)

Operation exmpale:

>>> m = modelA.objects.create()
>>> m.created
datetime.datetime(2015, 9, 2, 10, 41, 41, 937257, tzinfo=<UTC>)
>>> m.int_ini
datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)
>>> m.int_ini = 3
>>> m.save()
>>> m.int_ini
datetime.datetime(1970, 1, 1, 0, 3, tzinfo=<UTC>)
>>> m.num_field
0.0

Field Options

  • auto_now: Set to True for updating while saving, just like DatetimeField

  • auto_now_add: set to True for updating while creating, just like DatetimeField

  • round_to: percision (num) of round(value, num), default: 6

  • use_float: DEPRECATED in v0.3, see use_numeric

  • use_numeric: set as True that instance attribute would be numeric, default as False

Django settings

If USE_TZ is set to False, return current datetime (in UTC timezone) info without tzinfo while accessing attribute.

Example:

# In settings.py
USE_TZ = False

>>> m = modelA.objects.create()
>>> m.created
datetime.datetime(2015, 9, 2, 10, 41, 41, 937257)

Template Tags

Load template tags:

{% load unixtimestampfield %}

Two django template filter tags are available:

  • to_datetime: Filter value as datetime

  • to_timestamp: Filter value as timestamp

Tricky Sub-middleware

Due to value is stored as float, it’s hard for recognizing and leads to this tricky middleware.

Here are 3 modes to show data:

  • usf_default: Show data by default, according to use_numeric option of field. This is also default setting.

  • usf_datetime: Always convert to datetime object

  • usf_timestamp: Always convert to timestamp

Use USF_FORMAT to indicate display police in settings.py. Let’s see examples.

Assume ModelB as:

class ModelB(models.Model):

     num_field = UnixTimeStampField(use_numeric=True, default=0.0)
     dt_field = UnixTimeStampField(default=0.0)

Then getting field value what you want:

>>> m = ModelB()
# with USF_FORMAT='usf_default' in settings.py
>>> m.num_field, m.dt_field
(0.0, datetime.datetime(1970, 1, 1, 0, 0))

# with USF_FORMAT='usf_datetime' in settings.py
>>> m.num_field, m.dt_field
(datetime.datetime(1970, 1, 1, 0, 0), datetime.datetime(1970, 1, 1, 0, 0))

# with USF_FORMAT='usf_timestamp' in settings.py
>>> m.num_field, m.dt_field
(0.0, 0.0)

Version

v0.4.0 – Fix Python and Django compatiblity, check related section

v0.3.9 – Fix packages including in setup.py

v0.3.8 – Bugs fixed: Apply submiddleware to auto_now field and check format in submiddleware

V0.3.7 – Check minimum value.

V0.3.6 – Fix timezone problem. All records are stored UTC timezone and convert while retrive.

V0.3.5.1 – Integer compatibility and fix timezone problem

V0.3.5 – Parse time format: YYYY-mm-dd HH:MM:SS[.FFFFFF]

V0.3.4 – Bugs fixed.

V0.3.3 – Add sub-middleware and template tags

v0.3 – Add ordinal time field and change field options use_float to use_numeric!!!

v0.2 – Handle formfield and add options while init

v0.1 – Added UnixTimeStampField

LICENSE

MIT

Release files for django-unixtimestampfield 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-unixtimestampfield 1.0.3
File Size Uploaded
django_unixtimestampfield-1.0.3.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-unixtimestampfield 1.0.3
File Interpreter ABI Platform
django_unixtimestampfield-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / django_unixtimestampfield-1.0.3.tar.gz

Download URL django_unixtimestampfield-1.0.3.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b2ed1ff6dc37d6369c68e02ef43c1eb754518b99a5637c3f6329b9fc947e345e
BLAKE2b-256 checksum
How to use checksums
297de22731976b82e2922422bc1b3aa81adbea0181dd11047e3ed1a6f7e3a28f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2025.

Transparency log

Release files / django_unixtimestampfield-1.0.3-py3-none-any.whl

Download URL django_unixtimestampfield-1.0.3-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f32edf7f68534d179afb1ed1a959b870aeeadd77cb49f489d08b0d48424266ae
BLAKE2b-256 checksum
How to use checksums
78a95253c623789a95838da882f24e06c8cbb082c5a9c162d21468f68ff7968a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

0.5.0

2 release files

0.4.0

1 release file

0.3.9

1 release file

0.3.8

1 release file

0.3.7

1 release file

0.3.6

1 release file

0.3.5.1

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page