Convert Military Time to Standard Time?

Posted on
Fri Nov 11, 2011 8:42 pm
bschollnick2 offline
Posts: 1250
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Convert Military Time to Standard Time?

ckeyes888 wrote:
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.


The problem here, is that this isn't a military time problem. You would have the same issue with standard time.

new_datetime = datetime.datetime.now() + datetime.timedelta (minutes=45)

So convert from 24 hour time to standard, and use the above as a template...

------
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
Sat Nov 12, 2011 12:53 am
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Thanks, but I only know enough AS to get me in trouble.
Could you elaborate some on how to use that bit of code?

Carl

Posted on
Sat Nov 12, 2011 6:25 am
bschollnick2 offline
Posts: 1250
Joined: Oct 17, 2004
Location: Rochester, Ny

Re: Convert Military Time to Standard Time?

ckeyes888 wrote:
Thanks, but I only know enough AS to get me in trouble.
Could you elaborate some on how to use that bit of code?


Sorry, my bad, I forgot this thread was referring to Applescript. I gave you Python code...

Date-/Time-Calculations using AppleScript from http://erikslab.com/2007/11/26/date-tim ... plescript/

This short how-to explains how you calculate with time- and date-values in AppleScript. Quite a few people told me, that they are using the do shell script-handler presented in the post How to convert an “epoch”-time to a meaningful date and time. Nothing wrong with that, but if you just want to perform time-calculations inside an AppleScript, then there is an easier way.

If you assign the current time to a variable as in set myDate to current date and you just want to calculate the current date plus 2 days, then there is no need to use epoch-seconds.

set myNewDate to myDate + (2 * days), adds 2 days to the date and time stored in myDate.
set myNewDate to myDate + (4 * hours), adds 4 hours to the current time and date.
set myNewDate to myDate + (30 * minutes), adds 30 minutes to the current time and date.

It is really that simple, AppleScript uses epoch-seconds to perform these calculations, so you don’t have to.

If you need to set the date and time to the beginning of the current day, then use

set myTime to time of myDate

set myNewDate to myDate - myTime

The variable myNewDate now contains the current date and 00:00:00 as time.

------
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
Sat Nov 12, 2011 9:33 am
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Thanks. Guess I'm still not getting this.

I have a time variable "ClockTime" that reads 11:18 am that I'm trying to add time to.
Been trying variations of your post above but no go yet.

set the value of variable "HotTubReadyTime" to (value of variable "ClockTime") + (30 * minutes)

The error is, "Can't make 11:18 am into type number". Makes sense as it's not just a number.

Any help?

Thanks,

Carl

Posted on
Sat Nov 12, 2011 1:48 pm
jay (support) offline
Site Admin
User avatar
Posts: 8915
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Military Time to Standard Time?

Code: Select all
 set the value of variable "HotTubReadyTime" to ((date (value of variable "ClockTime")) + (30 * minutes)) as string

Jay (Indigo Support)
Twitter | Facebook

Posted on
Sat Nov 12, 2011 2:50 pm
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Thanks Jay. That works great albeit it returns "Saturday, November 12, 2011 6:46:00 PM".
Anyway to shorten that to just " 6:46 PM"?

Carl

Posted on
Sat Nov 12, 2011 5:30 pm
jay (support) offline
Site Admin
User avatar
Posts: 8915
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Military Time to Standard Time?

Code: Select all
 set the value of variable "HotTubReadyTime" to time string of ((date (value of variable "ClockTime")) + (30 * minutes))


That may show seconds - if so, open your System Preferences, open the "Language & Text" preference pane, click on the "Formats" tab, click the "Customize..." button under "Times", and change the "Medium" time format - that's the one that AppleScript uses when "time string" of a date is asked for. Just remove the seconds and the extra colon after the minutes.

Jay (Indigo Support)
Twitter | Facebook

Posted on
Sat Nov 12, 2011 5:46 pm
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Actually, I was hoping for just the time, not the month date etc.
Can that be done?

Thanks,

Carl

Posted on
Sat Nov 12, 2011 7:31 pm
jay (support) offline
Site Admin
User avatar
Posts: 8915
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Military Time to Standard Time?

Did you try the last one I posted?

Jay (Indigo Support)
Twitter | Facebook

Posted on
Sat Nov 12, 2011 10:15 pm
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

I did. It returns the full month date and year. Works for me....just curious if it can
just be the time.

Thanks,

Carl

Posted on
Sun Nov 13, 2011 12:18 am
jay (support) offline
Site Admin
User avatar
Posts: 8915
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Military Time to Standard Time?

The last one should only return "11:00:00 pm" (or something similar - it definitely should not return any of the date). Not sure why it isn't working for you. Make sure you got the last one that includes "time string" - maybe you didn't copy/paste it in correctly?

Jay (Indigo Support)
Twitter | Facebook

Posted on
Sun Nov 13, 2011 12:11 pm
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Doh! must have missed copied it. Easy to lose the seconds to just show 11:00 am?

Many thanks,

Carl

Posted on
Sun Nov 13, 2011 2:58 pm
jay (support) offline
Site Admin
User avatar
Posts: 8915
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Military Time to Standard Time?

Read the rest of the post that has the script in it... :P

Jay (Indigo Support)
Twitter | Facebook

Posted on
Sun Nov 13, 2011 3:55 pm
ckeyes888 offline
Posts: 1460
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Military Time to Standard Time?

Doh...again. I'd just set it to short. Maybe some day I'll learn to read.

Thanks,

Carl

Who is online

Users browsing this forum: No registered users and 0 guests