Python timestamp in precision of milliseconds - python

I need to output a timestamp for a .csv file of the current time in milliseconds. Right now I have:
localTime = time.localtime(time.time())
now = time.localtime(time.time())
currTime = time.time()
now = time.strftime("\"%Y-%m-%d %H:%M:%S.%f\"", time.localtime(currTime))
doing it this way will output the timestamp in the following format:
"2017-05-09 10:13:33.%f" this obviously is not correct. Ive heard that time.time only goes as precise as a second, but have also heard that it can support microseconds. Can somebody clear this up for me or show me the proper way to format this code to get a timestamp in the needed format? (2017-05-09 10:13:33.100) for example

A quick solution would be:
t=time.time()
millis = int((t - int(t))*1000)

As you said, the problem is that time doesn't necessarily give you the precision you want[1]. datetime would be a better option:
from datetime import datetime
now = datetime.utcnow() # or datetime.now(your_timezone)
formatted = now.strftime("%Y-%m-%d %H:%M:%S.%f")
print(formatted)
[1] Both in python 2.x and 3.x, according to the docs:
Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

Related

Converting seconds to minutes using datetime.timedelta

I am having trouble understanding a simple timedelta function.
In the code below, it is supposed to convert total_travel_time provided in seconds to minutes. Can someone briefly explain the logic behind the (seconds = total_travel_time) part?
Is it telling python that the total_travel_time is in seconds?
How come python understands to convert it to minutes even without "minutes" somewhere in the code?
total_travel_time = df['Trip Duration'].sum()
print("Total travel time: {}".format(str(datetime.timedelta(seconds = total_travel_time))))
Your code has some parts that aren't really relevant to your question.
It boils down to this:
import datetime
n = 120
print(datetime.timedelta(seconds=n))
Note that even the str() bit is superfluous, since passing it to print() already requires conversion to string anyway
Result:
0:02:00
It's not converting anything to minutes, it's constructing a timedelta object, being given a number of seconds. In this case, that happens to include a number of minutes. But depending on the number of seconds, it could be anything.
When printing a timedelta, Python needs to turn it into a string of characters (because that's all we can read) and it chooses the standard HH:mm:ss format (unless it's more than a day, then it'll print the number of days separately).
To get the actual number of minutes, you'd actually start with the seconds (that are accessible directly on the timedelta object):
print(datetime.timedelta(seconds=n).seconds // 60)

Python: UTC vs local timestamp

Why the followings return different timestamp? Is it because datetime.utcnow() doesn't have a timezone? It looks to me that tzinfo=utc is redudant, so I am probably not getting what is utcnow() and how an UTC number could not have a timezone. I guess there is a reason, so please enlight me :)
from datetime import datetime
from pytz import utc
local_seconds = int(datetime.utcnow().timestamp())
utc_seconds = int(datetime.utcnow().replace(tzinfo=utc).timestamp())
My goal is to get the UTC timestamp. It looks like the first method returns the local timestamp (correct me if I am wrong)
EDIT:
Where I live the timezone is GMT-5. In fact:
(utc_seconds-local_seconds)/3600 # is equal to -5.0
Following two statements would always return different result.
local_seconds = int(datetime.utcnow().timestamp())
utc_seconds = int(datetime.utcnow().replace(tzinfo=utc).timestamp())
Output:
1585584790
1585604590
You ask why? Because, by the time first statement executes, there is some time spent during execution and now the second statement would fetch you different result because datetime.utcnow() for 2nd statement has changed.
What I assume is, you want to see if both operations would give the same result or not? They definitely would have given the same results :
Had you provided them the same input?
Had you performed the similar operation from a common library.
To solve 1. change your code like this.
same_time_input = datetime.utcnow()
local_seconds = int(same_time_input.timestamp())
utc_seconds = int(same_time_input.replace(tzinfo=utc).timestamp())
Still the output would not be same, because you are using an external library, and the replace function is not working as you expected.
If you printout the tzinfo from same_time_input, you would see that it doesn't have any timezone info reason of which can be read here. --> Why does datetime.datetime.utcnow() not contain timezone information?
print(same_time_input.tzinfo)
Now, you are trying to give it a timezone info using a separate library which has different implementation internally resulting in slightly off results.

Python - Timedelta to datetime with miliseconds -> "HH:MM:SS"

On this problem I keep getting stuck when trying several options provided.
In simple words, I'm running a script that has a starting time (several actually, based on different criteria) and in a loop I want to display the running time of that criteria in a JSON and put it in a program (using requests) that is updated every time the loop passes one of the criteria.
I was doing that by simply running:
starting_time = datetime.now() #but just a bit earlier in the script
now = datetime.now()
running_time = now-starting_time
This running_time is then used as a variable in a JSON, but that needs to be in the format of 'HH:MM:SS' else my requests doesn't allow me to put. Which caused the problem for me, because it isn't possible to use strftime on a timedelta.
The timedelta might be based on miliseconds, but those are fine as "00:00:00"... but that caused me problems when trying to convert the timedelta to string first and then convert it back to a regular datetime.
What am I missing?
A possible workaround would be:
starting_time = datetime.now()
now = datetime.now()
running_time = now-starting_time
x = datetime.timedelta(seconds=running_time.seconds)
result = str(x)
if result[1] == ":":
result = "0"+result
print(result)
Here line 4 makes sure that x only has the seconds and ignores the miliseconds of running_time. Then we add a zero at the beginning in case needed.
But also see comment to better understand timedelta.

Python datetime precision

I have a Google App Engine datetime property which I populate with x.date = datetime.datetime.now(). I do a lot of comparisons between dates, and after much debugging, it turns out my client device sends dates out with less precision than a Python date, which caused a terrible mess.
Here is what Python generates:
2012-08-28 21:36:13.158497 with datetime.datetime.now(), but what I want is 2012-08-28 21:36:13.158000 (notice the three zeros at the end.)
How can I achieve this? (keep in mind, I'm not trying to format strings or anything. I want to format a date object.)
I guess one way would be to format it into a string with desired precision, like this:
dateString = date.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
and then back to a date object. But there's got to be a better way.
dt = dt.replace(microsecond = (dt.microsecond / 1000) * 1000)
This will truncate the last 3 digits. Proper rounding is a little more complicated due to the possibility that it might round to 1000000 microseconds.

Convert Unix Timestamp to human format in Django with Python

I'd like to a convert unix timestamp I have in a string (ex. 1277722499.82) into a more humanized format (hh:mm:ss or similar). Is there an easy way to do this in python for a django app? This is outside of a template, in the model that I would like to do this. Thanks.
edit
I'm using the python function time.time() to generate the timestamp. According to the doc:
time.time()
Return the time as a floating point number expressed in seconds
since the epoch, in UTC. Note that
even though the time is always
returned as a floating point number,
not all systems provide time with a
better precision than 1 second. While
this function normally returns
non-decreasing values, it can return a
lower value than a previous call if
the system clock has been set back
between the two calls.
import datetime
datestring = "1277722499.82"
dt = datetime.datetime.fromtimestamp(float(datestring))
print(dt)
2010-06-28 11:54:59.820000

Categories

Resources