Re: Extended date and time



On Nov 12, 8:46 pm, Jeremy Sanders <jeremy
+complangpyt...@xxxxxxxxxxxxxxxxx> wrote:
John Machin wrote:
What does "dates in the past" mean?? Please be more specific about the
earliest date that you want to be able to handle. Python's datetime
starts at 0001-01-01. Somebody mentioned the time module, which is
implementation-dependent but typically starts at 1970-01-01 .

What functionality do you need, other than two-way conversion between
days_since_epoch and (proleptic Gregorian) date/time?

I want to convert between seconds from the epoch (let's say 1970 in floating
point) and date and time. I also want it to work across all platforms.

Is there any way to convert a datetime into seconds from a certain date? Is
the most robust way of doing it just to subtract two datetime objects and
turn the timedelta into a floating point number?


That seems to be robust enough:

C:\junk>type epoch.py
import datetime
now = datetime.datetime(2007, 11, 12, 21, 20, 39, 859123)
for epoch_year in (1, 1970, 2070):
EPOCH = datetime.datetime(epoch_year, 1, 1)
print "\nepoch", repr(EPOCH)
diff = now - EPOCH
print "diff", repr(diff)
sdiff = diff.microseconds / 1000000. \
+ diff.seconds + diff.days * 24. * 3600.
print "sdiff", repr(sdiff)
roundtrip = EPOCH + datetime.timedelta(seconds=sdiff)
print "roundtrip", repr(roundtrip)
C:\junk>epoch.py

epoch datetime.datetime(1, 1, 1, 0, 0)
diff datetime.timedelta(732991, 76839, 859123)
sdiff 63330499239.859123
roundtrip datetime.datetime(2007, 11, 12, 21, 20, 39, 859123)

epoch datetime.datetime(1970, 1, 1, 0, 0)
diff datetime.timedelta(13829, 76839, 859123)
sdiff 1194902439.859123
roundtrip datetime.datetime(2007, 11, 12, 21, 20, 39, 859123)

epoch datetime.datetime(2070, 1, 1, 0, 0)
diff datetime.timedelta(-22696, 76839, 859123)
sdiff -1960857560.140877
roundtrip datetime.datetime(2007, 11, 12, 21, 20, 39, 859123)

C:\junk>

Cheers,
John

.



Relevant Pages

  • Re: converting a timezone-less datetime to seconds since the epoch
    ... We have a bunch of datetime objects that have tzinfo=None. ... We want to turn them into float timestamps in seconds since the epoch. ... def timestamp: ...
    (comp.lang.python)
  • Re: event update
    ... > (succession through their dateTime value). ... > On the other hand, if you just need the SUM up to the datetime, a running ... >>I have a feild that I want to be updated from another feild in a form, ... >> way the current count of the next week starts out with Diff added into it. ...
    (microsoft.public.access.forms)
  • converting a timezone-less datetime to seconds since the epoch
    ... We have a bunch of datetime objects that have tzinfo=None. ... We want to turn them into float timestamps in seconds since the epoch. ... def check: ...
    (comp.lang.python)
  • Re: Date EpochSeconds conversion
    ... represents your Epoch ... add it to the epoch DateTime to get the DateTime. ... // Assume Epoch is a DateTime instance representing the Epoch. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Date EpochSeconds conversion
    ... Do you have any clue on initial set epoch... ... you want to have a DateTime value that ... >> Regards, ...
    (microsoft.public.dotnet.languages.csharp)