python-datetime

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

Are there any datetime.tzinfo implementations in C?

為{幸葍}努か 提交于 2019-12-13 08:14:35
问题 I've been working on a Python library that uses a C extension module to do ISO 8601 parsing. Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Calls out to Python implementations of tzinfo (currently pytz.FixedOffset ) are simply too slow. In Python 3.7, datetime.timezone is finally exposed to the C-API. My code takes adavantage of it, and gets a tremendous performance boost from using a C implementation instead of a Python one. I'd love

How does utcnow() work without time-zones?

感情迁移 提交于 2019-12-13 04:37:55
问题 I am reading on SO that the Python standard library does not define any time-zones. Also, from the Python 2.7 datetime docs: Note that no concrete tzinfo classes are supplied by the datetime module. However, calling utcnow() generates the current time in UTC. Furthermore, Python can also provide the current local time with now() . I am assuming that this is only possible if Python "knows" the local time-to-UTC offset (in order to return the correct values). But doesn't that mean that Python

Cannot order by time using model manager

自古美人都是妖i 提交于 2019-12-13 04:22:24
问题 I made the following model manager class TaskManager(models.Manager): def get_queryset(self): return super(TaskManager,self).get_queryset().order_by('time__hour') But there is an error which says Cannot resolve keyword u'hour' into field. Join on 'time' not permitted. My models.py looks something like this - class Task(models.Model): content = models.TextField() slug = AutoSlugField(populate_from='content') time = models.DateTimeField() objects = TaskManager() I don't know what I am doing

Calculating average for relativedelta in python

只愿长相守 提交于 2019-12-13 04:00:53
问题 I have multiple relativedelta values in Python 3 and I would like to take an average of all these. For example, for the 2 values below, I would like a relativedelta value where hours = +3 day (average between the two). relativedelta(days=+2, hours=+1, minutes=+33, seconds=+51) relativedelta(hours=+4, minutes=+35, seconds=+36) How can I calculate the average between a list? If there was a way to get the total number of hours for each value, I could add them up and divide by number of values,

How to use date range in python to pull /query data using current date

我与影子孤独终老i 提交于 2019-12-13 03:14:01
问题 Below is my code that is grabbing data and converting the data into a CSV file (this is working). I am trying to only focus on the data that is returned from midnight till 4pm (BST (British Summer Time) UTC/GMT +1 hour) using the dates some how. Could someone show me how this is done please, DTDT is the date. If what I am trying to achieve does not make sense, just let me know I will try to explain it. My Code: from elasticsearch import Elasticsearch import csv es = Elasticsearch(["9200"]) #

How can i check if date is on range on Python? [duplicate]

女生的网名这么多〃 提交于 2019-12-13 02:42:44
问题 This question already has answers here : How to check if the current time is in range in python? (2 answers) Closed 2 years ago . I'm trying to check if today's date (in dd-mm-yyyy format) is in a given range. My code only checks the day, not the month or year... Could you help me to see what's wrong? Here it works fine... import datetime TODAY_CHECK = datetime.datetime.now() TODAY_RESULT = ('%s-%s-%s' % (TODAY_CHECK.day, TODAY_CHECK.month, TODAY_CHECK.year)) if '26-11-2017' <= TODAY_RESULT <

Create monthly time series with arbitrary start dates

依然范特西╮ 提交于 2019-12-13 01:27:49
问题 Using pandas it is easy to create a monthly series of dates. import pandas as pd pd.date_range('2012-04-23', '2013-01-23', freq='BM') DatetimeIndex(['2012-04-30', '2012-05-31', '2012-06-29', '2012-07-31', '2012-08-31', '2012-09-28', '2012-10-31', '2012-11-30', '2012-12-31'], dtype='datetime64[ns]', freq='BM') Notice that the dates in the DatetimeIndex are month ends. I know that it should be considering I chose freq='BM' but I don't believe I had a choice that would have accomplished my goal.

Why %z is not supported by python's strptime?

与世无争的帅哥 提交于 2019-12-12 13:18:41
问题 >>> datetime.strptime('2014-02-13 11:55:00 -0800', '%Y-%m-%d %H:%M:%S %z') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime (bad_directive, format)) ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z' I understand that it's not supported, but don't know why. Seems it's not hard to support that. And 'Offset from UTC' is not as ambiguous as

Finding time intervals per day from a list of timestamps in Python

会有一股神秘感。 提交于 2019-12-11 23:47:22
问题 i am trying to compute time intervals per day from a list of unix timestamps in Python. I have searched for simular questions on stack overflow but mostly found examples of computing deltas or SQL solutions. I have a list of the sort: timestamps = [1176239419.0, 1176334733.0, 1176445137.0, 1177619954.0, 1177620812.0, 1177621082.0, 1177838576.0, 1178349385.0, 1178401697.0, 1178437886.0, 1178926650.0, 1178982127.0, 1179130340.0, 1179263733.0, 1179264930.0, 1179574273.0, 1179671730.0, 1180549056