Long year

From Rosetta Code
Revision as of 22:57, 9 January 2020 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: Better (less verbose) demo)
Long year is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Most years have 52 weeks, some have 53, according to ISO8601. Write a function which determines if a given year is long (53 weeks) or not, and demonstrate it.


Perl 6

Works with: Rakudo version 2019.11

December 28 is always in the last week of the year. (By ISO8601) <lang perl6>sub is-long ($year) { Date.new("$year-12-28").week[1] == 53 }

  1. Testing
  2. Testing

say "Long years in the 20th century:\n", (1900..2000).grep: *.&is-long; say "\nLong years in the 21st century:\n", (2000..2100).grep: *.&is-long;</lang>

Output:
Long years in the 20th century:
(1903 1908 1914 1920 1925 1931 1936 1942 1948 1953 1959 1964 1970 1976 1981 1987 1992 1998)

Long years in the 21st century:
(2004 2009 2015 2020 2026 2032 2037 2043 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099)

Ruby

<lang ruby> require 'date'

def long_year?(year = Date.today.year)

 Date.new(year, 12, 28).cweek == 53

end

(2020..2030).each{|year| puts "#{year} is long? #{ long_year?(year) }." } </lang>

Output:
2020 is long? true.
2021 is long? false.
2022 is long? false.
2023 is long? false.
2024 is long? false.
2025 is long? false.
2026 is long? true.
2027 is long? false.
2028 is long? false.
2029 is long? false.
2030 is long? false.