Time-date as an integer
Roel Schroeven
rschroev_nospam_ml at fastmail.fm
Tue Aug 24 03:50:42 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:
> 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?
int(time.time())
That gives only second precision, though.
Calculations such as the one above can be simplified somewhat by grouping:
val = t.microsecond +
1000000 * (t.second +
60 * (t.minute +
60 * (t.hour +
24 * (t.day + # (you forgot this one)
31 * (t.month +
12 * year)))))
Or with less indentation, I used it here just to make the grouping clear.
--
"Codito ergo sum"
Roel Schroeven
More information about the Python-list
mailing list