Python : Convert time format to unix_timeformat - python

I have a time format like this
t = "2012-03-20T08:31:00-05:00"
I can extract the time contents using RegEx like this.
p = re.compile("(\d{4})\-(\d\d)\-(\d\d)T(\d\d):(\d\d):(\d\d)[\-|+]\d\d:\d\d")
matches = p.findall(t)
But I was wondering if there is a way to convert this format directly to unix_timestamp without using RegEx ? Is there a calendar library or something similar ?

datetime.datetime.strptime is your friend :)
http://docs.python.org/library/datetime.html#datetime.datetime.strptime

Use time.strptime:
time.strptime(t, "%Y-%m-%dT%H:%M:%S-%Z")
Unfortunately, this doesn't appear to work with numeric time zone offsets. python-dateutil should be able to handle your format:
dateutil.parser.parse(t)
Alternatively, you could split your string before the numeric offset:
t, offset_sign, offset = t[:-6], t[-6], t[-5:]
t = time.strptime(t, "%Y-%m-%dT%H:%M:%S")
offset = time.strptime(offset, "%H:%M")

Use strptime from the time module

Related

convert list of string to ISO 8601/RFC 3339 format

I have a list of strings start_date containing 147 different timestamps 2022-02-05T22:58:59. I am trying to convert each date/element in the list to an ISO 8601/RFC 3339 format YYYY-MM-DDTHH:mm:ssZ.
print(start_date)
['2022-02-05T22:58:59', '2022-01-27T22:58:59', '2022-01-27T22:58:59', '2022-01-25T22:58:59', '2022-01-22T22:58:59', '2022-01-21T22:58:59', '2022-01-15T22:58:59', '2022-01-11T22:58:59', '2022-01-09T22:58:59', '2021-12-29T22:58:59', '2021-12-15T22:58:59', '2021-11-26T22:58:59', '2021-11-24T22:58:59', '2021-11-05T22:58:59', '2021-10-24T22:58:59', '2021-10-14T22:58:59', '2021-10-09T22:58:59', '2021-09-29T22:58:59', '2021-09-18T22:58:59', '2021-09-16T22:58:59', '2021-09-12T22:58:59', '2021-08-29T22:58:59', '2021-08-28T22:58:59', '2021-08-27T22:58:59', '2021-08-22T22:58:59', '2021-08-14T22:58:59', '2021-08-12T22:58:59', '2021-08-06T22:58:59', '2021-08-04T22:58:59', '2021-07-28T22:58:59', '2021-07-28T22:58:59', '2021-07-22T22:58:59', '2021-07-16T22:58:59', '2021-07-09T22:58:59', '2021-07-07T22:58:59', '2021-07-07T22:58:59', '2021-07-02T22:58:59', '2021-06-30T22:58:59', '2021-06-25T22:58:59', '2021-06-12T22:58:59', '2021-06-10T22:58:59', '2021-06-09T22:58:59', '2021-06-03T22:58:59', '2021-05-27T22:58:59', '2021-05-09T22:58:59', '2021-05-06T22:58:59', '2021-04-30T22:58:59', '2021-04-13T22:58:59', '2021-04-10T22:58:59', '2021-04-07T22:58:59', '2021-03-28T22:58:59', '2021-03-25T22:58:59', '2021-03-18T22:58:59', '2021-03-12T22:58:59', '2021-03-10T22:58:59', '2021-03-06T22:58:59', '2021-03-05T22:58:59', '2021-02-16T22:58:59', '2021-02-10T22:58:59', '2021-02-05T22:58:59', '2021-01-02T22:58:59', '2020-12-04T22:58:59', '2020-07-29T22:58:59', '2020-07-01T22:58:59', '2020-06-24T22:58:59', '2020-06-06T22:58:59', '2020-06-05T22:58:59', '2020-06-03T22:58:59', '2020-05-30T22:58:59', '2020-05-29T22:58:59', '2020-05-28T22:58:59', '2020-05-23T22:58:59', '2020-05-22T22:58:59', '2020-05-21T22:58:59', '2020-05-16T22:58:59', '2020-05-14T22:58:59', '2020-04-29T22:58:59', '2020-04-23T22:58:59', '2020-04-22T22:58:59', '2020-04-12T22:58:59', '2020-04-10T22:58:59', '2020-04-01T22:58:59', '2020-04-01T22:58:59', '2020-03-20T22:58:59', '2020-03-06T22:58:59', '2020-02-28T22:58:59', '2020-02-16T22:58:59', '2020-02-15T22:58:59', '2020-02-08T22:58:59', '2020-02-06T22:58:59', '2020-02-05T22:58:59', '2020-01-30T22:58:59', '2020-01-29T22:58:59', '2020-01-18T22:58:59', '2020-01-17T22:58:59', '2020-01-11T22:58:59', '2020-01-10T22:58:59', '2020-01-10T22:58:59', '2020-01-09T22:58:59', '2020-01-03T22:58:59', '2019-12-14T22:58:59', '2019-12-12T22:58:59', '2019-12-06T22:58:59', '2019-11-28T22:58:59', '2019-11-20T22:58:59', '2019-11-15T22:58:59', '2019-11-09T22:58:59', '2019-10-23T22:58:59', '2019-10-10T22:58:59', '2019-10-02T22:58:59', '2019-09-21T22:58:59', '2019-09-18T22:58:59', '2019-09-14T22:58:59', '2019-09-13T22:58:59', '2019-09-06T22:58:59', '2019-08-31T22:58:59', '2019-07-26T22:58:59', '2019-07-24T22:58:59', '2019-06-27T22:58:59', '2019-06-20T22:58:59', '2019-06-06T22:58:59', '2019-05-29T22:58:59', '2019-05-15T22:58:59', '2019-05-08T22:58:59', '2019-05-03T22:58:59', '2019-05-01T22:58:59', '2019-04-27T22:58:59', '2019-04-24T22:58:59', '2019-04-18T22:58:59', '2019-04-13T22:58:59', '2019-04-06T22:58:59', '2019-03-22T22:58:59', '2019-03-21T22:58:59', '2019-03-20T22:58:59', '2019-03-07T22:58:59', '2019-03-02T22:58:59', '2019-02-16T22:58:59', '2019-01-26T22:58:59', '2019-01-23T22:58:59', '2019-01-10T22:58:59', '2019-01-09T22:58:59', '2019-01-05T22:58:59', '2018-12-14T22:58:59', '2018-12-06T22:58:59', '2018-11-29T22:58:59', '2018-11-23T22:58:59', '2018-11-21T22:58:59']
So far I have tried using the rfc3339 function from library rfc3339
start_date = rfc3339(start_date, utc=True, use_system_timezone=False)
It results in following error TypeError: 'module' object is not callable
Are there any other solutions? Thank you
Based on that error message, you've probably done import rfc3339, not from rfc3339 import rfc3339. You can't call the module (as it's telling you).
That said, if all your strings are of the format YYYY-MM-DDTHH:mm:ss and you want YYYY-MM-DDTHH:mm:ssZ, why not just...
z_dates = [date + "Z" for date in start_date]

Transform continious date string (20190327200000000W) in date time

I'm doing an application which parse a XML from http request and one of the attributes is a date.
The problem is that the format is a string without separation, for example: '20190327200000000W' and I need to transform it into a datetime format to send it to a database.
All the information I have found is with some kind of separation char (2019-03-23 ...). Can you help me?
Thanks!!!
Maybe this? (in jupypter notebook)
from datetime import datetime
datetime_object = datetime.strptime('20190327200000000W', '%Y%m%d%H%M%S%fW')
datetime_object
Well I have solved this, at first I did that Xenobiologist said, but I had a format problem, so I decided to delete the last character (the X of %X)...and I realized that I hadn't a string, I had a list, so I transformed to string and did the operations. My code (I'll put only the inside for loop part, without the parsing part):
for parse in tree.iter(aaa):
a = parse.get(m)
respon = a.split(' ')
if m == 'Fh':
x = str(respon[0])
x2 = len(x)
x3 = x[:x2-1]
print (x3)
y = time.strptime(x3, "%Y%m%d%H%M%S%f")

