This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.today() truncates microseconds
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, tim.peters
Priority: normal Keywords:

Created on 2003-01-02 14:23 by amaury.forgeotdarc, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Messages (3)
msg13805 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2003-01-02 14:23
While exploring the new datetime module, I got results 
like:
>>> datetime.datetime.today()
datetime.datetime(2003, 1, 2, 15, 14, 51, 480999)

Shouldn't the microseconds be 637000 ?
While not very important, this can lead you to believe 
that the timestamp is accurate to 1e-6 seconds, 
whereas only milliseconds are relevant (on W2K).

since today() is equivalent to fromtimestamp(time.time()),
I explored the results of time.time():

>>> time.time(),datetime.datetime.today()
returns:
(1041516891.4809999, 
datetime.datetime(2003, 1, 2, 15, 14, 51, 480999))

It seems that the float returned by time.time is truncated 
to 1e-6. I suggest that it should be rounded instead, so 
that the last digit (which is significant) is taken into 
account.

Looking into the code, I think that the code to change is 
in datetimemodule.c: the function 
datetime_from_timestamp could add 5e-7 to the "us" 
variable before casting it to int.
msg13806 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2003-01-02 15:18
Logged In: YES 
user_id=389140

Sorry for the typo:
in my sample, I would like the microseconds to show as 
481000 of course.
msg13807 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-01-02 16:37
Logged In: YES 
user_id=31435

I agree it's an irritation, and changed the code to round to the 
nearest microsecond instead.  Thanks for the report!  Fixed in

Modules/datetimemodule.c; new revision: 1.25
History
Date User Action Args
2022-04-10 16:06:05adminsetgithub: 37686
2003-01-02 14:23:25amaury.forgeotdarccreate