472,783 Members | 969 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,783 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 2017
>
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
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...

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.