Time-date as an integer
Peter Otten
__peter__ at web.de
Tue Aug 24 03:48:10 EDT 2004
Charles Hixson wrote:
> This is a concept, not a finished program, and an extract from a class
> at that...so forgive any illegalities, but:
...but not the funny whitespace.
> import datetime;
> def calcNodeId(self):
> t = datetime.utcnow()
> val = t.year * 133920000000 + # 12 months
> t.month * 11160000000 + # 31 days
> t.hour * 3600000000 + # 60 minutes
> t.minute * 60000000 + # 60 seconds
> t.second * 1000000 + t.microsecond
> if val <= self._dTime:
> val = self._dTime + 1
> self._dTime = val
> return val
>
> This is the best that I've been able to come up with in getting a
> date-time as an integer. It feels like one of the time or date
> libraries should have a better solution, but if so, I haven't found it.
> Can anyone suggest a better approach?
Have you considered a more lightweight approach?
>>> import itertools
>>> class Tree:
... def __init__(self):
... self.calcNodeId = itertools.count().next
...
>>> tree = Tree()
>>> tree.calcNodeId()
0
>>> tree.calcNodeId()
1
>>> tree.calcNodeId()
2
>>>
If you need the datetime information, just store the datetime, too.
Peter
More information about the Python-list
mailing list