472,964 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,964 software developers and data experts.

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/
Nov 10 '07 #1
6 2390
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alvaro G Vicario | last post by:
I really love the extended CHM format documentation. However, it apparently hasn't been updated since september 2003: http://www.php.net/docs-echm.php Does anyone know something about this?...
0
by: blucados | last post by:
Here are 3 questions about extended-status and server parameters. I have a server with 1G of Ram Dual Cpu running mysql 3.23.41. 1/ to know if table_cache need to be increased, what is the...
1
by: | last post by:
Hey all, Quick question...been bugging me for some time, really. I have a console app, it does some things, and I want to save an array of text to a text file. The text consists of ASCII and...
12
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine...
23
by: Bonj | last post by:
Hi I'm looking to write an extended stored procedure, the job of which will basically to read data from one table, process it using a COM object, and write (insert) rows out to another table. I...
3
by: ppateel | last post by:
Hi, First I would like to apologize for cross posting in three groups as I did not know which one would be the appropriate group. If some one points me to the correct one I will use that in the...
2
by: jajalc | last post by:
Hi all, WE have a windows service using .net and which uses a System.Timers.Timer..all the of code works fine..... but after a extended period of time the timer just stops firing the elasped...
10
by: =?Utf-8?B?RGljaw==?= | last post by:
I'd like to read a file's "extended" properties, i.e. for some JPEG images taken with a digital camera, I'd like to read the "Date Picture Taken" property. I can see this property from the Summary...
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.