[issue13882] Add format argument for time.time(), time.clock(), ... to get a timestamp as a Decimal object

STINNER Victor report at bugs.python.org
Tue Jan 31 00:15:29 CET 2012


STINNER Victor <victor.stinner at haypocalc.com> added the comment:

> One possibility (still awkward IMO) would be to use the return type as
> the format specifier.

Yeah, I already thaught to this idea. The API would be:
 - time.time(format=float)
 - time.time(format=decimal.Decimal)
 - time.time(format=datetime.datetime)
 - time.time(format=?) # for timespec, but I don't think that we need timespec in Python which is a object oriented language, we can use better than low level strutures
 - os.stat(path, format=decimal.Decimal)
 - etc.

I have to write a function checking that obj is decimal.Decimal or datetime.datetime without importing the module. I suppose that it is possible by checking obj type (it must be a class) and then obj.__module__.

> This would at least require the user to import
> datetime or decimal before calling time() with corresponding
> format.

Another possibility is what I proposed before in the issue #11457: take a callback argument.
http://bugs.python.org/issue11457#msg143738

The callback prototype would be:

def myformat(seconds, floatpart, divisor):
    return ...

Each module can implements its own converter and time can provide some builtin converts (because I don't want to add something related to time in the decimal module for example).

But I don't really like this idea because it requires to decide the API of the low level structure of a timestamp (which may change later), and it doesn't really solve the issue of "import decimal" if the converter is in the time module.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13882>
_______________________________________


More information about the Python-bugs-list mailing list