pytz

How to execute a command at exact time once a day in Django?

▼魔方 西西 提交于 2019-12-05 22:08:20
I am working on a Django web based project in which i need to build a application which work in the following sequence: 1) user open a page in which he need to enter a command and a time 2) Django application will execute that command at a given time on each day till user off the scheduler (by default it is True) What i am facing the problem is that : 1) How should i execute the commands on a time but on each day. To save the commands and time i created a following model in my models.py class commands(models.Model): username = models.ForeignKey(User) command = models.CharField(max_length=30)

How to convert a datetime from one arbitrary timezone to another arbitrary timezone

强颜欢笑 提交于 2019-12-05 21:44:27
Let's say I receive an arbitrary datetime object in a request, like this, which could be coming from any possible timezone - I don't know which one. For the sake of example, pretend it comes from the East Coast import pytz from colander import iso8601 ... ests1 = iso8601.parse_date('2016-04-01T00:00:00.0000-04:00') pretend ests1 is the object that comes in Using pytz, I can find out a bit about it's timezone etz = ests1.timetz() # 00:00:00-04:00 etzi = ests1.tzinfo # <FixedOffset '-04:00' datetime.timedelta(-1, 72000)> etzn = ests1.tzname() # '-04:00' # EST edst = ests1.dst() # '0:00:00' Note

PYTZ 'America/Edmonton' offset wrong [duplicate]

天涯浪子 提交于 2019-12-05 08:15:32
This question already has an answer here : Closed 6 years ago . Possible Duplicate: Weird timezone issue with pytz This seems wrong: >>> import pytz >>> z1 = timezone('America/Edmonton') >>> z2 = timezone('US/Mountain') >>> z1 <DstTzInfo 'America/Edmonton' LMT-1 day, 16:26:00 STD> >>> z2 <DstTzInfo 'US/Mountain' MST-1 day, 17:00:00 STD> >>> pytz.VERSION '2012f' >>> 'America/Edmonton' and 'US/Eastern' should be the same time zone (17:00:00 STD). Not to mention 16:26:00 doesn't make any sense. -- Update -- The above makes sense in context of Jon Skeet's answer. However, things get strange when I

pytz install on Mac

混江龙づ霸主 提交于 2019-12-05 05:02:11
问题 Django informs me "ImportError: No module named pytz", but when I go to use pip to install it, I get this result: Requirement already satisfied (use --upgrade to upgrade): pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python Is it possible that it is looking in the wrong location, or that I need to try to install it somewhere else? Here is the full stacktrace: Internal Server Error: /basicloginwebservice/ Traceback (most recent call last): File "/Library/Python/2

converting from local to utc timezone

☆樱花仙子☆ 提交于 2019-12-04 13:31:01
问题 I'm attempting to craft a function that takes a time object and converts it to UTC time. The code below appears to be off by one hour. When i run noon through the converter, i get back 18:00:00. But when i run the same data through online converters, i get 17:00:00. What am i doing wrong here? Any help would be greatly appreciated. import pytz, datetime def convert_to_utc(time, tz): now_dt = datetime.datetime.utcnow() #get a date object date_dt = now_dt.date() #combine the current date object

Issue with python/pytz Converting from local timezone to UTC then back

这一生的挚爱 提交于 2019-12-04 05:48:09
I have a requirement to convert a date from a local time stamp to UTC then back to the local time stamp. Strangely, when converting back to the local from UTC python decides it is PDT instead of the original PST so the post converted date has gained an hour. Can someone explain to me what is going on or what I am doing wrong? from datetime import datetime from pytz import timezone import pytz DATE_FORMAT = '%Y-%m-%d %H:%M:%S %Z%z' def print_formatted(dt): formatted_date = dt.strftime(DATE_FORMAT) print "%s :: %s" % (dt.tzinfo, formatted_date) #convert the strings to date/time date = datetime

pytz difference of 2 datetimes in seconds? (Different time zones)

我们两清 提交于 2019-12-04 05:39:33
问题 I have 2 datetime objects with 2 different time zones: datetime1 = 18:26:23, with tzinfo = UTC datetime2 = 14:30:00, with tzinfo = US/Eastern Both dates are on the same day. There should be exactly 1 hour, 3 minutes and 37 seconds difference between the 2 datetimes, which is: 3817 seconds total difference. However, when I use the following code to compare: time_diff = (datetime2 - datetime1).total_seconds() time_diff gives me a value of: 3576. Am I doing the difference in seconds wrong? Or am

Why does time still show with UTC time zone instead of settings.TIME_ZONE?

本小妞迷上赌 提交于 2019-12-04 05:33:37
问题 I have a model that shows a short string in __str__() method of the model def __str__(self): return "Scheduled at %s" % (self.date_time.strftime("%B %d %Y %I:%M %p")) #Output: <Task: Scheduled at September 30 2018 12:30 AM> # it should have been: <Task: Scheduled at September 29 2018 08:30 PM> When I go to the Django admin , I see in the title Task: Scheduled at September 30 2018 12:30 AM and in the input it is filled with the correct TIME_ZONE : 20:30:00 settings.py TIME_ZONE = 'Etc/GMT+4'

Why a timezone aware datetime's tzinfo does not equal the timezone?

北城余情 提交于 2019-12-04 03:58:56
问题 >>> import pytz >>> tz = pytz.timezone('America/Chicago') >>> dt_naive = datetime(year=2017, month=6, day=6) >>> dt_aware = tz.localize(dt_naive) >>> dt_aware.tzinfo == tz False What's the reason for these to differ? >>> dt_aware.tzinfo <DstTzInfo 'America/Chicago' CDT-1 day, 19:00:00 DST> >>> tz <DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD> 回答1: The key that determines the timezone from pytz is the string you passed to create the object: 'America/Chicago' . That key is available

How to store a naive datetime in Django 1.4

北慕城南 提交于 2019-12-04 03:37:11
I have a naive date and time in the format '2012-05-19 19:13:00' and need to store it using Django 1.4 and its timezone-aware abilities. Although there is no way of knowing what timezone the date is originally in, it seems to make sense to store it as if it were UTC. However, using pytz etc, I'm not sure how to convert a date that has no timezone into a UTC datetime. If it has no tzinfo then of course there can be no conversion to UTC. Instead you could just make the datetime object into an time-zone aware one: import datetime from pytz import UTC dt = datetime.datetime.now() # just some date