Django Unix timestamp (POSIX type) field
Project description
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 |
2.2.x |
v |
v |
v |
|
3.0.x |
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)
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-unixtimestampfield-0.5.0.tar.gz
.
File metadata
- Download URL: django-unixtimestampfield-0.5.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8224b19f79ffa2a6e21ef1a077b28c71cd457fde6e1cbbd2601d72788c83a8cb |
|
MD5 | f46fdf27a4c16c140e74ca64e94f05be |
|
BLAKE2b-256 | 6eb3e1507cd400d2b3a3adcba892aceebb97948807baa5cd742fa145f59a3e64 |
File details
Details for the file django_unixtimestampfield-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: django_unixtimestampfield-0.5.0-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee4fc20912aae357f864d46c520417f73cf7269be3bb5a2730a40600e934a747 |
|
MD5 | 452d2cf00d95e2153d91ff0500216158 |
|
BLAKE2b-256 | 604aaa7afa14334efeeced8073c1fd59aefaf05a3370fc48cbd18ba75dcb6519 |