What is an efficient way to test a string for a specific datetime format like "m%/d%/Y%" in python 3.6?

In my Python 3.6 application, from my input data I can receive datatimes in two different formats:
"datefield":"12/29/2017" or "datefield":"2017-12-31"
I need to make sure the that I can handle either datetime format and convert them to (or leave it in) the iso 8601 format. I want to do something like this:
#python pseudocode
import datetime
if datefield = "m%/d%/Y%":
final_date = datetime.datetime.strptime(datefield, "%Y-%m-%d").strftime("%Y-%m-%d")
elif datefield = "%Y-%m-%d":
final_date = datefield
The problem is I don't know how to check the datefield for a specific datetime format in that first if-statement in my pseudocode. I want a true or false back. I read through the Python docs and some tutorials. I did see one or two obscure examples that used try-except blocks, but that doesn't seem like an efficient way to handle this. This question is unique from other stack overflow posts because I need to handle and validate two different cases, not just one case, where I can simply fail it if it does validate.
You can detect the first style of date by a simple string test, looking for the / separators. Depending on how "loose" you want the check to be, you could check a specific index or scan the whole string with a substring test using the in operator:
if "/" in datefield: # or "if datefield[2] = '/'", or maybe "if datefield[-5] = '/'"
final_date = datetime.datetime.strptime(datefield, "%m/%d/%Y").strftime("%Y-%m-%d")
Since you'll only ever deal with two date formats, just check for a / or a - character.
import datetime
# M/D/Y
if '/' in datefield:
final_date = datetime.datetime.strpdate(date, '%M/%D/%Y').isoformat()
# Y-M-D
elif '-' in datefield:
final_date = datetime.datetime.strpdate(date, '%Y-%M-%D').isoformat()
A possible approach is to use the dateutil library. It contains many of the commonest datetime formats and can automagically detect these formats for you.
>>> from dateutil.parser import parse
>>> d1 = "12/29/2017"
>>> d2 = "2017-12-31"
>>> parse(d1)
datetime.datetime(2017, 12, 29, 0, 0)
>>> parse(d2)
datetime.datetime(2017, 12, 31, 0, 0)
NOTE: dateutil is a 3rd party library so you may need to install it with something like:
pip install python-dateutil
It can be found on pypi:
https://pypi.python.org/pypi/python-dateutil/2.6.1
And works with Python2 and Python3.
Alternate Examples:
Here are a couple of alternate examples of how well dateutil handles random date formats:
>>> d3 = "December 28th, 2017"
>>> parse(d3)
datetime.datetime(2017, 12, 28, 0, 0)
>>> d4 = "27th Dec, 2017"
>>> parse(d4)
datetime.datetime(2017, 12, 27, 0, 0)
I went with the advice of #ChristianDean and used the try-except block in effort to be Pythonic. The first format %m/%d/%Y appears a bit more in my data, so I lead the try-except with that datetime formatting attempt.
Here is my final solution:
import datetime
try:
final_date = datetime.datetime.strptime(datefield, "%m/%d/%Y").strftime("%Y-%m-%d")
except ValueError:
final_date = datetime.datetime.strptime(datefield, "%Y-%m-%d").strftime("%Y-%m-%d")

