How to convert HH:MM:SS time to decimal in Python - python

Assuming that I have the following date and time (June 12, 2017 07:10:43.340) that I stored in a dateime object. From that I have a timestamp object, as follows:
from datetime import *
timeH = datetime(2017, 6,12, 7, 10, 43, 340)
timeHstamp = timeH.timestamp()
What I want to obtain is a decimal representation of the time only. That is
7.178 which I calculated, manually, by dividing seconds over 60 (43.34/60), add it the minutes (10+0.7223), divide the total minutes by 60 (10.7223/60), and then add to hours (7+0.178). I know I can write a custom function that would output exactly what I need. However, I am wondering if there is any available methods that can achieve this.
I have searched stackoverflow and other sites; but, there doesn't seem to be function that does this in Python. This post provide something similar but in Matlab Convert hh:mm time to decimal time (Matlab)

One way using datetime.combine:
(datetime.combine(date.min, timeH.time()) - datetime.min).total_seconds()/3600
Output:
7.178611205555555

Related

Convert epoch time (minute since reference time) to human readable time format

The question in the title seems to be familiar as I could see lot of example blog posts and SO posts. However, I couldn't find a question similar to the issue I am facing. I have a netcdf file in which variable time has a single data value 10643385. The unit of this time variable is minutes since 2000-01-01 00:00:00 which is different from many examples I found on the internet.I am also aware of the fact that actual value of time is 27-03-2020 05:45. My query is that how do I get this epoch value int to the date time format like `27-03-2020 05:45'. Here is the sample code I have been trying which results in the reference datetime rather than actual datetime of the file:-
print(datetime.datetime.fromtimestamp(int(epoch_time_value)).strftime('%Y-%m-%d %H:%M:%S'))
The above single line of code result in 1970-05-04 09:59:45. Can some one help me to get the correct date.
import datetime
t = datetime.datetime(2000, 1, 1) + datetime.timedelta(minutes=10643385)
outputs
datetime.datetime(2020, 3, 27, 5, 45)
Python epoch time is in seconds, so we must first convert this to seconds by multiplying by 60.
Python epoch time starts on 1, Jan, 1970. Since netcdf starts on 2000-01-01, we must adjust by adding the amount of seconds from 1970 to 2000 (which happens to be 946684800).
Putting these together we get:
>>> import datetime
>>> epoch_time_value = 10643385
>>> _epoch_time_value = epoch_time_value * 60 + 946684800
>>> print(datetime.datetime.fromtimestamp(int(_epoch_time_value)).strftime('%Y-%m-%d %H:%M:%S'))
2020-03-26 22:45:00
Then, there may be some shift (possibly +/- 12 hours) based on timezone, so make sure the timezones in your calculations are synced when you do this!

Human readable delta time text to Python `timedelta`

