8
\$\begingroup\$

Daylight saving time (DST), is the practice of advancing clocks (typically by one hour) during warmer months so that darkness falls at a later clock time. The typical implementation of DST is to set clocks forward by one hour in the spring ("spring forward") and set clocks back by one hour in autumn ("fall back") to return to standard time. [from Wikipedia]

Write a program (full or function), takes no input from user, tells user when next Daylight Saving Time clock shift will happen in the following year for users' timezone (as configured on users' system / environment). Output should be the time when DST starts / ends in second precision, or output some other non-ambiguous text / value to tell user there will be no DST start / end happened in the following year.

Rules

  • This is , shortest codes in bytes win as usual;
  • Your program should running on a platform which support at least 12 different timezone, and at least 2 of them have different DST shifts in the following year. So a program simply tells user "there is no DST" on a platform only support UTC would be disqualified.
  • You may output in any reasonable format including (but not limited to) any one of following formats. Please also specify your output format in your post.
    • A Unix timestamp in second / millisecond;
    • Any human readable string in UTC or users timezone;
    • A built-in date time type value as return value of function;
  • If your program output time in users' timezone, you should clarify whether the time outputted had DST counted. This can be done by including UTC offset / timezone name in your output, or specify it in your post (only when all outputs both use / not use DST.
  • For timezone uses DST whole year, you program should tell user no DST start / end will happen;
  • If your program running in some sandbox / emulator / virtual machine / docker environment which provides its own timezone config, you may use the timezone info configured in the runtime environment instead of user's operating system. But again, the environment should support multiple timezone with different DST to make your answer valid.

To keep this challenge simple, you may assume:

  • The timezone will not change its offset to UTC due to other reasons;
  • The offset to UTC of the timezone is an integer in seconds (both before / after DST);
  • The timezone will start / end its DST when Unix timestamp (as in second) is an integer;
  • The timezone will either start / end its DST in the following year or it will never start / end its DST (No DST applied or use DST whole year);

Example Outputs

Following example outputs assume user is running your program in 2021-06-01T00:00:00Z. Your program may need to have different behavior as time past.

America/Anchorage
* 1636279200
* 2021-11-07T10:00:00.000Z
* Sun Nov 07 2021 01:00:00 GMT-0900 (Alaska Standard Time)
* Sun Nov 07 2021 02:00:00 GMT-0800 (Alaska Daylight Time)
 
Australia/Adelaide
* 1633192200
* 2021-10-02T16:30:00.000Z
* Sun Oct 03 2021 03:00:00 GMT+1030 (Australian Central Daylight Time)
* Sun Oct 03 2021 02:00:00 GMT+0930 (Australian Central Standard Time)

Asia/Singapore
* No DST
\$\endgroup\$
5
  • 2
    \$\begingroup\$ So we either have to encode the entire global table of DSTs or use an existing library to look it up? And all of this with the caveat that DSTs change quite frequently. What is the earliest date on which the program must work? \$\endgroup\$
    – Adám
    Jun 2, 2021 at 7:41
  • \$\begingroup\$ @Adám The program only need to work when it post (and maybe a few days later assume DST configures not changed). Using existing library is desired behavior. Whenever your runtime provide an API to know which timezone is user in, it would probably provide some other related APIs to know DST. \$\endgroup\$
    – tsh
    Jun 2, 2021 at 8:16
  • \$\begingroup\$ Are we allowed to error if there are no DST changes coming up? \$\endgroup\$
    – Adám
    Jun 2, 2021 at 9:46
  • \$\begingroup\$ my answer feels like trolling this question :P \$\endgroup\$
    – Wasif
    Jun 2, 2021 at 10:09
  • \$\begingroup\$ @Adám it is ok if the error message is different with output for dst changes. I don’t care if outputs are done via stdout or stderr. \$\endgroup\$
    – tsh
    Jun 2, 2021 at 10:32

5 Answers 5

5
\$\begingroup\$

APL (Dyalog Unicode), 105 103 bytes (SBCS)

-2 by throwing an error if the current location has no upcoming DST

Full program. Prints DateTime in local format (includes seconds), with the special value of midnight on the new-year of year 1 (in the past, so cannot be the real upcoming change time) indicating that there is no upcoming DST change.

⎕USING←'System'
DateTime.Now(⊃+.>↓⊢)∊(TimeZone.CurrentTimeZone.GetDaylightChanges¨0 1+⊃⎕TS).(Start End)

⎕USING←'System' use .NET's standard core library

().(Start End) get the Start and End times for each DST period in:

⎕TS the current TimeStamp as a 7-element list: (Y,M,D,h,m,s,ms)

 pick the first element (the year); 2021

0 1 add zero and one; (2021,2022)

