473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Time

I need to convert the string below into epoch seconds so that I can
perform substractions and additions. I assume I will need to break it
up into a time_t struct and use mktime. Two questions if you will
please:

Is there a way to use multiple separator characters for split similar
to awk's [|] style?

Could you point to an example of a python time_t struct?

05/11/2007 15:30

Thanks,

jvh

May 11 '07 #1
13 2054
>
Could you point to an example of a python time_t struct?
Or maybe that should be a tm struct???

May 11 '07 #2
Sorry, reading a little closer I see that the time tuple is apparently
an ordinary list.

jvh

May 11 '07 #3
On May 12, 7:46 am, HMS Surprise <j...@datavoiceint.comwrote:

[first message]

HS ==I need to convert the string below into epoch seconds so that I
can
perform substractions and additions.

JM ==I presume you mean "seconds since the epoch". You don't need to
do that.

HS ==I assume I will need to break it
up into a time_t struct and use mktime.

JM ==You assume wrongly. The time module exists (IMVHO) solely as a
crutch for people who are converting C etc code that uses the time.h
functions from the C standard library. If you are starting off from
scratch, use the Python datetime module -- especially if you need to
store and manipulate pre-1970 dates; e.g. the date of birth of anyone
aged more than about 37.5 years :-)

HS ==Two questions if you will
please:

Is there a way to use multiple separator characters for split similar
to awk's [|] style?

JM ==Only if you can find such a way in the manual.

HS ==Could you point to an example of a python time_t struct?

JM ==Python doesn't have that; it's a C concept
HS ==05/11/2007 15:30

[second message]

HS== Could you point to an example of a python time_t struct?

Or maybe that should be a tm struct???

JM ==See previous answer.

[third message]

HS ==Sorry, reading a little closer I see that the time tuple is
apparently
an ordinary list.

JM ==Huh? A tuple is a tuple. A tuple is not a list, not even a very
extraordinary one.

If you are desperate to use the time module, try this:
>>import time
s = "05/11/2007 15:30"
fmt = "%m/%d/%Y %H:%M"
# Given the current date, I'm presuming that your example indicates
that you adhere to the "month-first-contrary-to-common-sense"
religion :-)
>>time.strptime(s, fmt)
(2007, 5, 11, 15, 30, 0, 4, 131, -1)

otherwise:
>>import datetime
d1 = datetime.datetime.strptime(s, fmt)
d1
datetime.datetime(2007, 5, 11, 15, 30)
>>d2 = datetime.datetime(2007, 5, 1)
d2
datetime.datetime(2007, 5, 1, 0, 0)
>>delta = d1 - d2
delta
datetime.timedelta(10, 55800)
>>days_diff = delta.days + delta.seconds / 60. / 60. / 24.
days_diff
10.645833333333334

Do read the datetime module documentation for more info ... in
particular the timedelta object has a microseconds attribute; in
general there is a whole heap of functionality in there.

HTH,
John

May 11 '07 #4
Thanks for posting. I sure am sorry that I wasted your time. I should
have started the post stating I am using jython 2.2.3 and apparently
it has no datetime module. But I will keep datetime in mind for future
reference.

Since I had no datetime I cobbled out the following. Seems to work
thus far. Posted here for the general amusement of the list.

Regards,

jvh

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

from time import *
s = '05/11/2007 1:23 PM'
t = s.split()
mdy = t[0].split('/')

hrMn = t[1].split(':')
if t[2] == 'PM':
hrMn[0] = int(hrMn[0]) + 12

tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
0,0,0,0)
print tuple

eTime = mktime(tuple)
print 'eTime', eTime

May 14 '07 #5
On May 14, 9:00 am, HMS Surprise <j...@datavoiceint.comwrote:
Thanks for posting. I sure am sorry that I wasted your time. I should
have started the post stating I am using jython 2.2.3 and apparently
it has no datetime module. But I will keep datetime in mind for future
reference.

Since I had no datetime I cobbled out the following. Seems to work
thus far. Posted here for the general amusement of the list.

Regards,

jvh

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

from time import *
s = '05/11/2007 1:23 PM'
t = s.split()
mdy = t[0].split('/')

hrMn = t[1].split(':')
if t[2] == 'PM':
hrMn[0] = int(hrMn[0]) + 12

tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
0,0,0,0)
print tuple

eTime = mktime(tuple)
print 'eTime', eTime
Since jython works with Java, why not use Java's time/datetime
modules? Various links abound. Here are a few:

http://www.raditha.com/blog/archives/000552.html
http://www.xmission.com/~goodhill/dates/deltaDates.html
http://www.velocityreviews.com/forum...variables.html

Maybe those will give you some hints.

Mike

May 14 '07 #6
if t[2] == 'PM':
hrMn[0] = int(hrMn[0]) + 12

