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

Time

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
>
Could you point to an example of a python time_t struct?
Or maybe that should be a tm struct???

May 11 '07 #2

P: n/a
Sorry, reading a little closer I see that the time tuple is apparently
an ordinary list.

jvh

May 11 '07 #3

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

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

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

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

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

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

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

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

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

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

P: n/a
>
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 discussion thread is closed

Replies have been disabled for this discussion.