Calculating start and end of UK summertime

I have a need to calculate when British Summer Time starts and ends. After messing around, the following seems to work in Bash.

echo `date +%Y`-03-`cal 3 \`date +%Y\` | grep -oE "^[[:digit:]]{2}" | tail 
-1`T01:00:00Z

and

echo `date +%Y`-03-`cal 10 \`date +%Y\` | grep -oE "^[[:digit:]]{2}" | tail 
-1`T01:00:00Z

Are there better ways? Would this method work for other countries?

Britain gets summer?

I believe ksh93 has some date arithmetic tools. First Sunday after March/Oct 9 at 1 AM? Typo in October, too much March pasted in. I had a lesser grep, so I use sed:

$ echo `date +%Y`-10-`cal 10 \`date +%Y\` | sed -n 's/^\([0-9]\{2\}\).*/\1/p' | tail -1`T01:00:00Z
2011-10-30T01:00:00Z
$

You can use date as echo, or date once to variable or date once to make a command and eval it. You could call date iterating offsets until it met your template, to drop cal and the cal postprocessing.

Only if they adopt British Summer Time, and then you have to decide the effects of their time zone on date as well as time, and do they match the UK no matter what TZ?

Why not just use zdump and the appropriate timezone file? Look for isdst 1

zdump -v Europe/London | grep 2011

shows that BST started on March 27th and ends on Oct 30th.

1 Like

Some zdump can trim themselves:

zdump -v -c '2011,2011' Europe/London

Still, it begs the question of the need for some utility where you can ask for the dom of a Sunday in Mar 2011 with 9 < dom < 17. I suppose if you took cal apart a bit, it might not be too hard to make a calendar selector utility, maybe just a cal option / enhancement. You might tell it to start at 3/10/2011 and find 1 Sunday.

Well British Summer Time is defined as starting on the last Sunday of March and ending on the last Sunday of October. Our timezone setting is therefore explicitly set to:-

GMT0BST,M3.5.0,M10.5.0

GMT & Britain, of course, being the world standard for time - and quite right too.

One way to get the date would be like this:-

#!/bin/ksh

read YYYY?"Please enter the year required: "
cal 3 $YYYY|grep -v "^$"|tail -1|read BST_start rest
cal 10 $YYYY|grep -v "^$"|tail -1|read BST_end rest

echo "BST starts on $BST_start/3/$YYYY and ends on $BST_end/10/$YYYY"

Does that help?

Robin
Liverpool/Blackburn
UK - as if you couldn't guess.

Thanks for the replies. I guess the algorithm for summertime changeover varies by country. I didn't know about zdump, you have to browse the zoneinfo directory on your machine to find out what strings to use?

Last, not between 10 and 16?
British Summer Time - Wikipedia, the free encyclopedia

The definition for date to change and even the time of change varies with where you are in the world. I'd guess that each country just decided what was best for itself, although there is now a standardised date accross Europe. It caused no end of trouble when our mahines assumed that they were USA and just changed at an unexpected time.

Not much of the world uses Summer Time or Daylight Savings (same concept, different name in the USA) and I suppose that many as you get nearer the equator, is has less of an effect.

Robin
Liverpool/Blackburn
UK

With my datecalc script (which no one seems to remember :() this is pretty easy.

$ cat bst
#! /usr/bin/ksh

alias datecalc=./datecalc
function last_sunday
{
        typeset year=$1
        typeset month=$2
        typeset ldom lsom
        ldom=$(datecalc -l $year $month) || return 1
        ((lsom = ldom - $(datecalc -d $year $month $ldom)))
        echo $lsom
        return 0
}

year=$(date +%Y)
echo year = $year
for month in 3 10 ; do
        cal $month $year
        last=$(last_sunday $year $month)
        echo "last sunday is $year $month $last"
        echo
        echo
done


$
$
$ ./bst
year = 2011
     March 2011
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

last sunday is 2011 3 27


    October 2011
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
last sunday is 2011 10 30


$

It also varies by year.

Well, it is a calendar function, so it varies amidst 7 pairs of dates. I am just saying that a remote place might want to stay N hours off London time, so they might change more in mid-day, and possibly the day before West of London.