Oops, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12
May 14 '07 #7
On May 14, 9:09 am, kyoso...@gmail.com wrote:
On May 14, 9:00 am, HMS Surprise <j...@datavoiceint.comwrote:
Thanks for posting. I sure am sorry that I wasted your time. I should
have started the post stating I am using jython 2.2.3 and apparently
it has no datetime module. But I will keep datetime in mind for future
reference.
Since I had no datetime I cobbled out the following. Seems to work
thus far. Posted here for the general amusement of the list.
Regards,
jvh
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from time import *
s = '05/11/2007 1:23 PM'
t = s.split()
mdy = t[0].split('/')
hrMn = t[1].split(':')
if t[2] == 'PM':
hrMn[0] = int(hrMn[0]) + 12
tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
0,0,0,0)
print tuple
eTime = mktime(tuple)
print 'eTime', eTime

Since jython works with Java, why not use Java's time/datetime
modules? Various links abound. Here are a few:

http://www.raditha.com/blog/archives...erence-in-date...

Maybe those will give you some hints.

Mike
Excellent idea.

Thanks Mike.
jvh

May 14 '07 #8
On May 14, 9:22 am, HMS Surprise <j...@datavoiceint.comwrote:
if t[2] == 'PM':
hrMn[0] = int(hrMn[0]) + 12

Oops, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12
Oops +=1, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12

Need more starter fluid, coffee please!!!

May 14 '07 #9
En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <jo**@datavoiceint.com>
escribió:
On May 14, 9:22 am, HMS Surprise <j...@datavoiceint.comwrote:

Oops +=1, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12

Need more starter fluid, coffee please!!!
Still won't work for 12 AM nor 12 PM...

--
Gabriel Genellina

May 14 '07 #10
On 2007-05-14, Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <jo**@datavoiceint.com>
escribió:
>On May 14, 9:22 am, HMS Surprise <j...@datavoiceint.comwrote:

Oops +=1, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12

Need more starter fluid, coffee please!!!

Still won't work for 12 AM nor 12 PM...
Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
do they?

--
Grant Edwards grante Yow! HELLO, everybody,
at I'm a HUMAN!!
visi.com
May 14 '07 #11
On 2007-05-14, Grant Edwards <gr****@visi.comwrote:
On 2007-05-14, Gabriel Genellina <ga*******@yahoo.com.arwrote:
>En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <jo**@datavoiceint.com>
escribió:
>>On May 14, 9:22 am, HMS Surprise <j...@datavoiceint.comwrote:

Oops +=1, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12

Need more starter fluid, coffee please!!!

Still won't work for 12 AM nor 12 PM...

Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
do they?
http://www.astronomy.net/articles/13/
--
Grant Edwards grante Yow! I am a jelly donut.
at I am a jelly donut.
visi.com
May 14 '07 #12
En Mon, 14 May 2007 16:20:27 -0300, Grant Edwards <gr****@visi.com>
escribió:
>>>On May 14, 9:22 am, HMS Surprise <j...@datavoiceint.comwrote:

Oops +=1, should be:
hrMn[0] = int(hrMn[0]
if t[2] == 'PM':
hrMn[0] += 12

Need more starter fluid, coffee please!!!

Still won't work for 12 AM nor 12 PM...

Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
do they?

http://www.astronomy.net/articles/13/
No. Even ignoring that exact instant at Noon or Midnight, 12:01 PM
translates into itself, 12:01, on a 24 hr clock; and 12:01 AM becomes 0:01
on a 24 hr clock. For hours between 01 PM and 11 PM, yes, you can follow
the rule "add 12".

--
Gabriel Genellina

May 14 '07 #13
>
Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
do they?
>>t = (2007, 5, 14, 12, 0,0,0,0,0)
strftime('%p', t)
'PM'
>>t = (2007, 5, 14, 0,0,0,0,0,0)
strftime('%p', t)
'AM'
>>>
May 14 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bart Nessux | last post by:
am I doing this wrong: print (time.time() / 60) / 60 #time.time has been running for many hours if time.time() was (21600/60) then that would equal 360/60 which would be 6, but I'm not getting...
5
by: David Stockwell | last post by:
I'm sure this has been asked before, but I wasn't able to find it. First off I know u can't change a tuple but if I wanted to increment a time tuple by one day what is the standard method to do...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
3
by: Szabolcs Nagy | last post by:
I have to measure the time of a while loop, but with time.clock i always get 0.0s, although python manual sais: "this is the function to use for benchmarking Python or timing algorithms" So i...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Roseanne | last post by:
We are experiencing very slow response time in our web app. We run IIS 6 - windows 2003. I ran iisstate. Here's what I got. Any ideas?? Opened log file 'F:\iisstate\output\IISState-812.log'...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.