What is the actual data type for preference of type time?


(Ron) #1

I am had trouble determining the data type of preference declared as type time.
The documentation states type “time” is “Time of Day” but what is the actual data type.
http://docs.smartthings.com/en/latest/smartapp-developers-guide/preferences-and-settings.html
I think the documentation page above should have a column added for each data type which explains the data type created by the defined preference types. String, DateTime etc

I assumed it was a date time so trying to work with the preference created as
section(“Select time of day Mon-Fri…”) {
input “time1”, “time”, title: “Time Mon-Fri ?”
}

I assumed I could use
time1.format(“H”,tz) // to get the hour
time1.format(“m”,tz) // to get the min

But this didn’t work.
So I found a smart app using a time preference and found the following code which I didn’t really understand but I got it to work as follows
def schedTime = new Date(timeToday(time1).time)
def hour = schedTime.format(‘H’,tz)
def min = schedTime.format(“m”,tz)

I also tried just
time1.time.format(“m”,tz) but that also fails so time1.time isn’t a DateTime either.

So I have it working but i still don’t really understand what the data type was.

Why is it time1.time and not just time1 ?
Are there other attributes for this time type ex time1.min or time.date etc


(Brice; SmartRulesApp.com) #2

I believe it is just a string in this format: “yyyy-MM-dd’T’HH:mm:ss.SSSZ”


(Jim Anderson) #3

More information about the datetime format can be found here in the note about the time format.

I added more to the comment for that input field to help clarify.


(Ron) #4

@Jim So I tried the following code based on references you provided.

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”);
schedTime - formatter.parse(time2)
def hour = schedTime.format(“H”,tz)
def min = schedTime.format(“m”,tz)

but the ide doesn’t recognize SimpleDateFormat

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script14281697030721547018735.groovy: 65: unable to resolve class SimpleDateFormat
@ line 65, column 26.
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSSZ”);

So I am back to using
def schedTime = new Date(timeToday(time1).time)

Can you tell me what the “timeToday” method is ?
It is not intuitive that I need to create a date by calling a method called timeToday, passing it an argument which is my time preference and access an attribute called .time

What does the timeToday method do to this string and what does it return ?

Are there other convenience methods for this String Based SimpleDateFormat type ?


(Jim Anderson) #5

Hey @Ron,

Here’s the reference documentation for timeToday: https://graph.api.smartthings.com/ide/doc/smartApp#timeToday

We need to add a guide for working with time; it’s on the list. In the meantime, hopefully the reference docs help…


(Ron) #6

@Jim THANKS! Wow I looked all over for this and didn’t find it. I must be obtuse :frowning:

I have to be honest…What a terrible name for a method.
timeToday(string) = Returns a Date object for the specified time string.
Why call that time “TODAY” ? It should be parseTime or timeToDate or anything but timeToday :smile:

I seriously thought I was using the wrong method based on the name but now I know my code is actually correct. Except I think I need to add time zone.

Thanks so much for putting up with my questions. You have been VERY helpful !

UPDATE: After reading through the docs some more I not realize my code was a little funky.
Not it’s simply as follows where time2 is my preference setting.
def tz = location.timeZone
def schedTime = timeToday(time2,tz)
def hour = schedTime.format(“H”,tz)
def min = schedTime.format(“m”,tz)

Just in case anyone is reading or later reads this thread seeming similar assistance.