How to translate datetime string to sql format? - python

Is there any shorter method to translate this ISO 8601 compatible UTC time to the SQL DATETIME format?
>>> str = "2016-03-28T20:23:46+0800"
>>> temp = str.split('T')
>>> temp[1] = temp[1].rstrip('+')
>>> temp[1]
'20:23:46+0800'
>>> temp[1] = temp[1].split('+')[0]
>>> result = " ".join(temp)
>>> result
'2016-03-28 20:23:46'
Thanks!

You can simply switch formats:
>>> date_str = "2016-03-28T20:23:46+0800"
>>> format = '%Y-%m-%dT%H:%M:%S%z'
>>> new_format = '%Y-%m-%d %H:%M:%S'
>>> datetime.datetime.strptime(date_str, format).strftime(new_format)
'2016-03-28 20:23:46'
This will not work in python 2.x as it does not support the %z flag. See timezone python 2 support for a workaround

There is no easy way to do this.
Checkout this post for more details on possible solutions.
If you're looking for a quick hack try this:
st = '2016-03-28T20:23:46+0800'
st[:19].replace("T", " ")
Or, if you need the date in datetime:
datetime.datetime.strptime(st[:19], '%Y-%m-%dT%H:%M:%S')

Related

How to convert a downloaded string to datetime format?

I am trying to check if today's date < date downloaded from text file online. Here is my code :
import datetime
import requests
URL = "http://directlinktotextfile.com/text.txt"
result = requests.get(URL)
today = datetime.datetime.now().date()
Url_date = result.text
Url_date.strip()
Url_date = datetime.date(Url_date)
if today < Url_date :
print "Today is less than future date"
raw_input()
else:
print "Today is greater than or = to future date"
raw_input()
The result that comes back is just this : 2018,02,14. I use .strip() in case there might be blank spaces or extra lines. I've printed out result.text after strip() and it shows the correct details. Why is it that I can't check if today < Url_date. It works fine if I enter manually a date into datetime.date(2018,02,14), but when I'm downloading the string it won't work. Any suggestions?
You pass string to datetime.date() which should be each an integer.
Url_list = []
Url_list = Url_date.split(",")
yr = int(Url_list[0])
mn = int(Url_list[1])
d = int(Url_list[2])
Now pass these integers to datetime.date
Url_date = datetime.date(yr, mn, d)
The arguments you pass to datetime.date(arg1, arg2, arg3) are not strings as a whole. When you pass it from url, what you are actually doing is
datetime.date("2018,2,14")
Note that you are passing only one string argument and not 3 different integers. You should split the date string using comma and then convert each into integers and then pass them as arguments to datetime.date.
Here is what your code is trying to do :
Url_date = datetime.date("2018,02,14")
But he wants to have:
Url_date = datetime.date(2018,02,14)
Do
Url_date.split(',') # Result: ['2018','02','14']
And then convert all the string in the array in integers
It should be ok :)
Use strptime:
import datetime
today = datetime.datetime.now().date()
parsed = datetime.datetime.strptime("2018,02,14", "%Y,%m,%d").date()
print(today < parsed) # True

Change the format of a QDate

I need to change the format of a QDate. Here is my code:
yday = (QtCore.QDate.currentDate().addDays(-1))
And I got this result...
PyQt4.QtCore.QDate(2015, 4, 2)
But I need the date in this format:
2015/04/03
A QDate can be converted to a string using its toString method:
>>> yday = QtCore.QDate.currentDate().addDays(-1)
>>> yday.toString()
'Thu Apr 2 2015'
>>> yday.toString(QtCore.Qt.ISODate)
'2015-04-02'
>>> yday.toString('yyyy/MM/dd')
'2015/04/02'
Note that this output is from Python3. If you're using Python2, by default, the output will be a QString - but it can be converted to a python string using unicode().
you can use datetime.strftime()
yourdate.strftime('%Y, %m, %d')

python strptime from UTC string to datetime regex re.sub