I have seen many examples on how to parse a human readable text containing a date/time to datetime structure or even seconds since "Epoch".
A few Pyhton libraries (e.g. parsedatetime or dateparser claim to be able to parse relative date/times (like "1min 47 seconds ago") but the end result is always anchored to a specific date/time.
Example using two mentioned libraries:
sdate="1 min 37 seconds ago"
dateparser.parse(sdate)
datetime.datetime(2019, 8, 19, 17, 20, 29, 325230)
pdtCal.parse(sdate)
(time.struct_time(tm_year=2019, tm_mon=8, tm_mday=19, tm_hour=17, tm_min=22, tm_sec=49, tm_wday=0, tm_yday=231, tm_isdst=-1), 2)
What I need, though, is something as simple as a timedelta object, but from what I could learn, the best I can do is to compute the timedelta by subtracting the parsed datetime from current time.
Obviously, this is not the same since I will be adding a sampling error (datetime.datetime.now() is running at a different time as the parser run).
So I ask, is there a simple yet relieble way in Python to parse this delta time text directly into a timedelta object or a scalar value (e.g seconds count)?
Thanks!
There is a way to specify "anchor date" in dateparser using settings:
In [1]: from dateparser import parse
In [2]: from datetime import datetime
In [3]: anchor_date = datetime(2020, 1, 1)
In [4]: parsed_date = parse('1 min 37 seconds ago', settings={'RELATIVE_BASE': anchor_date})
In [5]: parsed_date - anchor_date
Out[5]: datetime.timedelta(days=-1, seconds=86303)
Using the same date as relative base and in delta calculation ensures precise results.

Convert 18-digit LDAP/FILETIME timestamps to human readable date

I have exported a list of AD Users out of AD and need to validate their login times.
The output from the powershell script give lastlogin as LDAP/FILE time
EXAMPLE 130305048577611542
I am having trouble converting this to readable time in pandas
Im using the following code:
df['date of login'] = pd.to_datetime(df['FileTime'], unit='ns')
The column FileTime contains time formatted like the EXAMPLE above.
Im getting the following output in my new column date of login
EXAMPLE 1974-02-17 03:50:48.577611542
I know this is being parsed incorrectly as when i input this date time on a online converter i get this output
EXAMPLE:
Epoch/Unix time: 1386031258
GMT: Tuesday, December 3, 2013 12:40:58 AM
Your time zone: Monday, December 2, 2013 4:40:58 PM GMT-08:00
Anyone have an idea of what occuring here why are all my dates in the 1970'
I know this answer is very late to the party, but for anyone else looking in the future.
The 18-digit Active Directory timestamps (LDAP), also named 'Windows NT time format','Win32 FILETIME or SYSTEMTIME' or NTFS file time. These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp and LastPwdSet. The timestamp is the number of 100-nanoseconds intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC.
Therefore, 130305048577611542 does indeed relate to December 3, 2013.
When putting this value through the date time function in Python, it is truncating the value to nine digits. Therefore the timestamp becomes 130305048 and goes from 1.1.1970 which does result in a 1974 date!
In order to get the correct Unix timestamp you need to do:
(130305048577611542 / 10000000) - 11644473600
Here's a solution I did in Python that worked well for me:
import datetime
def ad_timestamp(timestamp):
if timestamp != 0:
return datetime.datetime(1601, 1, 1) + datetime.timedelta(seconds=timestamp/10000000)
return np.nan
So then if you need to convert a Pandas column:
df.lastLogonTimestamp = df.lastLogonTimestamp.fillna(0).apply(ad_timestamp)
Note: I needed to use fillna before using apply. Also, since I filled with 0's, I checked for that in the conversion function about, if timestamp != 0. Hope that makes sense. It's extra stuff but you may need it to convert the column in question.
I've been stuck on this for couple of days. But now i am ready to share really working solution in more easy to use form:
import datetime
timestamp = 132375402928051110
value = datetime.datetime (1601, 1, 1) +
datetime.timedelta(seconds=timestamp/10000000) ### combine str 3 and 4
print(value.strftime('%Y-%m-%d %H:%M:%S'))

How to convert time object to minutes in python

I have an output from a database(postgreSQL) in python. The time is encoded as timestamptz. We need to be able to convert the time into just minutes.
2015-07-09 13:45:08.266520+00:00
For example this would be 825 minutes.
I'm having issues with the datetime.datetime formating, I just need to be able to drop everything but hours and minutes and then convert to minutes.
time_format = '%H:%M'
time1 = datetime.strptime('2015-07-09 13:45:08.266520+00:00', time_format)
So I'm trying to sort a big list of times into certain intervals. I need these times to be in just minutes in order to do any math on them, I do not want to have to mess with time anymore. Thats why I'm trying to just covert them into minutes by taking the hours * 60 + minutes. Since this is in a time format I cannot do any of that.
Substract the DateTime from a DateTime with the the reference point in time, extract the total_seconds from the resulting TimeDelta and divide by 60.
(You did not give any code. So I can't give any code based on your's.)
dateutil takes a lot of the headache out of parsing a ISO 8601 stamp:
>>> import dateutil.parser
>>> dateutil.parser.parse('2015-07-09 13:45:08.266520+00:00')
datetime.datetime(2015, 7, 9, 13, 45, 8, 266520, tzinfo=tzutc())
Since you want the answer to be '825 minutes', algebraically, you must be seeking an offset from an epoch mark:
>>> import datetime as dt
>>> mark=dateutil.parser.parse('2015-07-09 13:45:08.266520+00:00')-dt.timedelta(minutes=825)
>>> mark
datetime.datetime(2015, 7, 9, 0, 0, 8, 266520, tzinfo=tzutc())
So now use mark as the basis to calculate minute offsets. (You probably mean to ignore microseconds in the base time mark)
I understand what you're trying to do. So you want to convert the timestamp you're getting from your DB into minutes. Basically 0:00 would be 0, 1:00 would be 60 and so on.
Timestamps are standard, so a dirty hack you could do is just pull the indices of the relevant information that you want and go from there.
time = "2015-07-09 13:45:08.266520+00:00"
data = time[11:18]
print data //Will be 13:45:08
Then you can do data operations from there.

Manipulation with datetime in python

I am developing an application in python where I need to calculate time elapsed and take appropriate action.
I have two times as by default provided and current. I want compare the difference with some other time which is provided in string format.
I have tried following:
d1 = datetime.datetime(2015, 1, 21, 9, 54, 54, 340000)
print d1
d2 = datetime.datetime.now()
print d2
print d2 - d1
d3 =datetime.datetime.strptime("22:17:46.476000","%H:%M:%S.%f")
print d3
Output of the above program is :
2015-01-21 09:54:54.340000
2015-01-21 22:28:45.070000
12:33:50.730000
1900-01-01 22:17:46.476000
Here time difference is' 12:33:50.730000' and I want to compare it with '22:17:46.476000' which is string.
As I tried to convert string '22:17:46.476000' to time I got year as 1990. I want only time as '22:17:46.476000'. So I can compare these two time using timedelta.
How to get rid of year 1990-01-01. I want only '22:17:46.476000'.
I tried using time as time.strptime("22:17:46.476000","%H:%M:%S.%f") but it give output as time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=22, tm_min=17, tm_sec=46, tm_wday=0, tm_yday=1, tm_isdst=-1)
Thanks
As it stands, you are comparing two fundamentally different grandeurs.
When you subtract one Datetime object from another you get a "timedelta" object - that is a period of time, which can be expressed in days, hours, seconds, or whatever time unit.
Datetime objects on the other hand mark an specific point in time. When you parse the time-only string to a Datetime, since no year, month or day is specified, they get the default values of 1900-01-01.
So, since the value you want to compare the interval with is a time interval, not an specific point in time, that is what you should have on the other hand of the comparison.
The easiest way to do that from where you are is indeed to subtract, from your parsed object, the midnight of 1900-01-01 - and then you have a "timedelta" object, with just that duration. However, note that this is a hack, and as soon as the time interval you need to parse against is larger than 24h it will break (strptime certainly won't parse "30:15:..." as the 6th hour of 1901-01-02)
So, you'd better break apart and build a timedelta object from scratch for your string:
hour, min, sec = "22:17:46.476000".split(":")
d3 = datetime.timedelta(hours=int(hour), minutes=int(minutes), seconds=float(sec))
Given a datetime d3, you could obtain the time (without the date) using the time method:
>>> d3.time()
datetime.time(22, 17, 46, 476000)
You could then compare this time with d2.time():
>>> d2.time() > d3.time()
False
Alternatively, you could use combine to build a datetime out of d2's date and d3's time:
import datetime as DT
d1 = DT.datetime(2015, 1, 21, 9, 54, 54, 340000)
d2 = DT.datetime.now()
d3 = DT.datetime.strptime("22:17:46.476000","%H:%M:%S.%f")
d3 = DT.datetime.combine(d2.date(), d3.time())
print(d3)
# 2015-01-21 22:17:46.476000
You can then compare the two datetimes: d2 and d3. For example:
>>> d2 < d3
True
>>> (d3-d2).total_seconds()
36664.503375
The difference between two datetimes is a duration represented as a datetime.timedelta and not a datetime, so you can't format it using strftime.
Instead of using strptime to parse your value for d3, you should construct a datetime.timedelta directly. See the documentation for its constructor.
If you really need to parse d3 from a string, then you can either do the parsing yourself or use the python-dateutil package.
If I understand you correctly, 22:17:46.476000 doesn't represent a time but a time delta (because you want to compare it to another time delta).
If so, what you really need is to construct a datetime.timedelta object from your string. Unfortunately, it looks like there is no built-in way to do it, however, here you may find some recipes.

Categories

Resources