How to trim spaces within timestamps using 'm/d/yy' format

I have a Python script that generates .csv files from other data sources.
Currently, an error happens when the user manually adds a space to a date by accident. Instead of inputting the date as "1/13/17", a space may be added at the front (" 1/13/17") so that there's a space in front of the month.
I've included the relevant part of my Python script below:
def processDateStamp(sourceStamp):
matchObj = re.match(r'^(\d+)/(\d+)/(\d+)\s', sourceStamp)
(month, day, year) = (matchObj.group(1), matchObj.group(2), matchObj.group(3))
return "%s/%s/%s" % (month, day, year)
How do I trim the space issue in front of month and possibly on other components of the date (the day and year) as well for the future?
Thanks in advance.
Since you're dealing with dates, it might be more appropriate to use datetime.strptime than regex here. There are two advantages of this approach:
It makes it slightly clearer to anyone reading that you're trying to parse dates.
Your code will be more prone to throw exceptions when trying to parse data that doesn't represent dates, or represent dates in an incorrect format - this is good because it helps you catch and address issues that might otherwise go unnoticed.
Here's the code:
from datetime import datetime
def processDateStamp(sourceStamp):
date = datetime.strptime(sourceStamp.replace(' ', ''), '%M/%d/%y')
return '{}/{}/{}'.format(date.month, date.day, date.year)
if __name__ == '__main__':
print(processDateStamp('1/13/17')) # 1/13/17
print(processDateStamp(' 1/13/17')) # 1/13/17
print(processDateStamp(' 1 /13 /17')) # 1/13/17
You also can use parser from python-dateutil library. The main benefit you will get - it can recognize the datetime format for you (sometimes it may be useful):
from dateutil import parser
from datetime import datetime
def processDateTimeStamp(sourceStamp):
dt = parser.parse(sourceStamp)
return dt.strftime("%m/%d/%y")
processDateTimeStamp(" 1 /13 / 17") # returns 01/13/17
processDateTimeStamp(" jan / 13 / 17")
processDateTimeStamp(" 1 - 13 - 17")
processDateTimeStamp(" 1 .13 .17")
Once again, a perfect opportunity to use split, strip, and join:
def remove_spaces(date_string):
date_list = date_string.split('/')
result = '/'.join(x.strip() for x in date_list)
return result
Examples
In [7]: remove_spaces('1/13/17')
Out[7]: '1/13/17'
In [8]: remove_spaces(' 1/13/17')
Out[8]: '1/13/17'
In [9]: remove_spaces(' 1/ 13/17')
Out[9]: '1/13/17'