im using the following that works awesome for converting a UTC string to date times -- except when the milliseconds are LONGER than 6 digits. than it blows. urgh*&^*&
format = '%Y-%m-%dT%H:%M:%S'
if '.' in value:
format = format + '.%f'
if value.endswith('Z'):
format = format + 'Z'
return datetime.strptime(value, format)
here is the stacktrace
File "/usr/lib64/python2.6/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '2013-07-19T13:02:53.8241899Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
whats a method to restrict the milliseconds to 6 digits in length. ive used using '{:%Y-%m-%dT%H:%M:%S.6%f}'.format( datetime ) but that helps nonce/
so trying re.sub( ) to limit the decimals (if any) following the '.'character in the provided string. but am not very regex savvy.
im using python 2.6.* on the machine.
Sounds like a job for dateutil.
$ pip install python-dateutil
$ python
>>> import dateutil.parser
>>> d = dateutil.parser.parse('2013-07-19T13:02:53.8241899Z')
>>> print d
2013-07-19 13:02:53.824189+00:00
You could use .partition() method to parse the string:
from datetime import datetime
sz = '2013-07-19T13:02:53.8241899Z' # utc time in rfc3339 format (without offset)
sz = sz.rstrip('Z') # remove ending 'Z' if present
timestr, _, digits = sz.partition('.')
utc_dt = datetime.strptime(timestr, '%Y-%m-%dT%H:%M:%S')
if digits:
microseconds = int(digits, 10) * 10**(6 - len(digits))
utc_dt = utc_dt.replace(microsecond=int(microseconds + .5))
If you want to use re.sub and don't mind truncating the microseconds:
import re
from datetime import datetime
sz = '2013-07-19T13:02:53.8241899Z'
if '.' in sz:
sz = re.sub(r'(\.\d{,6})\d*Z?$', r'\1Z', sz) # truncate to 6 digits +Z
else: # no fractions
sz = sz.rstrip('Z') + '.0Z'
utc_dt = datetime.strptime(sz, '%Y-%m-%dT%H:%M:%S.%fZ')
If you wanted to do it with re.sub, here's a short example of one way to do that.
import re
from datetime import datetime
value = '2013-07-19T13:02:53.8241899Z'
regex = r"(\d{6,}\d+)"
if re.search(regex, value):
repl = lambda x: x.group(0)[:6]
value = re.sub(pattern=regex, repl=repl, string=value)
format = '%Y-%m-%dT%H:%M:%S'
if '.' in value:
format = format + '.%f'
if value.endswith('Z'):
format = format + 'Z'
print datetime.strptime(value, format)

Add utc time to filename python

How can i get the current time in UTC time (Zulu style for hours and minutes: 0100Z) , and add it to a string so i can concatenate it
This gives me cannot concatenate string:
import datetime
utc_datetime = datetime.datetime.utcnow()
utc_datetime.strftime("%Y-%m-%d-%H%MZ") //Result: '2011-12-12-0939Z'
filename = '/SomeDirectory/AnotherDirectory/FilePrefix_'+utc_datetime+'.txt'
And this gives me another string for the filename:
//returns: /SomeDirectory/AnotherDirectory/FilePrefix_2011-12-12 09:42:15.374022.txt
import datetime
utc_datetime = datetime.datetime.utcnow()
utc_datetime.strftime("%Y-%m-%d-%H%MZ") //Result: '2011-12-12-0939Z'
filename = '/SomeDirectory/AnotherDirectory/FilePrefix_'+str(utc_datetime)+'.txt'
Thanks in advance
What you want to do is probably :
import datetime
utc_datetime = datetime.datetime.utcnow()
formated_string = utc_datetime.strftime("%Y-%m-%d-%H%MZ") //Result: '2011-12-12-0939Z'
filename = '/SomeDirectory/AnotherDirectory/FilePrefix_%s.txt'% formated_string
or in a one-liner way :
filename = '/SomeDirectory/AnotherDirectory/FilePrefix_%s.txt'%datetime.datetime.utcnow().strftime("%Y-%m-%d-%H%MZ")
When using datetime.strftime it returns the string formatted as you need, it does not modify the datetime object.
EDIT : use %s instead of +, thanks Danilo Bargen
The strftime method of a datetime object only returns a value, but doesn't manipulate the original object. You need to save the result into the variable itself, or into a new variable.
import datetime
utc_datetime = datetime.datetime.utcnow().strftime("%Y-%m-%d-%H%MZ")
utc_datetime //Result: '2011-12-12-0939Z'
Additionally, you shouldn't use + to concatenate several strings because of performance reasons. Use this instead:
filename = '/directory/prefix_%s.txt' % utc_datetime
You need to save the result of utc_datetime.strftime() into a variable:
>>> import datetime
>>> utc_datetime = datetime.datetime.utcnow()
>>> s=utc_datetime.strftime("%Y-%m-%d-%H%MZ")
>>> filename = '/SomeDirectory/AnotherDirectory/FilePrefix_' + s + '.txt'
>>> print filename
/SomeDirectory/AnotherDirectory/FilePrefix_2011-12-12-0946Z.txt
>>>
You're currently computing a value and throwing away the string result.

Handling international dates in python

I have a date that is either in German for e.g,
2. Okt. 2009
and also perhaps as
2. Oct. 2009
How do I convert this into an ISO datetime (or Python datetime)?
Solved by using this snippet:
for l in locale.locale_alias:
worked = False
try:
locale.setlocale(locale.LC_TIME, l)
worked = True
except:
worked = False
if worked: print l
And then plugging in the appropriate for the parameter l in setlocale.
Can parse using
import datetime
print datetime.datetime.strptime("09. Okt. 2009", "%d. %b. %Y")
http://docs.python.org/library/locale.html
The datetime module is already locale-aware.
It's something like the following
# German locale
loc = locale.setlocale(locale.LC_TIME, ("de","de"))
try:
date = datetime.date.strptime(input, "%d. %b. %Y")
except:
# English locale
loc = locale.setlocale(locale.LC_TIME, ("en","us"))
date = datetime.date.strptime(input, "%d. %b. %Y")
Very minor point about your code snippet: I'm no Python expert but I'd consider the whole "flag to check for success + silently swallowing all exceptions" to be bad style.
try/expect/else does what you want in a cleaner way, I think:
for l in locale.locale_alias:
try:
locale.setlocale(locale.LC_TIME, l)
except locale.Error: # the doc says setlocale should throw this on failure
pass
else:
print l

Categories