Comparing dates in a Python Script

Posted on
Fri Mar 22, 2019 4:49 am
jrcitizen offline
Posts: 26
Joined: Jan 04, 2017

Comparing dates in a Python Script

I am trying to compare two dates (as an Action, Execute Script, Python). In short, what I want to achieve is if the current date (taken from the Indigo Server) is less than a variable (which is entered as dd/mm/yyyy), then it sets another variable to Yes (and if not, it sets that variable to No). I wrote the following, but am getting errors (see below)

Code: Select all
import datetime

SchoolHolidayExpiration = indigo.variables[1189488181].value
currentDate = indigo.server.getTime().date()

indigo.server.log(SchoolHolidayExpiration)
indigo.server.log(currentDate)

if currentDate <= SchoolHolidayExpiration:
   indigo.variable.updateValue(815180286, value=Yes)
else:
    indigo.variable.updateValue(815180286, value=No)


The first error I get is:
Script Error embedded script: Python argument types in
ServerInfo.log(datetime.date)
did not match C++ signature:
log(CCString message, CCString type='', bool isError=False)
Script Error Exception Traceback (most recent call shown last):

embedded script, line 7, at top level
ArgumentError: Python argument types in
ServerInfo.log(datetime.date)
did not match C++ signature:
log(CCString message, CCString type='', bool isError=False)


and if I take out the
indigo.server.log(currentDate)
, then I simply get
TypeError: can't compare datetime.date to unicode


Any ideas?

Posted on
Fri Mar 22, 2019 5:57 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Comparing dates in a Python Script

Try this code :) :
Code: Select all
import datetime

SchoolHolidayExpiration = datetime.datetime.strptime(indigo.variables[1189488181].value, '%d/%m/%Y').date()
currentDate = indigo.server.getTime().date()

indigo.server.log(u'School Holiday Expiration = {}'.format(SchoolHolidayExpiration))
indigo.server.log(u'Current Date = {}'.format(currentDate))

if currentDate <= SchoolHolidayExpiration:
    indigo.variable.updateValue(815180286, value='Yes')
else:
    indigo.variable.updateValue(815180286, value='No')

Posted on
Fri Mar 22, 2019 8:43 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Comparing dates in a Python Script

Note: in the US, it's likely the formatter would be '%m/%d/%Y' (not sure where the OP is).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Mar 22, 2019 10:22 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Comparing dates in a Python Script

jay (support) wrote:
Note: in the US, it's likely the formatter would be '%m/%d/%Y' (not sure where the OP is).

Point noted but the original poster specified that his input format was:
... which is entered as dd/mm/yyyy ...
:)

Posted on
Fri Mar 22, 2019 12:49 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Comparing dates in a Python Script

Then we can leave it here for North American citizens to easily find... :lol:

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Mar 22, 2019 2:01 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: Comparing dates in a Python Script

jay (support) wrote:
Then we can leave it here for North American citizens to easily find... :lol:

Actually, that would just be the US, Philippines, Micronesia and the Marshall Islands...
See... https://en.m.wikipedia.org/wiki/Date_format_by_country

Once again, as with its measurement system, the US marches to a different drummer.

Posted on
Sun Mar 24, 2019 11:30 am
jrcitizen offline
Posts: 26
Joined: Jan 04, 2017

Re: Comparing dates in a Python Script

Thanks autolog – that did it (correct date format and all! :D )

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest

cron