Peter
Peter Campbell Smith

Long years and failed palindromes

Weekly challenge 137 — 1 November 2021

Week 137: 1 Nov 2021

Task 1

Task — Long year

Write a script to find every year between 1900 and 2100 which is a Long Year. A year is Long if it extends into an ISO 8601 week 53.

Examples

1903 is a Long Year.

Analysis

Week 1 as defined in ISO 8601 always contains 4 January. That means that 28 December is always within the last week of the preceding year.

The POSIX function strftime has a '%V' formatting tag that returns the ISO week number given a (positive or negative) Unix epoch time.

And that's what my solution does.

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2021-11-01
use utf8;     # Week 137 - task 1 - Long year
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use POSIX 'strftime';
use Time::Local;

long_year();

sub long_year {
    
    my ($year, @t, @answers);
    
    # check for 28 Dec being in week 53
    for $year (1900 .. 2100) {
        @t = localtime(timelocal(0, 0, 12, 28, 11, $year));
        push @answers, $year if strftime('%V', @t) == 53;       
    }
    say qq[Output: ] . join(', ', @answers);
}

Output

Output: 1903, 1908, 1914, 1920, 1925, 1931, 1936, 1942,
1948, 1953, 1959, 1964, 1970, 1976, 1981, 1987, 1992,
1998, 2004, 2009, 2015, 2020, 2026, 2032, 2037, 2043,
2048, 2054, 2060, 2065, 2071, 2076, 2082, 2088, 2093,
2099

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain