Page 1 of 2

Convert Military Time to Standard Time?

PostPosted: Sat Jul 02, 2011 5:00 pm
by ckeyes888
Curious if it's possible using AS to convert military time to standard time, e.g. 2330 to 11:30 pm?

I use military time for my sprinkler set time, easier to adjust adding or subtracting to, and I'd like to be able
to have reminders/announcements based on that time, but in the am/pm format.

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sun Jul 03, 2011 6:03 am
by bschollnick2
ckeyes888 wrote:
Curious if it's possible using AS to convert military time to standard time, e.g. 2330 to 11:30 pm?

I use military time for my sprinkler set time, easier to adjust adding or subtracting to, and I'd like to be able
to have reminders/announcements based on that time, but in the am/pm format.


Carl, the formula for Military Time is simple...

Here's some pseudo code:

If hour => 13:
#
# Time is 24 hr
new_hour = hour - 12

And vice versa.

There is nothing tricky about 24 hour time...

Here's code to convert to 24 hour. The reason I am showing it, is simply to illustrate that most of the code is there to decode the time string...

I wasn't able to find any 24 to 12 code, at least my google foo failed me... I know it's out there...

-- This assumes that the input is a 12-hour time string, that any separators in it are colons, and that it ends with either "am" or "pm".
on h12toh24(input, separator)
set t to words of text 1 thru -3 of input
if (text -2 thru -1 of input is "pm") then set item 1 of t to (item 1 of t) + 12
repeat 3 - (count t) times
set end of t to 0
end repeat
tell (1000000 + (item 1 of t) * 10000 + (item 2 of t) * 100 + (item 3 of t)) as text
return text 2 thru 3 & separator & text 4 thru 5 & separator & text 6 thru 7
end tell
end h12toh24

Re: Convert Military Time to Standard Time?

PostPosted: Sun Jul 03, 2011 8:24 am
by ckeyes888
Many thanks Ben. Good example to learn from as well.

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sun Jul 03, 2011 12:52 pm
by ckeyes888
Found this code but have no idea if/how to implement it to set a variable in Indigo.

"Simple and clean... convert 24 hour format to 12 hour format with PHP native functions.
BTW, works in the other direction too (12 hour to 24 hour)... that's the second line below"

<?=date("g:i a", strtotime("13:30"));?>

<?=date("H:i", strtotime("1:30 pm"));?>



Can something like this be used?

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sun Jul 03, 2011 3:32 pm
by ckeyes888
Found another. Got this one working.
Code: Select all
set militaryTime to value of variable "sprk_Start_Time"
set standardTime to militaryTimeToStandardTime(militaryTime)

on militaryTimeToStandardTime(militaryTime)
    set militaryTime to militaryTime as text
    set theHour to text 1 thru 2 of militaryTime
    set theMinutes to text 3 thru 4 of militaryTime
    
    set theHourNumber to theHour as number
    if theHourNumber is equal to 24 then
        set standardTime to "12:00 AM"
    else if theHourNumber is greater than 12 then
        set theHourNumber to theHourNumber - 12
        set standardTime to (theHourNumber as text) & ":" & theMinutes & " PM"
    else if theHourNumber is equal to 12 then
        set standardTime to theHour & ":" & theMinutes & " PM"
    else if theHourNumber is equal to 0 then
        set standardTime to "12" & ":" & theMinutes & " AM"
    else
        set standardTime to theHour & ":" & theMinutes & " AM"
    end if
    set value of variable "sprk_Start_Time_AmPM" to standardTime
end militaryTimeToStandardTime

Re: Convert Military Time to Standard Time?

PostPosted: Sat Jul 16, 2011 8:05 am
by ckeyes888
Hmm...well the above script works if the military time variable is all four characters, 0021 etc.
If it's less than four characters, 21 etc., it doesn't work.

Any help in a fix?

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Tue Jul 19, 2011 2:30 pm
by Brandt
Ben is better at AS than I am, but if militaryTime is a standard AS timestamp then you could do something like:

