Extended date and time

J

Jeremy Sanders

Hi - I need to add support to a program for dates and times. The built-in
Python library seems to be okay for many purposes, but what I would like
would be Unix epoch style times (seconds relative to some date), covering a
large period from the past to the future. What would be nice would be a
library which can take floating point seconds from an epoch.

Does anyone know of a library which can convert from human style dates and
times to a floating point epoch and back again? I expect I could fudge the
fractional seconds with the built-in library, but I can't see how to get
dates in the past.

Thanks, Jeremy.
 
C

Colin J. Williams

Adam said:
The "time" module in the standard library does epoch, and conversions.

Get current local time in seconds since epoch (1970):

1194790069.33

Convert to a struct_time object for conversions:

(2007, 11, 11, 8, 7, 49, 6, 315, 0)

Make it a readable string:

'Sun 11/11/2007, 08:07:49 AM'

Convert string back into a struct_time object, then seconds again:

1194790069.0

... etc. If you're starting the other direction, change the format
string passed to strptime to match the pattern of your
existing strings. The standard docs for the time module has all the
details.

- Adam
What about November 5, 1605 ?

Colin W.
 
D

D.Hering

Hi - I need to add support to a program for dates and times. The built-in
Python library seems to be okay for many purposes, but what I would like
would be Unix epoch style times (seconds relative to some date), covering a
large period from the past to the future. What would be nice would be a
library which can take floating point seconds from an epoch.

Does anyone know of a library which can convert from human style dates and
times to a floating point epoch and back again? I expect I could fudge the
fractional seconds with the built-in library, but I can't see how to get
dates in the past.

Thanks, Jeremy.

Have you looked at mx.DateTime:
http://www.egenix.com/products/python/mxBase/mxDateTime/

In matplotlib, I also use their Dates modules functions for
conversions (see the near bottom of the page):
http://matplotlib.sourceforge.net/matplotlib.dates.html

In the scipy sandbox, you can also build a package called
'TimeSeries':
http://www.scipy.org/SciPyPackages/TimeSeries

I also have trouble with date/times with whats available. Off the top
of my head... converting a numpy array of epochs to some datetime
object and back.

If I had the time I'd contribute additional functionality to Pierre's
and Matt's TimeSeries module (the one in scipy).

-dieter
 
D

D.Hering

On Nov 10, 10:37 am, Jeremy Sanders <jeremy

I also have trouble with date/times with whats available. Off the top
of my head... converting a numpy array of epochs to some datetime
object and back.

If I had the time I'd contribute additional functionality to Pierre's
and Matt's TimeSeries module (the one in scipy).

-dieter

I made a request for this to Pierre and Matt on the scipy-user list.
 
J

John Machin

Hi - I need to add support to a program for dates and times. The built-in
Python library seems to be okay for many purposes, but what I would like
would be Unix epoch style times (seconds relative to some date), covering a
large period from the past to the future. What would be nice would be a
library which can take floating point seconds from an epoch.

Does anyone know of a library which can convert from human style dates and
times to a floating point epoch and back again? I expect I could fudge the
fractional seconds with the built-in library, but I can't see how to get
dates in the past.

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

Jeremy Sanders

John said:
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?

Thanks

Jeremy
 
J

John Machin

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
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top