Long year: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments.)
(→‎{{header|REXX}}: showed more results to match another example output.)
Line 76: Line 76:
if HI=='' | HI=="," then HI= LO /* " " " " */
if HI=='' | HI=="," then HI= LO /* " " " " */
if HI=='*' then HI= left( date('S'), 4) /*an asterisk ≡ current yr*/
if HI=='*' then HI= left( date('S'), 4) /*an asterisk ≡ current yr*/

do j=LO to HI /*process single yr or range of years.*/
do j=LO to HI /*process single yr or range of years.*/
say ' year ' j " is a " right( word('short long', weeks(j)-51),5) " year"
say ' year ' j " is a " right( word('short long', weeks(j)-51),5) " year"
Line 84: Line 83:
pWeek: parse arg yr; return (yr + yr%4 - yr%100 + yr%400) // 7 /*calculate P*/
pWeek: parse arg yr; return (yr + yr%4 - yr%100 + yr%400) // 7 /*calculate P*/
weeks: parse arg y; $= 52; if pWeek(y)==4 | pWeek(y-1)==3 then $= $ + 1; return $</lang>
weeks: parse arg y; $= 52; if pWeek(y)==4 | pWeek(y-1)==3 then $= $ + 1; return $</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 1990 &nbsp; 2030 </tt>}}
<pre>
<pre>
year 1990 is a short year
year 1990 is a short year

Revision as of 23:43, 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

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)

REXX

<lang rexx>/*REXX program determines if a (calendar) year is a SHORT or LONG year (52 or 53 weeks).*/ parse arg LO HI . /*obtain optional args. */ if LO== | LO=="," | LO=='*' then LO= left( date('S'), 4) /*Not given? Use default.*/ if HI== | HI=="," then HI= LO /* " " " " */ if HI=='*' then HI= left( date('S'), 4) /*an asterisk ≡ current yr*/

    do j=LO  to  HI                             /*process single yr  or range of years.*/
    say '     year '   j   " is a "   right( word('short long', weeks(j)-51),5)   " year"
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ pWeek: parse arg yr; return (yr + yr%4 - yr%100 + yr%400) // 7 /*calculate P*/ weeks: parse arg y; $= 52; if pWeek(y)==4 | pWeek(y-1)==3 then $= $ + 1; return $</lang>

output   when using the inputs of:     1990   2030
     year  1990  is a  short  year
     year  1991  is a  short  year
     year  1992  is a   long  year
     year  1993  is a  short  year
     year  1994  is a  short  year
     year  1995  is a  short  year
     year  1996  is a  short  year
     year  1997  is a  short  year
     year  1998  is a   long  year
     year  1999  is a  short  year
     year  2000  is a  short  year
     year  2001  is a  short  year
     year  2002  is a  short  year
     year  2003  is a  short  year
     year  2004  is a   long  year
     year  2005  is a  short  year
     year  2006  is a  short  year
     year  2007  is a  short  year
     year  2008  is a  short  year
     year  2009  is a   long  year
     year  2010  is a  short  year
     year  2011  is a  short  year
     year  2012  is a  short  year
     year  2013  is a  short  year
     year  2014  is a  short  year
     year  2015  is a   long  year
     year  2016  is a  short  year
     year  2017  is a  short  year
     year  2018  is a  short  year
     year  2019  is a  short  year
     year  2020  is a   long  year
     year  2021  is a  short  year
     year  2022  is a  short  year
     year  2023  is a  short  year
     year  2024  is a  short  year
     year  2025  is a  short  year
     year  2026  is a   long  year
     year  2027  is a  short  year
     year  2028  is a  short  year
     year  2029  is a  short  year
     year  2030  is a  short  year

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.