I am using Django 1.11 and Postgres 9.4.
How can I ensure TruncYear to produce Zulu time (2019-10-01T00:00:00Z). I notice it creates datetime with timezone like this (2017-01-01T00:00:00+03:00)
Here is my code for the TruncYear queryset:
from django.db.models import Count
from django.db.models.functions import TruncMonth, TruncYear, TruncDay, TruncHour
tracking_in_timeseries_data = Tracking.objects.annotate(
year=TruncYear('created_at')).values('year', 'venue').annotate(
count=Count('employee_id', distinct = True)).order_by('year')
>>> for exp in tracking_in_timeseries_data:
... print(exp['year'], exp['venue'], exp['count'])
2017-01-01 00:00:00+00:00 4 1
2019-01-01 00:00:00+00:00 2 2
2019-01-01 00:00:00+00:00 3 1
2019-01-01 00:00:00+00:00 4 1
2019-01-01 00:00:00+00:00 5 1
2019-01-01 00:00:00+00:00 6 1
>>> tracking_in_timeseries_data
<QuerySet [{'venue': 4, 'year': datetime.datetime(2017, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 2, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 2}, {'venue': 3, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 4, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 5, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}, {'venue': 6, 'year': datetime.datetime(2019, 1, 1, 0, 0, tzinfo=<UTC>), 'count': 1}]>
And if I serialize it produce this:
serializer.py
class TimeseriesYearSerializer(serializers.ModelSerializer):
venue = VenueTSSerializer(read_only=True)
year = serializers.DateTimeField(read_only=True)
count = serializers.IntegerField(read_only=True)
class Meta:
model = Tracking
fields = ['venue', 'year', 'count']
output:
[
{
"count": 1,
"year": "2017-01-01T00:00:00+03:00",
"venue_id": 2
},
{
"count": 1,
"year": "2018-01-01T00:00:00+03:00",
"venue_id": 1
},
{
"count": 1,
"year": "2018-01-01T00:00:00+03:00",
"venue_id": 2
},
{
"count": 3,
"year": "2019-01-01T00:00:00+03:00",
"venue_id": 1
},
{
"count": 3,
"year": "2019-01-01T00:00:00+03:00",
"venue_id": 2
}
]
How can I ensure TruncYear queryset it produce a datetime strings in Zulu time like this 2019-10-01T00:00:00Z rather than with a timezone 2019-01-01T00:00:00+03:00.
UPDATE: I noticed I temporary fix this by restarting the django service.
sudo supervisorctl stop all
sudo supervisorctl start all
It was then able to produce Z time like this 2019-10-01T00:00:00Z But after few hours, it started to produce timezone timeformat like this 2017-01-01T00:00:00+03:00
I also noticed if I rebooted the server, it wont have the Z time. I have to do supervisorctl stop and start then it temporarily seems to fix it.
Here are my code snippet for the supervisor restart
/home/user/myapp/gunicorn_start.bash
/etc/supervisor/conf.d/myapp.conf
https://gist.github.com/axilaris/01525b78fcdc03071fcd34818820d7f1
This is my server version Ubuntu 16.04.3 LTS
What could be the problem and how to fix it so it consistently produce Zulu time.