Page 1 of 1

Time Zero Padding

PostPosted: Mon Jul 24, 2017 7:29 am
by esprits300
hello, i hopefully have a simple question. i have a control page that lists the time variable. It shows up as "08:00 AM". Im trying to REMOVE the first "0" so it looks like "8:00 AM". i looked into creating a new time variable and having it update via python and various other CLI switches, but no success. apparently this is called zero padding or rather removing the zero padding. anyone have any ideas? thank you.

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 8:44 am
by jay (support)
What's populating your time variable? That's where you'll need to make the change. For instance, the following Python script will do it:

Code: Select all
from datetime import datetime
now = datetime.now()
time_string = "{:1d}:{:02d} {:%p}".format(int("{:%I}".format(now)), now.minute, now)
indigo.variable.updateValue(123456, value= time_string)

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 8:56 am
by esprits300
for the moment, i was using the built in variable for system time. i didn't see any options for it, which is why i figured id use a script for more options. this looks like it should work, i won't get a chance to test it until later but thank you for the quick response!!

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 9:13 am
by jay (support)
What built-in variable? There isn't a built-in Indigo variable that holds the current date/time. So if you've got an Indigo variable that shows date/time, it's something that you've created.

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 2:34 pm
by esprits300
you are correct. i didn't even realize that. i built it SO long ago i thought it was builtin. i was using the insert time to variable. i re-worked it to update using your python script but its in 24 hour format now. Any way to get 12 hour format without the 0 for non 2 digit numbers? thank you!

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 3:06 pm
by esprits300
looks like i got it, i used this:

import datetime
now = datetime.datetime.now()
indigo.variable.updateValue(12345678, value= now.strftime("%-I:%M %p"))

Re: Time Zero Padding

PostPosted: Mon Jul 24, 2017 4:33 pm
by jay (support)
Note that using the datetime formatters in that way may not be future proof. Specifically, the dash between the % and the I will only work if a particular C formatting routine is supported. It is now on macOS, and while it seems unlikely to change, may not work correctly in the future. The method I outlined is done in the Python interpreter (and not passed directly to a C routine) so it's guaranteed to work on any Python platform.

Re: Time Zero Padding

PostPosted: Tue Jul 25, 2017 7:39 am
by esprits300
intersting. i will definitely keep that in mind! i tried the code you provided but it printed out 24 hour, not 12. i found the other code online after more digging, If i can get yours to show 12 hour, id do that! either way, at this moment I'm satisfied, on to the next opportunity! thanks for the quick responses! as usual, you guys are awesome!

Re: Time Zero Padding

PostPosted: Tue Jul 25, 2017 8:44 am
by jay (support)
Ah, so it does. I've updated the script above to work as described (12 hour clock, no padding on the hour). It's somewhat messy, and there may be another way, but that works.

Re: Time Zero Padding

PostPosted: Tue Jul 25, 2017 12:12 pm
by roussell
I like 12-hour "blank padding" for the hours and no seconds as it helps to keep things centered, so I do something like this:

Code: Select all
from datetime import datetime
now = datetime.now()
time_string = "{d:%l}:{d.minute:02} {d:%p}".format(d=now)
indigo.variable.updateValue(123456, value= time_string)


Terry

Re: Time Zero Padding

PostPosted: Tue Jul 25, 2017 1:07 pm
by jay (support)
Yeah, he was pretty specific so that's why I didn't use %l. Lots of ways to skin cats in Python!