pytz

Django timezone问题

江枫思渺然 提交于 2019-12-18 22:20:44
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 今天用django做个blog碰到了问题,提交内容后浏览提示Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed? django官方文档这么说: When support for time zones is enabled, Django stores date and time information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms. This is handy if your users live in more than one time zone and you want to display date and time information according to each user’s wall clock. Even if your Web site

How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

99封情书 提交于 2019-12-17 18:49:02
问题 Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork . Does pytz give me those standard names somewhere, or will i have to manually do this via a table? This could potentially get messy, since for example places are EST in a season and shift to showing EDT during others. 回答1: Given a pytz timezone for a particular

pytz utc conversion

你说的曾经没有我的故事 提交于 2019-12-17 17:33:27
问题 What is the right way to convert a naive time and a tzinfo into an UTC time? Say I have: d = datetime(2009, 8, 31, 22, 30, 30) tz = timezone('US/Pacific') First way, pytz inspired: d_tz = tz.normalize(tz.localize(d)) utc = pytz.timezone('UTC') d_utc = d_tz.astimezone(utc) Second way, from UTCDateTimeField def utc_from_localtime(dt, tz): dt = dt.replace(tzinfo=tz) _dt = tz.normalize(dt) if dt.tzinfo != _dt.tzinfo: # Houston, we have a problem... # find out which one has a dst offset if _dt

get the DST boundaries of a given timezone in python

半世苍凉 提交于 2019-12-17 12:21:08
问题 Is it possible to get the DST boundaries of a given timezone with pytz? 回答1: It doesn't seem to be officially supported. However, you can poke at the internals of a DstTzInfo object and get it from there: >>> from pytz import timezone >>> tz = timezone('Europe/London') >>> tz._utc_transition_times [datetime.datetime(1, 1, 1, 0, 0), datetime.datetime(1916, 5, 21, 2, 0), ... datetime.datetime(2036, 3, 30, 1, 0), datetime.datetime(2036, 10, 26, 1, 0), datetime.datetime(2037, 3, 29, 1, 0),

Django: 'unicode' object has no attribute 'tzinfo'. Production server only

谁说我不能喝 提交于 2019-12-14 03:49:01
问题 I'm stumped. With my local set up (python manage.py runserver) everything runs fine. With my production set up (wsgiserver.CherryPyWSGIServer), I get 'unicode' object has no attribute 'tzinfo' when my program tries to convert datetime2(7) from the database to local time in the pytz module. Using Django 1.9. Both set ups use django-pyodbc-azure to connect to the same mssql database. In fact to troubleshoot this, both use the same settings files. # settings.py ... DATABASES = { 'default': {

Localize datetime (timezone aware) from timezone offset

痞子三分冷 提交于 2019-12-14 03:13:39
问题 I have a UTC timestamp and a timezone offset timestamp (both in milliseconds): utc_time = 1394452800000 timezoneoffset = -14400000 If I wanted to get the datetime I would do : print datetime.utcfromtimestamp(utc_time/1000) >>>2014-03-10 12:00:00 How can I localize this datetime but also the final object be timezone aware? If I divide timezoneoffset , -14400000/(3600*1000) = -4 (hours). So the final output should be: >>>2014-03-10 08:00:00-04:00 My try: from pytz import timezone from dateutil

How to manage timezones in a web application?

寵の児 提交于 2019-12-12 10:57:23
问题 I wan't to manage the different timezones of my users in my web application, but I have no idea where to start. I have to save the local time of each user in my database?, or maybe make the conversion to a UTC time, save it, and then make the conversion again to show it?, or there is another way? For example, if one of my users make an appointment in his local time, I have to convert it to UTC store it in my database, and then when he need it, convert it again to his local time an show it??

How to get pytz timezone from common abbreviation (PST, EST, etc.)?

青春壹個敷衍的年華 提交于 2019-12-12 08:35:05
问题 This is a similar question as How to get the common name for a pytz timezone eg. EST/EDT for America/New_York , except I want to be able to just get a timezone from "PST" from pytz. such as tz = timezone("PST") Is this something like this possible with pytz? 回答1: I ended up just manually making a dictionary that mapped abbreviations to timezone names. For example, 'PST' : 'America/Los_Angeles' would be an entry (as would PDT for the daylight savings abbreviation). 回答2: Have you tried using

Date conversion on server in different timezone

亡梦爱人 提交于 2019-12-12 02:13:59
问题 I have a date string (event occurrence) in local time (scraped from a web page using Scrapy) and I need to convert it to a timestamp. I do this using: return int(time.mktime(time.strptime(dateString, "%Y-%m-%d"))) That gives me a time stamp that I store in my database. Every day I scrape the page for new data so I need to compare dates. I use the same function as above to convert to a timestamp that I compare with the timestamp in the database. This way I avoid duplicate records. When I run

Get timestamp for 27/02/2019 00:00 US/Eastern in python using pytz and datetime

℡╲_俬逩灬. 提交于 2019-12-11 17:19:08
问题 I have the following string: 27/02/2019 As it is known in the program that those dates correspond to NY time zone, I would like to get the timestamp corresponding to: 27/02/2019 00:00 US/Eastern I have tried: import datetime import pytz exchange_tz = pytz.timezone('US/Eastern') _period1 = datetime.datetime(2019,2,27) _period1_localized = exchange_tz.localize(_period1) _period1_ts = int((_period1_localized - datetime.datetime(1970, 1, 1, tzinfo=exchange_tz)).total_seconds()) >>> _period1_ts