What I am trying to do is to subtract 7 hours from a date. I searched stack overflow and found the answer on how to do it here. I then went to go read the documentation on timedelta because I was unable to understand what that line in the accepted answer does, rewritten here for ease:
from datetime import datetime
dt = datetime.strptime( date, '%Y-%m-%d %H:%M' )
dt_plus_25 = dt + datetime.timedelta( 0, 2*60*60 + 30*60 )
Unfortunately, even after reading the documentation I still do not understand how that line works.
What is the timedelta line doing? How does it work?
Additionally, before I found this stackoverflow post, I was working with time.struct_time tuples. I had a variable tm:
tm = time.strptime(...)
I was simply accessing the hour through tm.tm_hour and subtracting seven from it but this, for obvious reasons, does not work. This is why I am now trying to use datetime. tm now has the value
tm = datetime.strptime(...)
I'm assuming using datetime is the best way to subtract seven hours?
Note: subtracting seven hours because I want to go from UTC to US/Pacific timezone. Is there a built-in way to do this?
What is the timedelta line doing? How does it work?
It creates a timedelta object.
There are two meanings of "time".
"Point in Time" (i.e, date or datetime)
"Duration" or interval or "time delta"
A time delta is an interval, a duration, a span of time. You provided 3 values.
0 days.
2*60*60 + 30*60 seconds.
timedelta() generates an object representing an amount of timeāthe Greek letter delta is used in math to represent "difference". So to compute an addition or a subtraction of an amount of time, you take the starting time and add the change, or delta, that you want.
The specific call you've quoted is for generating the timedelta for 2.5 hours. The first parameter is days, and the second is seconds, so you have (0 days, 2.5 hours), and 2.5 hours in seconds is (2 hours * 60 minutes/hour * 60 seconds/minute) + (30 minutes * 60 seconds / minute).
For your case, you have a negative time delta of 0 days, 7 hours, so you'd write:
timedelta(0, -7 * 60 * 60)
... or timedelta(0, -7 * 3600) or whatever makes it clear to you what you're doing.
Note: subtracting seven hours because I want to go from UTC to US/Pacific timezone. Is there a built-in way to do this?
Yes there is: datetime has built-in timezone conversion capabilities. If you get your datetime object using something like this:
tm = datetime.strptime(date_string, '%Y-%m-%d %H:%M')
it will not have any particular timezone "attached" to it at first, but you can give it a timezone using
tm_utc = tm.replace(tzinfo=pytz.UTC)
Then you can convert it to US/Pacific with
tm_pacific = tm_utc.astimezone(pytz.all_timezones('US/Pacific'))
I'd suggest doing this instead of subtracting seven hours manually because it makes it clear that you're keeping the actual time the same, just converting it to a different timezone, whereas if you manually subtracted seven hours, it looks more like you're actually trying to get a time seven hours in the past. Besides, the timezone conversion properly handles oddities like daylight savings time.
To do this you will need to install the pytz package, which is not included in the Python standard library.
Related
I am using datetime.strptime() to convert a string containing time and date from a sensor into a datetime object.
The code sometimes fails. Minimal example:
datetime.strptime('1/9/2021 24:01:53', '%d/%m/%Y %H:%M:%S')
Output error:
ValueError: time data '1/9/2021 24:01:53' does not match format '%d/%m/%Y %H:%M:%S'
I am guessing this has to do with the fact that the time is more than 23:59:59 - which seems to me a non-realistic time (I would think that 1/9/2021 24:01:53 could potentially be 2/9/2021 00:01:53 - a time format which I have never seen).
Is this a non-standard way of representing time or possibly a hardware/software issue with the sensor acquisition system? If it is a different way of representing time, how can I convert it to a standard datetime object?
Kind regards,
D.F.
If the hour exceeds 23 in a variable representing time, a good option is to create a timedelta from it, which you can then add to a datetime object. For given example that might look like
from datetime import datetime, timedelta
def custom_todatetime(s):
"""
split date/time string formatted as 'DD/MM/YYYY hh:mm:ss' into date and time parts.
parse date part to datetime and add time part as timedelta.
"""
parts = s.split(' ')
seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(parts[1].split(':'))))
return datetime.strptime(parts[0], "%d/%m/%Y") + timedelta(seconds=seconds)
s = '1/9/2021 24:01:53'
print(custom_todatetime(s))
# 2021-09-02 00:01:53
Note: conversion of hh:mm:ss to seconds taken from here - give a +1 there if helpful.
Say I have this time
00:46:19,870
where it represents 46h 19m and 870 is 870/1000 of a minute (I think I can just get rid of the last part). How do I convert this to seconds?
I've tried
time.strptime('00:46:19,870'.split(',')[0],'%H:%M:%S')
but realized that it wouldn't work as it's using a format different than mine.
How can I convert 00:46:19,870 to 2779?
You are close, you can still use the datetime you just need to calculate the time delta. What you really have isn't really a date but what appears to be a stopwatch time time. You can still strip the time from that and you will notice that Python uses a default year, month, and day. You can use that default to figure out the delta in seconds:
from datetime import datetime
DEFAULT_DATE = (1900, 1, 1)
stopwatch = datetime.strptime('00:46:19,870', '%H:%M:%S,%f')
a_timedelta = stopwatch - datetime(*DEFAULT_DATE)
seconds = a_timedelta.total_seconds()
print(seconds)
I have 2 timestamps derived from OOZIE in this format:
2019-07-20T16:34:45.000Z
and say
2020-08-20T16:20:15.000Z
How can I find the time difference in seconds / mins / days? Which python library can help? Any suggestions?
I don't know about any library, But you can do it yourself.
I am sharing a code that might work.
time_stamp1 = input("Time stamp1")
time_stamp2 = input("Time stamp2")
yrs = int(time_stamp1[:4]) - int(time_stamp2[:4])
mnths = int(time_stamp1[5:7]) - int(time_stamp2[5:7])
days = int(time_stamp1[8:10]) - int(time_stamp1[8:10])
hrs = int(time_stamp1[11:13]) - int(time_stamp2[11:13])
min = int(time_stamp1[14:16]) - int(time_stamp1[14:16])
sec = int(time_stamp1[17:19]) - int(time_stamp1[17:19])
ms = int(time_stamp1[20:23]) - int(time_stamp1[20:23])
print(yrs, mnths, days)
print(hrs, min, sec, ms)
You can use the abs() function if you don't want to know whether timestamp2 is older than timestamp 1 or not or vice versa.
You could use the datetime module.
First, use strptime to convert each of those timestamps into dates. The general format of it is:
from datetime import datetime
date1 = datetime.strptime("2019-07-20T16:34:45.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
date2 = datetime.strptime("2020-08-20T16:20:15.000Z","%Y-%m-%dT%H:%M:%S.%fZ")
(Note: the second argument could be inaccurate, depending on the specifics of OOZIE).
For info on how to construct a format/use strptime, see
this and this for an introduction to strptime.
Then, you could just do t_diff = date1-date2. This will return a timedelta object, with attributes for years, months, weeks, days, etc... microseconds). It also has a builtin .total_seconds() method.
Hope this helped!
I have been playing a bit to try to understand pandas timestamps and timedeltas. I like how you can operate with them, but when trying subtraction I found this a bit odd:
now = pd.Timestamp('now')
then = now - pd.to_timedelta('1h')
print (now - then)
print (then - now)
print ((now - then).seconds)
print ((then - now).seconds)
Results in:
0 days 01:00:00
-1 days +23:00:00
3600
82800
a) How should I understand this behavior?
b) Is there a way to have an absolute value of the difference of timestamps, the equivalent to abs()?
The reason for this seemingly strange/buggy behaviour is that the .seconds attribute of a timedelta (for pandas.Timedelta, but this is inherited from standard library's timedelta.timedelta) is very ambigous.
The timedelta is stored in 3 parts: days, seconds, microseconds (https://docs.python.org/2/library/datetime.html#timedelta-objects). So the seconds is the sum of hours, minutes and seconds (in seconds).
So there are 2 'strange' things that can lead to confusion:
When having a negative timedelta, you get -1 days +23:00:00 instead of -01:00:00. This is because only the days part can be negative. So a negative timedelta will always be defined as a negative number of days with adding hours or seconds again to get the correct value. So this gives you the +23h part.
The seconds is the sum of hours, minutes and seconds. So the +23:00:00 we get is equal to 82800 seconds.
Bottomline is, the .seconds attribute of a timedelta does not give you the seconds part neither the total seconds (timedelta converted to seconds). So in practice, I think you should almost never use it.
To obtain the timedelta in seconds, you can use the total_seconds method. If I define the negative difference to diff = then - now:
In [12]: diff
Out[12]: Timedelta('-1 days +23:00:00')
In [13]: diff.seconds
Out[13]: 82800
In [14]: diff.total_seconds()
Out[14]: -3600.0
I have a problem that seems really easy but I can't figure it out.
I want to achieve the following:
Time_as_string - time_now = minutes left until time as string.
I scrape a time from a website as a string, for example: '15:30'.
I want to subtract the current time from this to show how many minutes
are left untill the scraped time string.
I tried many things like strftime(), converting to unix timestamp, googling solutions etc.
I can make a time object from the string through strftime() but I can't subtract it from the current time.
What is the best way to achieve this?
from datetime import datetime
s = "15:30"
t1 = datetime.strptime(s,"%H:%M")
diff = t1 - datetime.strptime(datetime.now().strftime("%H:%M"),"%H:%M")
print(diff.total_seconds() / 60)
94.0
If '15:30' belongs to today:
#!/usr/bin/env python3
from datetime import datetime, timedelta
now = datetime.now()
then = datetime.combine(now, datetime.strptime('15:30', '%H:%M').time())
minutes = (then - now) // timedelta(minutes=1)
If there could be midnight between now and then i.e., if then is tomorrow; you could consider a negative difference (if then appears to be in the past relative to now) to be an indicator of that:
while then < now:
then += timedelta(days=1)
minutes = (then - now) // timedelta(minutes=1)
On older Python version, (then - now) // timedelta(minutes=1) doesn't work and you could use (then - now).total_seconds() // 60 instead.
The code assumes that the utc offset for the local timezone is the same now and then. See more details on how to find the difference in the presence of different utc offsets in this answer.
The easiest way is probably to subtract two datetimes from each other and use total_seconds():
>>> d1 = datetime.datetime(2000, 1, 1, 20, 00)
>>> d2 = datetime.datetime(2000, 1, 1, 16, 30)
>>> (d1 - d2).total_seconds()
12600.0
Note that this won't work if the times are in different timezones (I just picked January 1, 2000 to make it a datetime). Otherwise, construct two datetimes in the same timezones (or UTC), subtract those and use total_seconds() again to get the difference (time left) in seconds.