Useful time/date script

Posted on
Tue Nov 28, 2017 11:08 am
roussell offline
User avatar
Posts: 1108
Joined: Aug 18, 2008
Location: Alabama

Useful time/date script

I was digging though my schedules and triggers looking for any embedded AppleScript holdouts and found a very old one that I had forgotten about. I use this script to tell Indigo if 'today' is a weekend, the month and season. It populates variables that are used throughout indigo so although it's small, it's crucial to a happy house. I've included my original AppleScript and the replacement Python script I quickly banged out to replace it. Although I'm certain it could be optimized, it's working fine in the house. Perhaps it can help others trying to convert AppleScript. FYI, it's an embedded script in a schedule that runs at midnight.

The original AppleScript:
Code: Select all
set value of variable "DayOfWeek" to (weekday of (current date)) as text

set weekend to {Saturday, Sunday}
if weekday of (current date) is in weekend then
   set value of variable "Weekend" to "Yes"
else
   set value of variable "Weekend" to "No"
end if

set value of variable "Month" to (month of (current date)) as text
set spring to {March, April, May}
set summer to {June, July, August}
set fall to {September, October, November}
set winter to {December, January, February}
if month of (current date) is in spring then
   set value of variable "Season" to "Spring"
else if month of (current date) is in summer then
   set value of variable "Season" to "Summer"
else if month of (current date) is in fall then
   set value of variable "Season" to "Fall"
else if month of (current date) is in winter then
   set value of variable "Season" to "Winter"
end if


...and the Python version:
Code: Select all
import datetime
import calendar
DayName = calendar.day_name[datetime.datetime.now().weekday()]
DayOfYear = datetime.datetime.today().timetuple().tm_yday
DayOfMonth = datetime.datetime.now().day
MonthName = calendar.month_name[datetime.datetime.now().month]
LeapYear = calendar.isleap(datetime.datetime.now().year)

#Is today part of a weekend?
if DayName == 'Saturday' or DayName == 'Sunday':
   Weekend = "Yes"
else:
   Weekend = "No"

#What season is today in?
if LeapYear:
   Spring = range(80, 172)
   Summer = range(173, 265)
   Fall = range(266, 356)
   # winter = everything else
else:
   Spring = range(79, 171)
   Summer = range(172, 264)
   Fall = range(265, 355)
   # winter = everything else

if DayOfYear in Spring:
   Season = 'Spring'
elif DayOfYear in Summer:
   Season = 'Summer'
elif DayOfYear in Fall:
   Season = 'Fall'
else:
   Season = 'Winter'

indigo.variable.updateValue(1632954055, value= DayName)
indigo.variable.updateValue(1156789336, value= Weekend)
indigo.variable.updateValue(843403989, value= MonthName)
indigo.variable.updateValue(648104659, value= Season)



Terry

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

Re: Useful time/date script

Cool... 8)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue May 21, 2019 11:35 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Useful time/date script

Can you explain the range calculation.... My guess is days of the year... but wouldn't that put the end of fall (356) at December 22nd +/-?

Is there a Houston Version? Summer = range (5 - 358) #winter = everything else

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests

cron