473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2423
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(n ow_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************* *************** ***********@pyt hon.org on behalf
of Jeremy Sanders
*Sent:* Sat 11/10/2007 9:37 AM
*To:* py*********@pyt hon.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.jeremysande rs.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...@gmai l.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_epoc h 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_epoc h 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_epoc h 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.dateti me(2007, 11, 12, 21, 20, 39, 859123)
for epoch_year in (1, 1970, 2070):
EPOCH = datetime.dateti me(epoch_year, 1, 1)
print "\nepoch", repr(EPOCH)
diff = now - EPOCH
print "diff", repr(diff)
sdiff = diff.microsecon ds / 1000000. \
+ diff.seconds + diff.days * 24. * 3600.
print "sdiff", repr(sdiff)
roundtrip = EPOCH + datetime.timede lta(seconds=sdi ff)
print "roundtrip" , repr(roundtrip)
C:\junk>epoch.p y

epoch datetime.dateti me(1, 1, 1, 0, 0)
diff datetime.timede lta(732991, 76839, 859123)
sdiff 63330499239.859 123
roundtrip datetime.dateti me(2007, 11, 12, 21, 20, 39, 859123)

epoch datetime.dateti me(1970, 1, 1, 0, 0)
diff datetime.timede lta(13829, 76839, 859123)
sdiff 1194902439.8591 23
roundtrip datetime.dateti me(2007, 11, 12, 21, 20, 39, 859123)

epoch datetime.dateti me(2070, 1, 1, 0, 0)
diff datetime.timede lta(-22696, 76839, 859123)
sdiff -1960857560.1408 77
roundtrip datetime.dateti me(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
1888
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? Has the project died? -- -- Álvaro G. Vicario - Burgos, Spain
0
1259
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 maximum rate of opened_table that must be seen. I read somewhere that 1 or 2
1
3760
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 extended ascii (codes 0 to 255). I am using ofstream to output, and this is the line doing the actual output: outfile << screenbuffer.Char.AsciiChar; ...
12
5514
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 with 64 Gb of memory with a single non-partitioned database using extended storage and with intra-parallelism enabled. I've been experimenting...
23
1904
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 don't really know where to start with finding the functions necessary to read from an existing table. Can anyone be so kind as to point me in the...
3
1838
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 future. I am trying to findout the scalabilty of an user written extended store procedure. I have created a dll using a C program...
2
4035
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 event. the elasped event is fired every 2 sec and the code which is in the elasped event is multithreaded. This only happens if the service runs...
10
2335
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 tab (in Advanced mode) using Windows Explorer.
6
2629
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 in the page and I've got code to get the selected value from it in the Radiobuttonlist_SelectedIndexChanged event. The code in there is: ...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.