Code: Select all
set standardTime to ((militaryTime div hours) - 12) & ":" & (militaryTime div minutes)

if (militaryTime div hours) > 12 then
set timeNotation to "pm"
else
set timeNotation to "am"
end if

set standardTime to standardTime & timeNotation




ckeyes888 wrote:
Found another. Got this one working.
Code: Select all
set militaryTime to value of variable "sprk_Start_Time"
set standardTime to militaryTimeToStandardTime(militaryTime)

on militaryTimeToStandardTime(militaryTime)
    set militaryTime to militaryTime as text
    set theHour to text 1 thru 2 of militaryTime
    set theMinutes to text 3 thru 4 of militaryTime
    
    set theHourNumber to theHour as number
    if theHourNumber is equal to 24 then
        set standardTime to "12:00 AM"
    else if theHourNumber is greater than 12 then
        set theHourNumber to theHourNumber - 12
        set standardTime to (theHourNumber as text) & ":" & theMinutes & " PM"
    else if theHourNumber is equal to 12 then
        set standardTime to theHour & ":" & theMinutes & " PM"
    else if theHourNumber is equal to 0 then
        set standardTime to "12" & ":" & theMinutes & " AM"
    else
        set standardTime to theHour & ":" & theMinutes & " AM"
    end if
    set value of variable "sprk_Start_Time_AmPM" to standardTime
end militaryTimeToStandardTime

Re: Convert Military Time to Standard Time?

PostPosted: Thu Jul 21, 2011 9:54 am
by ckeyes888
Been trying variations of this with no luck:
Code: Select all
tell application "IndigoServer"
    set militaryTime to value of variable "sprk_Start_Time"
    set standardTime to ((militaryTime div hours) - 12) & ":" & (militaryTime div minutes)
    
    if (militaryTime div hours) > 12 then
        set timeNotation to " pm"
    else
        set timeNotation to " am"
    end if
    
    set standardTime to standardTime & timeNotation
    set value of variable "sprk_Start_Time_AmPM" to standardTime
end tell


Carl

Re: Convert Military Time to Standard Time?

PostPosted: Thu Jul 21, 2011 11:41 am
by Brandt
Carl,

The computer relies on a complete date/time stamp, and this complete stamp stored in your variables would make it easier to interact with using AS. I assume you are just putting a time like "2300" as the value of the variable? Is your system time set to 12 or 24 hour format? Can you give us a bigger (more detailed ) overview of what you are trying to do? We may be making this more complicated than it needs to be.


-Brandt

Re: Convert Military Time to Standard Time?

PostPosted: Thu Jul 21, 2011 12:39 pm
by Brandt
military time by standard isn't ever written with less than 4 characters ( i should know I was in the Navy ;) ) Applescript isn't really meant to handle this it is meant to communicate between mac applications, in other words it's not really the right tool for the job. Indigo 5 supports python, which would probably be a better tool for the job.

ckeyes888 wrote:
Hmm...well the above script works if the military time variable is all four characters, 0021 etc.
If it's less than four characters, 21 etc., it doesn't work.

Any help in a fix?

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Thu Jul 21, 2011 12:55 pm
by ckeyes888
My system clock is using the 12 hr format. I use the military time format for my sprinkler
countdown timer variable as it just seemed easier to add/subtract from, due to rain/heat etc.
with my incredibly limited knowledge of AS.
I have a script that executes whenever the variable "sprinkler_start_time" changes to convert it to standard time which sets another
variable, sprinkler_start_time_AmPm", which I use for display purposes on CP's etc.

Again, the script I'm using works fine if the military time variable is all four digits, 0045,
but doesn't if it's less digits, e.g.45.

Hope this is making some sense anyway.

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Thu Jul 21, 2011 1:19 pm
by Brandt
ok well use your original code, just add some tests to it:


Code: Select all
set militaryTime to value of variable "sprk_Start_Time"
set standardTime to militaryTimeToStandardTime(militaryTime)

