473,396 Members | 2,033 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,396 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 2059
>
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.