Converting timestamp with milliseconds to datetime in python - python

ALL,
I'm trying to get a statistical data of the file. Doing so gives me following:
atime - datetime timestamp representation
atime_nano - nano-seconds resolution in addition to a_time.
What I'd like to do is to convert atime.atime_nano to a datetime variable in Python.
So if I have:
atime = 1092847621L
atime_nano = 7100000L
I'd like to convert it to the datetime object in python that will have correct date with the milliseconds.
How can I do that?
Thank you.

Datetimes can have microseconds (1 microsecond = 1000 nanoseconds)
You can do the following for your example:
dt = datetime.fromtimestamp(1092847621L).replace(microsecond = 7100000L/1000)

Related

what is this time format: XX:XX.X, and how to transfer it to usual year-month-day-hour-minute-second in Python

I have some date data like:
46:53.4
57:00.0
51:50.9
53:13.9
What is this time format? And how to transfer it to the usual year-month-day-hour-minute-second in Python?
Code:
import datetime
#Input Date String
t = "46:53.4"
#Return a datetime corresponding to date string
dateTimeObject = datetime.datetime.strptime(t, '%M:%S.%f')
print (dateTimeObject)
Result:
1900-01-01 00:46:53.400000
I suppose 46:53.4 means forty-six minutes and fifty-three-point-four seconds.

Python: Is there a way to subtract date time objects with the values written as "hours":"minutes":"seconds":"milliseconds"

I'm trying to convert strings into a date time object given the hours minutes seconds and milliseconds, so I can find the difference in time between the two strings. If there is a better way to do this please let me know, that'd be great.
the format of the date time object looks like this:
But when I tried subtracting two strings converted into date time objects the result wasn't correct. for example:
from datetime import datetime
format = '%H:%M:%S:%f'
x = '12:51:11:153'
time1 = datetime.strptime(x, format)
y = '13:51:11:153'
time2 = datetime.strptime(y, format)
difference = time2 - time1
print(difference)
"The print" = 0:00:00
Am I missing something for the format, or is there a special method to subtract datetime objects from each other? Also, is there an easier way to do this?
I think your solution is right. I can't reproduce that. This is my code and the output if I run it.
from datetime import datetime
import time
format = '%H:%M:%S:%f'
time1 = datetime.strptime("12:51:11:153", format)
time2 = datetime.strptime("13:51:11:153", format)
print((time2-time1).total_seconds())
--> 3600.0 seconds (type is float)
type(time2-time1) --> <class 'datetime.timedelta'>
time2-time1 --> datetime.timedelta(seconds=3600)

How to Converting Date String with timezone to timestamp milliseconds in Python 2.7

I have this date string in python "2016-12-12T00:00:00+01:00"
How do I convert the said date string with timestamp to timestamp in milliseconds so I can compute use the value so I can compute it?
Purpose:
to_timestamp_milliseconds("2017-12-12T00:00:00+01:00") + (10500 * 1000)
Easy
dt = dateutil.parser.parse("2017-12-12T00:00:00+01:00")
timestamp = int(time.mktime(dt.timetuple()))
now you got seconds, I guess you can convert it to milliseconds your self

Python - How to convert datetime data using toordinal considering the time

Let's assume that I have the following data:
25/01/2000 05:50
When I convert it using datetime.toordinal, it returns this value:
730144
That's nice, but this value just considers the date itself. I also want it to consider the hour and minutes (05:50). How can I do it using datetime?
EDIT:
I want to convert a whole Pandas Series.
An ordinal date is by definition only considering the year and day of year, i.e. its resolution is 1 day.
You can get the microseconds / milliseconds (depending on your platform) from epoch using
datetime.datetime.strptime('25/01/2000 05:50', '%d/%m/%Y %H:%M').timestamp()
for a pandas series you can do
s = pd.Series(['25/01/2000 05:50', '25/01/2000 05:50', '25/01/2000 05:50'])
s = pd.to_datetime(s) # make sure you're dealing with datetime instances
s.apply(lambda v: v.timestamp())
If you use python 3.x. You can get date with time in seconds from 1/1/1970 00:00
from datetime import datetime
dt = datetime.today() # Get timezone naive now
seconds = dt.timestamp()

python: convert date timestamp to epoch unix time and figure out number of days remaining?

I want to convert 2014-08-14 20:01:28.242 into a unix timestamp 245293529385 and subtract this by the current timestamp in order to figure out how many days have past and are ultimately remaining by subtracting this value from 14.
Scenario: user signs up and I want to count down the number of days remaining in their trial.
time.strptime to the rescue! Use the format string %Y-%m-%d %H:%M:%S.%f. For example:
import time
t = '2014-08-14 20:01:28.242'
ts = time.strptime(t, '%Y-%m-%d %H:%M:%S.%f')
timestamp = time.mktime(ts)
Now to convert it to a datetime (from: How do you convert a Python time.struct_time object into a datetime object? ):
from datetime import datetime
dt = datetime.fromtimestamp(timestamp)
There are two parts:
Convert input time string into datetime object
#!/usr/bin/env python
from datetime import datetime
dt = datetime.strptime('2014-08-14 20:01:28.242', '%Y-%m-%d %H:%M:%S.%f')
Convert datetime object to Unix time ("seconds since epoch")
The result depends on what time zone is used for the input time e.g., if the input is in UTC then the corresponding POSIX timestamp is:
timestamp = (dt - datetime(1970,1,1)).total_seconds()
# -> 1408046488.242
If your input is in the local timezone then see How do I convert local time to UTC in Python?

Categories

Resources