Removing seconds from time when copying to variable

Posted on
Wed Jan 29, 2020 9:11 am
indigobo offline
User avatar
Posts: 22
Joined: Jan 04, 2019
Location: Raleigh, NC

Removing seconds from time when copying to variable

Hello,

I am using a script which works perfectly, however I do not want the seconds to be included. I have tried removing the %S in the code (see below) but that just results in an error. Any help would be appreciated!

Code: Select all
from datetime import datetime

startTimeVariable = indigo.variables[403061892].value  # variable SleepTimeStart
endTimeVariable = indigo.variables[1302608487].value  # variable SleepTimeStop

try:
   startTime = datetime.strptime(startTimeVariable, "%m-%d %H:%M:%S")
except StandardError, e:
    indigo.server.log(u"Error Converting Start Time ('%s'). Error='%s'" % (startTimeVariable, e), isError=True)   
    return
try:
   endTime = datetime.strptime(endTimeVariable, "%m-%d %H:%M:%S")
except StandardError, e:
    indigo.server.log(u"Error Converting End Time ('%s'). Error='%s'" % (endTimeVariable, e), isError=True)   
    return

if endTime > startTime:
    deltaTime = endTime - startTime
    indigo.variable.updateValue(992740824, str(deltaTime))  # variable SleepTimeAmount
else:
    indigo.variable.updateValue(992740824, value="end Time is before start time")
Last edited by indigobo on Wed Jan 29, 2020 8:32 pm, edited 1 time in total.

Posted on
Wed Jan 29, 2020 10:06 am
DaveL17 offline
User avatar
Posts: 6769
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Removing Second When Copying To Variable

I think the easiest way is to use the Python format specifier. Change this line:
Code: Select all
indigo.variable.updateValue(992740824, str(deltaTime))  # variable SleepTimeAmount
to this:
Code: Select all
indigo.variable.updateValue(992740824, u"{0:%Y-%m-%d %H:%M}".format(deltaTime))  # variable SleepTimeAmount
Untested, but that should work.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Jan 29, 2020 8:31 pm
indigobo offline
User avatar
Posts: 22
Joined: Jan 04, 2019
Location: Raleigh, NC

Re: Removing Second When Copying To Variable

Thank you so much for your help! I replaced the line of code with the suggested line but when I it's run it throws an error "invalid conversion specification"

Posted on
Wed Jan 29, 2020 9:04 pm
DaveL17 offline
User avatar
Posts: 6769
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Removing seconds from time when copying to variable

Apologies. That is indeed invalid for a timedelta object. This is a bit of a kludge, but it should work.

Code: Select all
if endTime > startTime:
    deltaTime = endTime - startTime
    newDeltaTime = ':'.join(str(deltaTime).split(':')[:2])
    indigo.variable.updateValue(992740824, newDeltaTime)  # variable SleepTimeAmount
else:
    indigo.variable.updateValue(992740824, value="end Time is before start time")

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Jan 30, 2020 10:08 am
jay (support) offline
Site Admin
User avatar
Posts: 18231
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Removing seconds from time when copying to variable

Here's another option for calculating just the hours and minutes of a timedelta object:

Code: Select all
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
hour_minute_str = "{}:{}".format(hours, minutes)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Feb 01, 2020 7:03 am
indigobo offline
User avatar
Posts: 22
Joined: Jan 04, 2019
Location: Raleigh, NC

Re: Removing seconds from time when copying to variable

Hey Dave & Jay,

Sorry, I just got around to implementing the above code (saw something shiny and had to follow it).

The code works perfectly and is exactly what I needed. Thank you!!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest