Time-date as an integer

C

Charles Hixson

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?
 
P

Peter Otten

Charles said:
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?
.... def __init__(self):
.... self.calcNodeId = itertools.count().next
....
If you need the datetime information, just store the datetime, too.

Peter
 
R

Roel Schroeven

Charles said:
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.
 
B

Beeyah

Charles Hixson said:
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?

Your indentation is... interesting. Have you considered timestamps?
 
G

Grant Edwards

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?

Don't forget about months with lengths other than 31.

Don't forget about leap years.

Don't forget that 2000 was a leap year but 1900 and 2100 aren't.

Don't forget about leap-seconds.

Don't forget about calendar discontinuities (which occurred at
different places in different locales).
 
J

Jeff Lindholm

import datetime;
def calcNodeId(self):
t = datetime.utcnow()
val = ( (t.toordinal() * 24 * 60 * 60) + (t.hour * 60 * 60) + (t.minute
* 60) + t.second ) * 1000000
if val <= self._dTime:
val = self._dTime + 1
self._dTime = val
return val

This should hadle your leap years. You will of course loose leap seconds,
but I honestly not sure where you get those.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top