TimeZone.CurrentTimeZone.GetDaylightChanges¨ for each year, get its DST period

enlist (flatten from pair of pairs, to 4-element list)

DateTime.Now() with the current time as left argument, apply the following tacit infix function:

 drop the following number of elements from that:

  +.> the sum of the 4-element Boolean list indicating the change times that the current time is greater than

 pick the first remaining change time

If there are no upcoming DST changes, then all Start and End times are midnight on the new-year of year 1. The current time is greater than all of these values, so we end up with no elements. Trying to pick the first one errors with a NONCE ERROR.

Trial run from the UK currently gives:

31/10/2021 02:00:00
\$\endgroup\$
3
  • \$\begingroup\$ APL is built on .NET? \$\endgroup\$
    – Wasif
    Jun 2, 2021 at 10:05
  • \$\begingroup\$ No, but it is tightly integrated with both .NET Framework and .NET Core. Dyalog APL was one of the languages Microsoft listed as supporting .NET when .NET was first launched. \$\endgroup\$
    – Adám
    Jun 2, 2021 at 10:07
  • \$\begingroup\$ Ah then I would have used Powershell instead of python to use the .NET builtin \$\endgroup\$
    – Wasif
    Jun 2, 2021 at 10:08
4
\$\begingroup\$

PHP, 72 70 bytes

for($d=date(I,$r=$t=time());$d==date(I,++$t);)$t-$r<4e7?:die();echo$t;

Try it online! for 'America/Anchorage', replace the timezone in the header to test another one

This time I'm proud of good ol' PHP! Will output the timestamp of the shift if there is one, or terminates with an empty string if there is no shift.

  • date('I',timestamp) will return 1 if DST is on or 0 if off
  • bruteforces for each second until the value changes
  • if the checked timestamp minus initial timestamp reaches 40000000 (a regular year is 31536000 seconds) it terminates

EDIT: saved 2 bytes by pre-incrementing $t

\$\endgroup\$
1
\$\begingroup\$

Python 3, 183 bytes

from datetime import*
import pytz,time
y=k=datetime.now().replace(microsecond=0)
f=timedelta(seconds=1)
while pytz.timezone(time.tzname[0]).localize(y+f).dst():y+=f
if y!=k:print(y+f)

Try it online!

-63 thanks to @ovs

Test for America Anchorage!

Horrendously slow, but works.

It repeatdly adds 1 second to current date, until it is DST or not.

If the current date is not DST, it exists with empty output.

I tested this by changing timezone (third line, function parameter) and dates (sixth and fifth line). It worked like a charm! The test timezone was Anchorage, and test date was Nov 1, 2021 (Changed date only for faster speed)

\$\endgroup\$
8
  • \$\begingroup\$ I'm unclear on how this works. What did it print for Anchorage on Nov 1, 2021? \$\endgroup\$
    – Adám
    Jun 2, 2021 at 10:12
  • \$\begingroup\$ @Adám it returned '2021-11-07 01:00:00', its very slow because it tries to brute force the date, by incrementing the date by 1 second, until the incremented date is inside DST \$\endgroup\$
    – Wasif
    Jun 2, 2021 at 10:13
  • \$\begingroup\$ What does it print for Anchorage on Mar 10, 2021? \$\endgroup\$
    – Adám
    Jun 2, 2021 at 10:14
  • \$\begingroup\$ @Adám I am on mobile now and running code from TIO so it wouls run for max 60 seconds, it should work definitely if given time, but I can't test right now \$\endgroup\$
    – Wasif
    Jun 2, 2021 at 10:15
  • \$\begingroup\$ How so? From Nov 1, 2021 there are 7 days until the answer. From Mar 10, 2021, there are only 4 days until the answer. \$\endgroup\$
    – Adám
    Jun 2, 2021 at 10:16
0
\$\begingroup\$

JavaScript (Node.js), 172 bytes

for(d=()=>new Date,l=console.log,t=1000,a=d(),b=d(),c=0,i=x=>x.setSeconds(x.getSeconds()+1);(a-d()<32*t**3||l("no DST"))&&(c===t||!c||l(r));i(a),c=a-b,r=~~((+b+t)/t),i(b));

Try it online!

This increments a Date by 1 wall clock time second until it detects that the incremented value is not 1 Unix time second apart from the original one. In such case it prints the Unix timestamp (seconds, integer) of the change. If no gap are found, prints no DST.

\$\endgroup\$
2
0
\$\begingroup\$

Go, 168 bytes

package main
import."time"
func main(){a:=Now()
_,b:=a.Zone()
for c:=a;c.Unix()<a.AddDate(1,0,0).Unix();c=c.Add(Minute){_,d:=c.Zone()
if d!=b{print(c.String())
break}}}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.