Convert Military Time to Standard Time?

Posted on
Sat Jul 02, 2011 5:00 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Convert Military Time to Standard Time?

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

Posted on
Sun Jul 03, 2011 6:03 am
bschollnick2 offline
Posts: 1355
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Convert Military Time to Standard Time?

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

------
My Plugins for Indigo (v4, v5, and v6) - http://bit.ly/U8XxPG

Security Script for v4 - http://bit.ly/QTgclf
for v5 - http://bit.ly/T6WBKu

Support Forum(s) - http://www.perceptiveautomation.com/userforum/viewforum.php?f=33

Posted on
Sun Jul 03, 2011 8:24 am
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Many thanks Ben. Good example to learn from as well.

Carl

Posted on
Sun Jul 03, 2011 12:52 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Posted on
Sun Jul 03, 2011 3:32 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Posted on
Sat Jul 16, 2011 8:05 am
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Posted on
Tue Jul 19, 2011 2:30 pm
Brandt offline
User avatar
Posts: 414
Joined: Dec 24, 2008
Location: Mission Viejo, CA

Re: Convert Military Time to Standard Time?

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

Indigo 7 w/ Dual-Band 2413U PLM
macOS High Sierra 10.13.x
2011 iMac 3.4 GHz Intel Core i7

Posted on
Thu Jul 21, 2011 9:54 am
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Posted on
Thu Jul 21, 2011 11:41 am
Brandt offline
User avatar
Posts: 414
Joined: Dec 24, 2008
Location: Mission Viejo, CA

Re: Convert Military Time to Standard Time?

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

Indigo 7 w/ Dual-Band 2413U PLM
macOS High Sierra 10.13.x
2011 iMac 3.4 GHz Intel Core i7

Posted on
Thu Jul 21, 2011 12:39 pm
Brandt offline
User avatar
Posts: 414
Joined: Dec 24, 2008
Location: Mission Viejo, CA

Re: Convert Military Time to Standard Time?

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

Indigo 7 w/ Dual-Band 2413U PLM
macOS High Sierra 10.13.x
2011 iMac 3.4 GHz Intel Core i7

Posted on
Thu Jul 21, 2011 12:55 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Posted on
Thu Jul 21, 2011 1:19 pm
Brandt offline
User avatar
Posts: 414
Joined: Dec 24, 2008
Location: Mission Viejo, CA

Re: Convert Military Time to Standard Time?

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.

Indigo 7 w/ Dual-Band 2413U PLM
macOS High Sierra 10.13.x
2011 iMac 3.4 GHz Intel Core i7

Posted on
Fri Jul 22, 2011 9:56 pm
Brandt offline
User avatar
Posts: 414
Joined: Dec 24, 2008
Location: Mission Viejo, CA

Re: Convert Military Time to Standard Time?

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

Indigo 7 w/ Dual-Band 2413U PLM
macOS High Sierra 10.13.x
2011 iMac 3.4 GHz Intel Core i7

Posted on
Fri Jul 22, 2011 11:47 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Thanks a bunch Brandt. I'll install it tomorrow.

Carl

Posted on
Fri Nov 11, 2011 2:02 pm
ckeyes888 offline
Posts: 2417
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

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

Who is online

Users browsing this forum: No registered users and 10 guests