WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: stanl on April 20, 2014, 07:23:56 AM

Title: Why Doesn't This Work?
Post by: stanl on April 20, 2014, 07:23:56 AM
Code (WINBATCH) Select

#DefineSubRoutine cvtTime(time)
If StrIndexNC(time,"AM",0,@FWDSCAN)>0
   time=StrReplace(time,"AM","")
   Time= "2014:01:01:":time:":00:00"
   Message("",time)
Endif

If StrIndexNC(time,"PM",0,@FWDSCAN)>0
   time=StrReplace(time,"PM","")
   Time= "2014:01:01:":Int(ItemExtract(1,time,":"))+12:":":ItemExtract(2,time,":"):":00:00"
   Message("",time)
Endif

#EndSubroutine


t1="11:38AM"
t2="5:00PM"

t1= cvtTime(t1)
t2= cvtTime(t2)

t3= TimeDiff(t2,t1)
Message("",t3)

Exit
Title: Re: Why Doesn't This Work?
Post by: snowsnowsnow on April 20, 2014, 08:42:30 AM
After fixing all the :s back to StrCat()s (using my handy, dandy TXL-based converter program), I found two errors in your program:

1) You need a "Return" statement in your UDS.  I added "Return Time" just before the EndSubroutine.

2) You have one too many :00s in there.  I changed :00:00 to :00 in both branches in the UDS.

After that, it worked fine.

Fixed code is attached.

Happy Easter!!!
Title: Re: Why Doesn't This Work?
Post by: Deana on April 20, 2014, 10:36:11 AM
You will need a return statement in your UDF. Give this code sample a try:

Code (winbatch) Select

#DefineSubRoutine cvtTime(time)
   hh = ItemExtract( 1, time, ':' )
   mm =  ItemExtract( 2, time, ':' )
   If StrIndexNC( mm, 'AM', 0, @FWDSCAN )>0
   mm = StrReplace( mm, 'AM', '' )
   ElseIf StrIndexNC( mm,'PM', 0, @FWDSCAN )>0
   mm=StrReplace( mm, 'PM', '' )
   hh = hh + 12
   Else
   Pause( 'cvtTime Error', 'Invalid Time Input' )
Exit
   Endif
   time = '2014:01:01:':StrFixLeft(hh,0,2):':':mm:':00'
   Return time ;must return value
#EndSubroutine


t1= '11:38AM'
t2= '5:00PM'

t1= cvtTime(t1)
t2= cvtTime(t2)

t3= TimeDiff(t2,t1)
Message('',t3)

Exit
Title: Re: Why Doesn't This Work?
Post by: stanl on April 20, 2014, 10:54:36 AM
Thanks both;

I realized the mistake I made by not coding a return soon after I posted, and the extra :00 was a mis-read of the TimeDiff() in the help file...  The actual problem this relates to is  a  miserable .csv output I am receiving from a switch with multiple time in/time out rows for phone_id's, all in the hh:mmAM/PM format. 

If there were a space before the AM/PM, no problem treating the time as an Object and inserting into a table where I could perform a SELECT GROUP BY, max/min times to get total time logged in. I'll end up doing that anyway, just user Deana's uds as middleware.

Thanks again...  and Hope your Easter is happy.
Title: Re: Why Doesn't This Work?
Post by: stanl on April 21, 2014, 05:07:10 AM
P.S. [Note To Deana]

ElseIf StrIndexNC( mm,'PM', 0, @FWDSCAN )>0
           mm=StrReplace( mm, 'PM', '' )
           hh = hh + 12
     
probably should have
           If Int(hh)<>12 Then  hh = hh + 12
   
Title: Re: Why Doesn't This Work?
Post by: snowsnowsnow on April 21, 2014, 06:57:41 AM
Note that there is a symmetric problem with 12:00 AM.  In fact, the fact that both 12s need to be handled specially shows you just how screwed up our time system is.

I've solved this problem several times over the years, in various languages, and each time I've had to include code to handle the 12s.  What it boils down to is that both 12s are wrong.  12 AM is really 12 PM (12 hours after noon), and 12 PM is really 12 AM (12 hours after midnight).

Obviously designed by a committeeââ,¬Â¦
Title: Re: Why Doesn't This Work?
Post by: ChuckC on April 21, 2014, 09:50:47 AM
Uh... I just use a 24 hour clock and don't worry about the AM/PM distinction.  It's kinda funny... my children grew up with all the clocks in the house configured for military time, and when teachers tried to force the 12 hour AM/PM clock on them in elementary school, their response was "that's so stupid".