python strptime from UTC string to datetime regex re.sub - python

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)

Related

Convert Date and Time in required format "Jun 9 18:04:42:"

Current Format is Jun 9 18:04:42:
Required Format is 09/06/2021 18:04:42:00
There is no year in current format, but I need to add it.
I have tried following code
s= 'Jun 9 18:04:42:'
'''
def convert_to_date(s):
if s!='None':
if s=='':
return s
else:
print ("to unpack values:",s)
s=s.replace(' ', ':')
f_month,f_day,f_hour,f_minute,f_seconds,f_miliseconds = s.split(':')
s = str(f_month) +'-' + str(f_day) + ' ' + str(f_hour) + ':' + str(f_minute)+':'+str(f_seconds) +':'+str(f_miliseconds)
print ("unpacked values:",s)
return s
'''
output is: 'Jun-9 18:04:42:'
Use dateutil module's parser.parse method, which can parse a range of differently formatted date strings and return a datetime.datetime object. Then you can convert it to any other format using strftime function:
>>> from dateutil.parser import parse
>>> dt = parse('Jun 9 18:04:42').strftime('%d/%m/%Y %H:%M:%S:00')
>>> print (dt)
09/06/2021 18:04:42:00

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

How to translate datetime string to sql format?

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')

How get a datetime string to suffix my filenames?

I need a function to generate datafile names with a suffix which must be the current date and time.
I want for the date Feb, 18 2014 15:02 something like this:
data_201402181502.txt
But this is that I get: data_2014218152.txt
My code...
import time
prefix_file = 'data'
tempus = time.localtime(time.time())
suffix = str(tempus.tm_year)+str(tempus.tm_mon)+str(tempus.tm_mday)+
str(tempus.tm_hour)+str(tempus.tm_min)
name_file = prefix_file + '_' + suffix + '.txt'
You can use time.strftime for this, which handles padding leading zeros e.g. on the month:
from time import strftime
name_file = "{0}_{1}.txt".format(prefix_file,
strftime("%Y%m%d%H%M"))
If you simply turn an integer to a string using str, it will not have the leading zero: str(2) == '2'. You can, however, specify this using the str.format syntax: "{0:02d}".format(2) == '02'.
Looks like you want
date.strftime(format)
The format string will allow you to control the output of strftime, try something like:
"%b-%d-%y"
From http://docs.python.org/2/library/datetime.html
Using str.format with datetime.datetime object:
>>> import datetime
>>> '{}_{:%Y%m%d%H%M}.txt'.format('filename', datetime.datetime.now())
'filename_201402182313.txt'

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.

Categories