Convert Military Time from text file to Standard time Python

I am having problems with logic on how to convert military time from a text file to standard time and discard all the wrong values. I have only got to a point where the user is asked for the input file and the contents are displayed from the text file entered. Please help me
Python's datetime.time objects use "military" time. You can do things like this:
>>> t = datetime.time(hour=15, minute=12)
>>> u = datetime.time(hour=16, minute=44)
>>> t = datetime.datetime.combine(datetime.datetime.today(), t)
>>> t
datetime.datetime(2011, 5, 11, 15, 12)
>>> u = datetime.datetime.combine(datetime.datetime.today(), u)
>>> t - u
datetime.timedelta(-1, 80880)
With a little twiddling, conversions like the ones you describe should be pretty simple.
Without seeing any code, it's hard to tell what exactly you want. But I assume you could do something like this:
raw_time = '2244'
converted_time = datetime.time(hour=int(raw_time[0:2]), minute=int(raw_time[2:4]))
converted_time = datetime.datetime.combine(datetime.datetime.today(), converted_time)
Now you can work with converted_time, adding and subtracting timedelta objects. Fyi, you can create a timedelta like so:
td = datetime.timedelta(hours=4)
The full list of possible keyword arguments to the timedelta constructor is here.
from dateutil.parser import parse
time_today = parse("08:00")
from dateutil.relativedelta import relativedelta
required_time = time_today-relativedelta(minutes=35)
print required_time
datetime.datetime(2011, 5, 11, 7, 25)
It's not a true answer like the other two, but the philosophy I use when dealing with dates and times in python: convert to a datetime object as soon as possible after getting from the user, and only convert back into a string when you are presenting the value back to the user.
Most of the date/time math you will need can be done by datetime objects, and their cousins the timedelta objects. Unless you need ratios of timedeltas to other timedeltas, but that's another story.

Categories