Based on this code, how do I get the UTC offset of a given date by seconds. Get UTC offset from time zone name in python
Currently, I have this code:
Python 2.7.3 (default, Jul 3 2012, 19:58:39)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
>>> import datetime
>>> offset = datetime.datetime.now(pytz.timezone('US/Pacific')).strftime('%z')
>>> offset
'-0700'
>>> int(offset)*3600
-2520000
>>> int(offset)*360
-252000
>>>
For example, US/Pacific sometimes is -800 hours (probably military time :D) but I need it to be in seconds, ex: -25200.
Shall I just multiply it by 360? Doesn't sound right but who knows.
TIA
>>> import datetime, pytz
>>> melbourne = pytz.timezone("Australia/Melbourne")
>>> melbourne.utcoffset(datetime.datetime.now())
datetime.timedelta(0, 36000)
>>> pacific = pytz.timezone("US/Pacific")
>>> pacific.utcoffset(datetime.datetime.now())
datetime.timedelta(-1, 61200)
>>> -1*86400+61200
-25200
>>> pacific.utcoffset(datetime.datetime.now()).total_seconds()
-25200.0
Related
I have epoch time like this 1488445200000
and I need to convert it to datetime variable. I tried
from datetime import datetime
dt = datetime.fromtimestamp(1488445200000 // 3600)
dt
I got
datetime.datetime(1983, 2, 7, 10, 10)
but the result does not seem to be right (the right one should be March 2, 2017 10:00:00 AM GMT+01:00)... what shall I do?
And what if what is I need to do is not just for one value 1488445200000 but for a column in a dataframe. Do I have to loop? Or is there a matrix operation?
Your timestamp is in milliseconds and datetime expects seconds. Divide by 1000 and it works:
$ python3
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1488445200000 // 1000)
>>> dt
datetime.datetime(2017, 3, 2, 10, 0)
Try This:
Use datetime module:
from datetime import datetime
ts = int(1488445200000//1000)
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2015, 8, 30, 23, 17, 6, 937659)
>>> datetime.datetime.now()
datetime.datetime(2015, 8, 30, 23, 17, 14, 378097)
>>>
In Python3.4 datetime.today() and datetime.now() What's the difference?
According to the document of datetime.now(tz=None):
Return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).
Else tz must be an instance of a class tzinfo subclass, and the current date and time are converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).
Thus datetime.now() supplies more precision if possible.
I have the following timestamps since epoch:
Timestamp
1346114717972
1354087827000
How can I convert these timestamps to some specific output format, e.g., mm/dd/yyyy hr:min:sec?
I have tried to convert them to datetime.datetime but it failed:
>>> datetime.datetime.fromtimestamp(1346114717972)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: timestamp out of range for platform time_t
How can I do this?
I would use the time module
>>> import time
>>> time.gmtime(1346114717972/1000.)
time.struct_time(tm_year=2012, tm_mon=8, tm_mday=28, tm_hour=0, tm_min=45, tm_sec=17, tm_wday=1, tm_yday=241, tm_isdst=0)
This shows the timestamp in UTC/GMT time.
The timestamp is divided by 1000 as the stamps you have provided are in milliseconds since the epoch, not seconds.
Then use strftime to format like so:
>>> time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(1346114717972/1000.))
'08/28/2012 00:45:17'
Assuming millisecond resolution:
import datetime
s = '1346114717972'
fmt = "%Y-%m-%d %H:%M:%S"
# local time
t = datetime.datetime.fromtimestamp(float(s)/1000.)
print t.strftime(fmt) # prints 2012-08-28 02:45:17
# utc time
t_utc = datetime.datetime.utcfromtimestamp(float(s)/1000.)
print t_utc.strftime(fmt) # prints 2012-08-28 00:45:17
Have a look at the documentation for the strftime() and strptime() behavior.
This is the simplest method I've ever seen-
$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print(time.strftime('%Y-%m-%dT%H:%M:%S %Z',time.localtime(time.time())))
2018-05-02T13:21:44 IST
I have converted a ctime value to unicode
1295478503.6789999
to
'Wed Jan 19 18:08:23 2011'
Can I go backwards?
From the second line to the first?
You want the Python time module, specifically the strptime() function. (How'd you convert from ctime to unicode/string? Probably with time.strftime(). So it makes sense to look for the inverse direction of that, which is time.strptime().)
Here's a good Google query for this, too: http://www.google.com/search?q=python+parse+time+string
Sample:
entropy:~>python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> u = u'Wed Jan 19 18:08:23 2011'
>>> t = time.strptime(u)
>>> t
time.struct_time(tm_year=2011, tm_mon=1, tm_mday=19, tm_hour=18, tm_min=8, tm_sec=23, tm_wday=2, tm_yday=19, tm_isdst=-1)
>>> time.mktime(t)
1295489303.0
import time
time.mktime(time.strptime('Wed Jan 19 18:08:23 2011'))
Is there an out of the box way to format in python (or within django templates), a date with full month name in accordance to polish language rules?
I want to get:
6 września 2010
and not:
>>> datetime.today().date().strftime("%d %B %Y")
'06 wrzesień 2010'
Use Babel:
>>> import babel.dates
>>> import datetime
>>> now = datetime.datetime.now()
>>> print(babel.dates.format_date(now, 'd MMMM yyyy', locale='pl_PL'))
6 września 2010
Update: Incorporated Nathan Davis' comment.
https://docs.djangoproject.com/en/stable/ref/templates/builtins/#date
E
Month, locale specific alternative representation usually used for long date representation.
'listopada' (for Polish locale, as opposed to 'Listopad')
If you want to specify format in your html template then this will do:
{{ datefield|date:"j E Y" }}
OP's code now works as he intended: returning '30 kwietnia 2020' and not 30 kwiecień 2020. Tested today with Python 2.7 and Python 3.6. Setting locale was required, I used the platform one - this might be an issue with web-apps as per other answers.
$ python
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [cut repeated code]
$ python3.6
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2020, 4, 30, 18, 13, 21, 308217)
>>> datetime.datetime.today().strftime("%d %B %Y")
'30 April 2020'
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'pl_PL.UTF-8'
>>> datetime.datetime.today().strftime("%d %B %Y")
'30 kwietnia 2020'
Try this...
datetime.today().date().strftime("%-d %B %Y")