on militaryTimeToStandardTime(militaryTime)
    set militaryTime to militaryTime as text
    if length of militaryTime is equal to 4 then   
        set theHour to text 1 thru 2 of militaryTime
        set theMinutes to text 3 thru 4 of militaryTime
    else if length of militaryTime is equal to 2 then
        set theHour to "00"
        set theMinutes to text 1 thru 2 of militaryTime
    end if
    set theHourNumber to theHour as number
    if (theHourNumber is equal to 00) & (theMinutes is equal to 00) then
        set standardTime to "12:00 AM"
    else if theHourNumber is greater than 12 then
        set theHourNumber to theHourNumber - 12
        set standardTime to (theHourNumber as text) & ":" & theMinutes & " PM"
    else if theHourNumber is equal to 12 then
        set standardTime to theHour & ":" & theMinutes & " PM"
    else if (theHourNumber is equal to 00) & (theMinutes is not equal to 00) then
        set standardTime to "12" & ":" & theMinutes & " AM"
    else
        set standardTime to theHour & ":" & theMinutes & " AM"
    end if
    end militaryTimeToStandardTime

    set value of variable "sprk_Start_Time_AmPM" to standardTime




This code is untested and very sloppy, I'd be surprised if it works....If you can wait I can test and improve it when I get home this evening.

Re: Convert Military Time to Standard Time?

PostPosted: Fri Jul 22, 2011 9:56 pm
by Brandt
Finally got around to testing it and added a few things:

Code: Select all
set militaryTime to value of variable "sprk_Start_Time"
set standardTime to militaryTimeToStandardTime(militaryTime)

on militaryTimeToStandardTime(militaryTime)
   set militaryTime to militaryTime as text
   if length of militaryTime is equal to 4 then
      set theHour to text 1 thru 2 of militaryTime
      set theMinutes to text 3 thru 4 of militaryTime
   else if length of militaryTime is equal to 3 then
      set theHour to "0" & text 1 of militaryTime
      set theMinutes to text 2 thru 3 of militaryTime
   else if length of militaryTime is equal to 2 then
      set theHour to "00"
      set theMinutes to text 1 thru 2 of militaryTime
   else if length of militaryTime is equal to 1 then
      set theHour to "00"
      set theMinutes to "0" & text 1 of militaryTime
   end if
   set theHourNumber to theHour as number
   if (theHourNumber is equal to 0) and (theMinutes is equal to 0) then
      set standardTime to "12:00 AM"
   else if theHourNumber is greater than 12 then
      set theHourNumber to theHourNumber - 12
      set standardTime to (theHourNumber as text) & ":" & theMinutes & " PM"
   else if theHourNumber = 12 then
      set standardTime to theHour & ":" & theMinutes & " PM"
   else if (theHourNumber is equal to 0) and (theMinutes is not equal to 0) then
      set standardTime to "12" & ":" & theMinutes & " AM"
   else
      set standardTime to theHour & ":" & theMinutes & " AM"
   end if
end militaryTimeToStandardTime

set value of variable "sprk_Start_Time_AmPM" to standardTime




This works with 1, 30, 124, 1750, 0000....just remember there is no 2400. It goes from 2359 to 0000

Re: Convert Military Time to Standard Time?

PostPosted: Fri Jul 22, 2011 11:47 pm
by ckeyes888
Thanks a bunch Brandt. I'll install it tomorrow.

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Fri Nov 11, 2011 2:02 pm
by ckeyes888
Came across another issue. I have a trigger that turns on the hot tub and calculates
what time the tub will reach the desired temp based on it's current temp.
I simply take the military time and add time to it depending on the current hot tub temp.

Problem is that the new number can be outside the military time spec by going over 59 mins.
e.g. if the military time is 1550 and I add 30 mins it becomes 1580, then when I convert that
using the script above the ready time becomes 3:80 pm.

Any ideas?

Many thanks,

Carl