Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - Django: How to make a datetime object aware of the timezone in which it was created?

I am running a program that requests ocean tide data from a remote server. The time and date of this tide data is being computed based on my machine's local time zone. I want to use these local dates and times to create a datetime object, which I will then save in a Django model.

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)

How do I ensure that the datetime object is aware that it was created based on a local time zone, before posting it to Django?

I guess, I would want it to look like this before posting it:

datetime_obj = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute, loc_timezone)

How do I get the local timezone of my machine dynamically? And how to I ensure that all users see the time converted to their own local timezone.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, make sure you're familiar with Django's documentation on timezones, set USE_TZ = True, and install pytz.

I don't quite understand where your date is coming from. If it's coming from the server as part of their data (i.e. it represents when the tides were measured), it should either be in UTC already or you will need to know the time zone they are using. If you're creating it, then the simplest thing is just to use django.utils.timezone.now() (which returns a timezone-aware datetime) when you create your model instance.

If you really need to manually create it as you've described, follow the usage here or use make_aware():

from django.utils.timezone import make_aware

naive = datetime(loc_year, loc_month, loc_date, loc_hour, loc_minute)
make_aware(naive)  # current timezone, or...
make_aware(naive, timezone=pytz.timezone("Europe/Helsinki"))  # ...specific timezone

The current timezone will be the default timezone (as defined by the TIME_ZONE setting) unless you've used activate() to specify a different one. The default time zone may or may not be the same as the server's system timezone. Getting the system timezone in a format that pytz can understand is discussed in this answer.

Finally, ensuring that users see the time converted to their local time zone is non-trivial, as discussed here:

The current time zone is the equivalent of the current locale for translations. However, there’s no equivalent of the Accept-Language HTTP header that Django could use to determine the user’s time zone automatically. Instead, Django provides time zone selection functions. Use them to build the time zone selection logic that makes sense for you.

See the examples there for guidance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...