27đź‘Ť
Assuming you’ve got pytz
installed:
from datetime import datetime
import pytz
local_tz = pytz.timezone("Asia/Singapore")
utc_dt = datetime.utcfromtimestamp(timestamp).replace(tzinfo=pytz.utc)
local_dt = local_tz.normalize(utc_dt.astimezone(local_tz))
For example:
>>> from datetime import datetime
>>> import pytz
>>> local_tz = pytz.timezone("Asia/Singapore")
>>> utc_dt = datetime.utcfromtimestamp(1325376000).replace(tzinfo=pytz.utc)
>>> utc_dt
datetime.datetime(2012, 1, 1, 0, 0, tzinfo=<UTC>)
>>> local_dt = local_tz.normalize(utc_dt.astimezone(local_tz))
>>> local_dt
datetime.datetime(2012, 1, 1, 8, 0, tzinfo=<DstTzInfo 'Asia/Singapore' SGT+8:00:00 STD>)
>>> local_dt.replace(tzinfo=None)
datetime.datetime(2012, 1, 1, 8, 0)
38đź‘Ť
all methods above are valide, but not “django like”.
Here is a simple example, how a django programmer would do that:
from datetime import datetime
from django.utils.timezone import make_aware
# valid timestamp
value = 1531489250
# you can pass the following obj to a DateTimeField, when your settings.USE_TZ == True
datetime_obj_with_tz = make_aware(datetime.fromtimestamp(value))
See more utilites on the Django github timezone module to get whole overview…
- [Django]-Access tuple in django template
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-Django: get unique object list from QuerySet
7đź‘Ť
Pass the pytz tzinfo object to fromtimestamp()
method:
#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz
tz = pytz.timezone("Asia/Singapore")
print(datetime.fromtimestamp(1325376000, tz))
# -> 2012-01-01 08:00:00+08:00
Note: the result object is timezone-aware: you could compare it with other aware datetime objects i.e., you don’t need to convert it to UTC for comparison — you can use it as is.
I dont even know where +06:55 is coming from when singapore is +08:00.
You see +06:55
due to the invalid .replace()
call. get_current_timezone()
returns pytz.timezone("Asia/Singapore")
that has a variable utc offset (it may have a different utc offset at different dates). When you call .replace()
some random (depends on the implementation) tzinfo object is used. The issue is that .replace()
method does not allow pytz.timezone("Asia/Singapore")
to choose the correct tzinfo for the input date.
>>> list(tz._tzinfos.values())
[<DstTzInfo 'Asia/Singapore' MALT+7:00:00 STD>,
<DstTzInfo 'Asia/Singapore' MALT+7:20:00 STD>,
<DstTzInfo 'Asia/Singapore' JST+9:00:00 STD>,
<DstTzInfo 'Asia/Singapore' SMT+6:55:00 STD>,
<DstTzInfo 'Asia/Singapore' SGT+7:30:00 STD>,
<DstTzInfo 'Asia/Singapore' MALT+7:30:00 STD>,
<DstTzInfo 'Asia/Singapore' MALST+7:20:00 DST>,
<DstTzInfo 'Asia/Singapore' LMT+6:55:00 STD>,
<DstTzInfo 'Asia/Singapore' SGT+8:00:00 STD>]
i.e., both +06:55
and +0800
are valid (at different dates) for Singapore. That is why you should use .replace()
only with timezones that have a constant utc offset such as the utc timezone itself (the offset is zero, always for any date).
fromtimestamp(,tz)
method calls tz.fromutc()
internally that allows tz
to choose the correct offset for a given utc time.
- [Django]-What is the best way to upload files in a modern browser
- [Django]-Django – Reverse for '' not found. '' is not a valid view function or pattern name
- [Django]-Django 'Sites' Model – what is and why is 'SITE_ID = 1'?