Reputation: 513
I have a scenario where i can get date time in any of the following format.
1) as string in following format : 1970-01-01T00:00:00
2) "now-5m"
3) "now-1h"
or similar combinations with min, second or even day, I want to get a time which is x min older than this given time, What would be the best way to do so ? Is there any library present which can handle all these formats and give me datetime in some uniform format?
Upvotes: 2
Views: 1923
Reputation: 46291
You can combine hours and minutes like this:
import datetime
#Convert the string to datetime object
dt = datetime.datetime.strptime('1970-01-01T00:00:00', '%Y-%m-%dT%H:%M:%S')
#Subtract one hour and 5 minutes together :)
print(dt - datetime.timedelta(hours=1, minutes=5))
It returns:
1969-12-31 22:55:00
Upvotes: 1
Reputation: 513
After doing more research i figured out there is a module called dateparser which provides a parse method, using parse method we can convert any format into datetime.datetime object and then we can subtract using relativedelta method as prosti suggested.
Sample code
import dateparser
from dateutil.relativedelta import relativedelta
print(dateparser.parse("now"))
print(dateparser.parse("now-30s"))
print(dateparser.parse("now-5m"))
print(dateparser.parse("1970-01-01T00:00:00"))
print(dateparser.parse("1970/01/01T00:00:00"))
print(type(dateparser.parse("1970/01/01T00:00:00")))
dt = dateparser.parse("now-5m")
print(dt - relativedelta(minutes=10)) # equals to now -15 min
output::
2019-05-06 12:55:54.074220
2019-05-06 12:55:24.076534
2019-05-06 12:50:54.077823
1970-01-01 00:00:00
1970-01-01 00:00:00
2019-05-06 12:40:54.087881
you can read more at :: https://pypi.org/project/dateparser/
Upvotes: 5
Reputation: 20490
You are looking at the datetime
module of python. We use the datetime.strptime to convert the datetime string to datetime object, then use datetime.timedelta to do the operations like subtracting minutes and hours
import datetime
#Convert the string to datetime object
dt = datetime.datetime.strptime('1970-01-01T00:00:00', '%Y-%m-%dT%H:%M:%S')
#Subtract 5 minutes from the time
print(dt - datetime.timedelta(minutes=5))
#Subtract 1 hour from the time
print(dt - datetime.timedelta(hours=1))
The output will be 1969-12-31 23:55:00
In addition, you can also look at dateutil library which gives you dateutil.relativedelta in which you can provide years, days and months as well
import datetime
from dateutil.relativedelta import relativedelta
#Convert the string to datetime object
dt = datetime.datetime.strptime('1970-01-01T00:00:00', '%Y-%m-%dT%H:%M:%S')
#Subtract 5 minutes from the time
print(dt - relativedelta(days=5))
#Subtract 1 hour from the time
print(dt - relativedelta(month=1))
The output here will be
1969-12-27 00:00:00
1970-01-01 00:00:00
Now in order to avoid having to provide hours,minutes,days
etc as argument, you can convert all of them into a standard format (say seconds
and minutes
), and then use that.
So e.g if you want to subtract an hour and 5 minutes, you can just subtract 65 minutes
#Subtract 65 minutes from the time
print(dt - relativedelta(minutes=5))
#Subtract 1 hour and 5 minutes from the time
print(dt - relativedelta(hours=1, minutes=5))
Maybe you can write a function which gets a string like 5m
, 1h
, 2d
as input and give you minutes in return
def get_minutes(s):
if 'm' in s:
return int(s.replace('m',''))
elif 'h' in s:
return int(s.replace('h', ''))*60
elif 'd' in s:
return int(s.replace('d', ''))*1440
print(get_minutes('5m'))
#5
print(get_minutes('1h'))
#60
print(get_minutes('2d'))
#2880
Then you can use this in your timedelta
or relativedelta
function, perhaps expand this function to include more types of time strings
Similarly you can come up with a standard time which you convert all your times to, and then use that in the argument!
Upvotes: 3
Reputation: 1858
An idea for 2), 3) cases, to be tested and refined:
import pandas as pd
import datetime as dt
def fuzzy_parse(t): return dt.datetime.utcnow() - pd.Timedelta(t.split("-")[1])
print(dt.datetime.utcnow(), fuzzy_parse("now-1s"))
print(dt.datetime.utcnow(), fuzzy_parse("now-1m"))
print(dt.datetime.utcnow(), fuzzy_parse("now-1h"))
print(dt.datetime.utcnow(), fuzzy_parse("now-1d"))
print(dt.datetime.utcnow(), fuzzy_parse("now-1w"))
Output:
2019-05-06 11:24:12.347120 2019-05-06 11:24:11.347125
2019-05-06 11:24:12.347388 2019-05-06 11:23:12.347391
2019-05-06 11:24:12.347554 2019-05-06 10:24:12.347557
2019-05-06 11:24:12.347710 2019-05-05 11:24:12.347712
2019-05-06 11:24:12.347863 2019-04-29 11:24:12.347865
Upvotes: 1