By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Extended date and time

P: n/a
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.

--
Jeremy Sanders
http://www.jeremysanders.net/
Nov 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Adam Pletcher wrote:
The "time" module in the standard library does epoch, and conversions.

Get current local time in seconds since epoch (1970):
>import time
now_secs = time.time()
print now_secs
1194790069.33

Convert to a struct_time object for conversions:
>now_struct = time.localtime(now_secs)
print now_struct
(2007, 11, 11, 8, 7, 49, 6, 315, 0)

Make it a readable string:
>now_string = time.strftime('%a %m/%d/%Y, %I:%M:%S %p', now_struct)
print now_string
'Sun 11/11/2007, 08:07:49 AM'

Convert string back into a struct_time object, then seconds again:
>now_struct2 = time.strptime(now_string, '%a %m/%d/%Y, %I:%M:%S %p')
print now_struct2
(2007, 11, 11, 8, 7, 49, 6, 315, -1)
>now2 = time.mktime(now_struct2)
print now2
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.
>

------------------------------------------------------------------------
*From:* py***************************************@python.o rg on behalf
of Jeremy Sanders
*Sent:* Sat 11/10/2007 9:37 AM
*To:* py*********@python.org
*Subject:* Extended date and time

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.

--
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list
Nov 11 '07 #2

P: n/a
On Nov 10, 10:37 am, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
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.

--
Jeremy Sandershttp://www.jeremysanders.net/
Have you looked at mx.DateTime:
http://www.egenix.com/products/pytho...se/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

Nov 11 '07 #3

P: n/a
On Nov 11, 4:46 pm, "D.Hering" <vel.ac...@gmail.comwrote:
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.

Nov 11 '07 #4

P: n/a
On Nov 11, 2:37 am, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
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?

Nov 11 '07 #5

P: n/a
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?

Thanks

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Nov 12 '07 #6

P: n/a
On Nov 12, 8:46 pm, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
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

Nov 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.