Time Zero Padding

Posted on
Mon Jul 24, 2017 7:29 am
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Time Zero Padding

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.

Posted on
Mon Jul 24, 2017 8:44 am
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Time Zero Padding

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)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 24, 2017 8:56 am
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: Time Zero Padding

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!!

Posted on
Mon Jul 24, 2017 9:13 am
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Time Zero Padding

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 24, 2017 2:34 pm
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: Time Zero Padding

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!

Posted on
Mon Jul 24, 2017 3:06 pm
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: Time Zero Padding

looks like i got it, i used this:

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

Posted on
Mon Jul 24, 2017 4:33 pm
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Time Zero Padding

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jul 25, 2017 7:39 am
esprits300 offline
Posts: 49
Joined: Apr 29, 2012

Re: Time Zero Padding

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!

Posted on
Tue Jul 25, 2017 8:44 am
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Time Zero Padding

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Jul 25, 2017 12:12 pm
roussell offline
User avatar
Posts: 1108
Joined: Aug 18, 2008
Location: Alabama

Re: Time Zero Padding

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

Posted on
Tue Jul 25, 2017 1:07 pm
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Time Zero Padding

Yeah, he was pretty specific so that's why I didn't use %l. Lots of ways to skin cats in Python!

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests