Applescript detect Daylight Saving Time

I was wondering if there were a simple Apple script way to detect if Daylight Saving Time is in effect:
On the Mac running the script.
At the time the script is run.

Internet material on this subject contain all manner of complications. However, the Mac operating system always displays the correct time. Hence, it must know when it is using DST. Isn’t there a simple Apple script parameter with this information, or else some stored value in system files reflecting this information?

You may use :

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	set theName to thisZone's |name|() as text
	set theAbbr to thisZone's abbreviation() as text
	set theOffset to thisZone's daylightSavingTimeOffset()
	set isDST to thisZone's isDaylightSavingTime() as boolean
	return {zone_Name:theName, zone_Abbr:theAbbr, zone_Offset:theOffset, isDaylightSavingTime:isDST}
end timeZoneInfo

my timeZoneInfo()

or the bare version :

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	return {isDaylightSavingTime:thisZone's isDaylightSavingTime() as boolean}
end timeZoneInfo

my timeZoneInfo()

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 mars 2020 21:25:00

Thanks for the reply. I am not a sophisticated Apple scripter.
I tried a test running in the script editor thus:

timeZoneInfo()

on timeZoneInfo()
set thisZone to current application’s NSTimeZone’s localTimeZone()
return {isDaylightSavingTime:thisZone’s isDaylightSavingTime() as boolean}
end timeZoneInfo

And got:
error “NSTimeZone doesn’t understand the “localTimeZone” message.” number -1708 from NSTimeZone

I this had run successfully, what do I check in the main script (boolean) to determine whether DST is on or off?

I tested Yvan’s second script and it worked fine. You do have to include the entire script beginning with:

In my case, Yvan’s script correctly returned:

Ok, I included those items in the beginning of my test and it ran without error.

In my case, Yvan’s script correctly returned:

{isDaylightSavingTime:true}

I know this because when I tried to display it, I got an error message that it couldn’t make it a string. But I saw what it was.

But what I don’t understand (even though I looked all over for it) is what the brackets mean with a colon in between. And how do I get the true or false out of it so I can do an if, then branch?

Yvan will better explain but the following is a working example:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set dst to isDaylightSavingTime of timeZoneInfo() --> set dst to value of isDaylightSavingTime

if dst then
	display dialog "Daylight saving time in effect" buttons "ok" --> dst is true
else
	display dialog "No daylight savings time" buttons "ok" --≥ dst is false
end if

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	return {isDaylightSavingTime:thisZone's isDaylightSavingTime() as boolean}
end timeZoneInfo

EDIT:

This is the same example but with Shane’s handler:

set dst to isDST() --> dst is set to true or false

if dst then
	display dialog "Daylight saving time in effect" buttons "ok" --> dst is true
else
	display dialog "No daylight savings time" buttons "ok" --≥ dst is false
end if

on isDST()
	script theScript
		property parent : a reference to current application
		use framework "Foundation"
		on isDST()
			current application's NSTimeZone's localTimeZone()'s isDaylightSavingTime() as boolean
		end isDST
	end script
	return theScript's isDST()
end isDST

FWIW, if you want a completely standalone handler that returns true or false, you can use something like this:

on isDST()
	script theScript
		property parent : a reference to current application
		use framework "Foundation"
		on isDST()
			current application's NSTimeZone's localTimeZone()'s isDaylightSavingTime() as boolean
		end isDST
	end script
	return theScript's isDST()
end isDST

Thanks peavine AND Yvan Koenig!

That formulation:

set dst to isDaylightSavingTime of timeZoneInfo()

worked perfectly (on latest Catalina).

I am trying to remedy my Apple script ignorance, but even though I have programming experience in other areas, I find most online information about Apple script either too basic, or too advanced. You people certainly know what you are doing; I wonder if you know a good instruction set that is thorough but also does not assume I have an intimate knowledge of the entire Apple script/Mac OS framework.

Thanks again.

I used records to return what I assumed to be self explanatory results.

A well known example is the record returned by:

set aRecord to display dialog "what's your first name" default answer "Yvan"

--> {button returned:"OK", text returned:"Yvan"}

display dialog "You clicked the button : " & button returned of aRecord & linefeed & "You type the first name : " & text returned of aRecord

The final result is the dialog displaying:
“You clicked the button : OK
You type the first name : Yvan”

The result of my first script would have been deciphered with:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	set theName to thisZone's |name|() as text
	set theAbbr to thisZone's abbreviation() as text
	set theOffset to thisZone's daylightSavingTimeOffset()
	set isDST to thisZone's isDaylightSavingTime() as boolean
	return {zone_Name:theName, zone_Abbr:theAbbr, zone_Offset:theOffset, isDaylightSavingTime:isDST}
end timeZoneInfo

set aRecord to my timeZoneInfo()
display dialog "The current timeZone name is : " & zone_Name of aRecord & linefeed & "The current timeZone abbreviated descriptor is : " & zone_Abbr of aRecord & linefeed & "The current daylightSavingTimeOffset : " & zone_Offset of aRecord & linefeed & "The current isDaylightSavingTime is : " & isDaylightSavingTime of aRecord

Here it displayed:
“The current timeZone name is : Europe/Paris
The current timeZone abbreviated descriptor is : UTC+1
The current daylightSavingTimeOffset : 0,0
The current isDaylightSavingTime is : false”

With the short one :

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	return {isDaylightSavingTime:thisZone's isDaylightSavingTime() as boolean}
end timeZoneInfo


set aRecord to my timeZoneInfo()
display dialog "The current isDaylightSavingTime is : " & isDaylightSavingTime of aRecord

which displayed : “The current isDaylightSavingTime is : false”

As Shane wrote, we may return a single value or a list of values:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

on timeZoneInfo()
	set thisZone to current application's NSTimeZone's localTimeZone()
	set theName to thisZone's |name|() as text
	set theAbbr to thisZone's abbreviation() as text
	set theOffset to thisZone's daylightSavingTimeOffset()
	set isDST to thisZone's isDaylightSavingTime() as boolean
	return {theName, theAbbr, theOffset, isDST}
end timeZoneInfo

set {zone_Name, zone_Abbr, zone_Offset, isDaylightSavingTime} to my timeZoneInfo()
display dialog "The current timeZone name is : " & zone_Name & linefeed & "The current timeZone abbreviated descriptor is : " & zone_Abbr & linefeed & "The current daylightSavingTimeOffset : " & zone_Offset & linefeed & "The current isDaylightSavingTime is : " & isDaylightSavingTime

I wish to add that I am always surprised when askers don’t take care to test the COMPLETE code posted by helpers.
We aren’t paid according to the number of characters in our codes.
It would be logical to assume that if something is written, it’s because it’s USEFUL !
The button [Open this Scriplet in your Editor:] is delivered to help you to grab the true script with no typo.
In an other Web area where we share code too, such button is missing from time to time and it’s annoying :rolleyes:

Last words, I forgot to write that my first proposals were based upon code written by Shane Stanley.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 mars 2020 10:12:41

Thanks again Yvan Koenig!

It wasn’t that I didn’t take care; it was because my knowledge of Apple script usage is not complete - which is why I show up here to ask questions. Now that I know some more than I did before, I won’t make that mistake again. Furthermore, as I amass some experience and knowledge, studying Apple script more formally (if I ever can find a good instruction set) will be easier.

I will look carefully at your further examples to see if I can understand how it works.

I wrote sunset/sunrise calculation AppleScript two years ago.
It requires city name, city latitude, city longitude and time zone name.

http://piyocast.com/as/archives/2338