Long year: Difference between revisions

From Rosetta Code
Content added Content deleted
(julia example)
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
Line 51: Line 51:
</pre>
</pre>


=={{header|REXX}}==
<lang rexx> </lang>


=={{header|Perl 6}}==
=={{header|Perl 6}}==

Revision as of 23:28, 9 January 2020

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.


Julia

<lang julia>using Dates

has53weeks(year) = week(Date(year, 12, 28)) == 53

println(" Year 53 weeks?\n----------------") for year in 1990:2021

   println(year, "   ", has53weeks(year) ? "Yes" : "No")

end

</lang>

Output:
 Year  53 weeks?
----------------
1990   No
1991   No
1992   Yes
1993   No
1994   No
1995   No
1996   No
1997   No
1998   Yes
1999   No
2000   No
2001   No
2002   No
2003   No
2004   Yes
2005   No
2006   No
2007   No
2008   No
2009   Yes
2010   No
2011   No
2012   No
2013   No
2014   No
2015   Yes
2016   No
2017   No
2018   No
2019   No
2020   Yes
2021   No

REXX

<lang rexx> </lang>

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

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; say "\nLong years in the 22nd century:\n", (2100..^2200).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)

Long years in the 22nd century:
(2105 2111 2116 2122 2128 2133 2139 2144 2150 2156 2161 2167 2172 2178 2184 2189 2195)

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.