Last Friday of each month: Difference between revisions
Content added Content deleted
(→{{header|Picat}}: Split into subsections.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33: | Line 33: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible. |
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible. |
||
< |
<syntaxhighlight lang="360asm">* Last Friday of each month 17/07/2016 |
||
LASTFRI CSECT |
LASTFRI CSECT |
||
USING LASTFRI,R13 base register |
USING LASTFRI,R13 base register |
||
Line 111: | Line 111: | ||
DW DS D packed (PL8) 15num |
DW DS D packed (PL8) 15num |
||
YREGS |
YREGS |
||
END LASTFRI</ |
END LASTFRI</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 129: | Line 129: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">;https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto.27s_methods |
||
BYTE FUNC DayOfWeek(INT y BYTE m,d) ;1<=m<=12, y>1752 |
BYTE FUNC DayOfWeek(INT y BYTE m,d) ;1<=m<=12, y>1752 |
||
BYTE ARRAY t=[0 3 2 5 0 3 5 1 4 6 2 4] |
BYTE ARRAY t=[0 3 2 5 0 3 5 1 4 6 2 4] |
||
Line 193: | Line 193: | ||
PrintB2(last) PutE() |
PrintB2(last) PutE() |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Last_Friday_of_each_month.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Last_Friday_of_each_month.png Screenshot from Atari 8-bit computer] |
||
Line 216: | Line 216: | ||
Uses GNAT. Applicable to any day of the week, cf. [[http://rosettacode.org/wiki/Find_last_sunday_of_each_month#Ada]]. |
Uses GNAT. Applicable to any day of the week, cf. [[http://rosettacode.org/wiki/Find_last_sunday_of_each_month#Ada]]. |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, GNAT.Calendar.Time_IO, Ada.Command_Line, |
||
Ada.Calendar.Formatting, Ada.Calendar.Arithmetic; |
Ada.Calendar.Formatting, Ada.Calendar.Arithmetic; |
||
Line 245: | Line 245: | ||
Put_Line(Selected); |
Put_Line(Selected); |
||
end loop; |
end loop; |
||
end Last_Weekday_In_Month;</ |
end Last_Weekday_In_Month;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>>./last_weekday_in_month friday 2012 |
<pre>>./last_weekday_in_month friday 2012 |
||
Line 264: | Line 264: | ||
Basically the same as the "Find the Last Sunday Of Each Month" task solution. |
Basically the same as the "Find the Last Sunday Of Each Month" task solution. |
||
{{Trans|ALGOL W}} |
{{Trans|ALGOL W}} |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find the last Friday in each month of a year # |
||
# returns true if year is a leap year, false otherwise # |
# returns true if year is a leap year, false otherwise # |
||
# assumes year is in the Gregorian Calendar # |
# assumes year is in the Gregorian Calendar # |
||
Line 314: | Line 314: | ||
) |
) |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 333: | Line 333: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Basically the same as the "Find the Last Sunday Of Each Month" task solution, uses the Day_of_week and isLeapYear procedures from the the day-of-the-week and leap-year tasks (included here for convenience). |
Basically the same as the "Find the Last Sunday Of Each Month" task solution, uses the Day_of_week and isLeapYear procedures from the the day-of-the-week and leap-year tasks (included here for convenience). |
||
< |
<syntaxhighlight lang="algolw">begin % find the last Friday in each month of a year % |
||
% returns true if year is a leap year, false otherwise % |
% returns true if year is a leap year, false otherwise % |
||
% assumes year is in the Gregorian Calendar % |
% assumes year is in the Gregorian Calendar % |
||
Line 390: | Line 390: | ||
for mPos := 1 until 12 do write( year, if mPos < 10 then "-0" else "-1", mPos rem 10, "-", last( mPos ) ) |
for mPos := 1 until 12 do write( year, if mPos < 10 then "-0" else "-1", mPos rem 10, "-", last( mPos ) ) |
||
end |
end |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 410: | Line 410: | ||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
< |
<syntaxhighlight lang="applescript">-- LAST FRIDAYS OF YEAR ------------------------------------------------------ |
||
-- lastFridaysOfYear :: Int -> [Date] |
-- lastFridaysOfYear :: Int -> [Date] |
||
Line 605: | Line 605: | ||
map(column, item 1 of xss) |
map(column, item 1 of xss) |
||
end transpose</ |
end transpose</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2014-01-31 2015-01-30 2016-01-29 2017-01-27 2018-01-26 |
<pre>2014-01-31 2015-01-30 2016-01-29 2017-01-27 2018-01-26 |
||
Line 626: | Line 626: | ||
AppleScript's weekday constants can be coerced either to English text or to the integers 1 (for Sunday) to 7 (Saturday). |
AppleScript's weekday constants can be coerced either to English text or to the integers 1 (for Sunday) to 7 (Saturday). |
||
< |
<syntaxhighlight lang="applescript">on lastFridayOfEachMonthInYear(y) |
||
-- Initialise an AppleScript date to the first day of some month in the specified year. |
-- Initialise an AppleScript date to the first day of some month in the specified year. |
||
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
||
Line 648: | Line 648: | ||
end lastFridayOfEachMonthInYear |
end lastFridayOfEachMonthInYear |
||
lastFridayOfEachMonthInYear(2020)</ |
lastFridayOfEachMonthInYear(2020)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 667: | Line 667: | ||
The above is of course hard-coded for Fridays. It can be made more flexible by taking an AppleScript weekday constant as a parameter: |
The above is of course hard-coded for Fridays. It can be made more flexible by taking an AppleScript weekday constant as a parameter: |
||
< |
<syntaxhighlight lang="applescript">on lastWeekdayWOfEachMonthInYear(w, y) -- Parameters: (AppleScript weekday constant, AD year number) |
||
-- Initialise an AppleScript date to the first day of some month in the specified year. |
-- Initialise an AppleScript date to the first day of some month in the specified year. |
||
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
tell (current date) to set {firstDayOfNextMonth, its day, its year} to {it, 1, y} |
||
Line 691: | Line 691: | ||
end lastWeekdayWOfEachMonthInYear |
end lastWeekdayWOfEachMonthInYear |
||
lastWeekdayWOfEachMonthInYear(Friday, 2020)</ |
lastWeekdayWOfEachMonthInYear(Friday, 2020)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>"./last_Fridays 2020 |
<pre>"./last_Fridays 2020 |
||
Line 709: | Line 709: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">lastFridayForMonth: function [m][ |
||
ensure -> in? m 1..12 |
ensure -> in? m 1..12 |
||
Line 721: | Line 721: | ||
loop 1..12 'month [ |
loop 1..12 'month [ |
||
print to :string.format:"yyyy-MM-dd" lastFridayForMonth month |
print to :string.format:"yyyy-MM-dd" lastFridayForMonth month |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 739: | Line 739: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="ahk">if 1 = ; no parameter passed |
||
{ |
{ |
||
InputBox, 1, Last Fridays of year, Enter a year:, , , , , , , , %A_YYYY% |
InputBox, 1, Last Fridays of year, Enter a year:, , , , , , , , %A_YYYY% |
||
Line 767: | Line 767: | ||
stmp += 1, days |
stmp += 1, days |
||
} |
} |
||
MsgBox % res</ |
MsgBox % res</syntaxhighlight> |
||
{{out}} for 2012: |
{{out}} for 2012: |
||
<pre>2012-01-27 |
<pre>2012-01-27 |
||
Line 784: | Line 784: | ||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
<syntaxhighlight lang="autoit"> |
|||
<lang AutoIt> |
|||
#include <Date.au3> |
#include <Date.au3> |
||
Line 807: | Line 807: | ||
ConsoleWrite($sResult) |
ConsoleWrite($sResult) |
||
EndFunc ;==>_GetFridays |
EndFunc ;==>_GetFridays |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 827: | Line 827: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f LAST_FRIDAY_OF_EACH_MONTH.AWK year |
# syntax: GAWK -f LAST_FRIDAY_OF_EACH_MONTH.AWK year |
||
# converted from Fortran |
# converted from Fortran |
||
Line 845: | Line 845: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 866: | Line 866: | ||
The algorithm has been slightly simplified to avoid the additional day adjustment inside the loop, and the year is obtained from stdin rather than via the command line. |
The algorithm has been slightly simplified to avoid the additional day adjustment inside the loop, and the year is obtained from stdin rather than via the command line. |
||
< |
<syntaxhighlight lang="befunge">":raeY",,,,,&>55+,:::45*:*%\"d"%!*\4%+!3v |
||
v2++1**"I"5\+/*:*54\-/"d"\/4::-1::p53+g5< |
v2++1**"I"5\+/*:*54\-/"d"\/4::-1::p53+g5< |
||
>:00p5g4-+7%\:0\v>,"-",5g+:55+/68*+,55+%v |
>:00p5g4-+7%\:0\v>,"-",5g+:55+/68*+,55+%v |
||
^<<_$$vv*86%+55:<^+*86%+55,+*86/+55:-1:<6 |
^<<_$$vv*86%+55:<^+*86%+55,+*86/+55:-1:<6 |
||
>$$^@$<>+\55+/:#^_$>:#,_$"-",\:04-\-00g^8 |
>$$^@$<>+\55+/:#^_$>:#,_$"-",\:04-\-00g^8 |
||
^<# #"#"##"#"##!` +76:+1g00,+55,+*<</ |
^<# #"#"##"#"##!` +76:+1g00,+55,+*<</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 892: | Line 892: | ||
Doesn't work with Julian calendar (then again, you probably don't need to plan your weekends for middle ages). |
Doesn't work with Julian calendar (then again, you probably don't need to plan your weekends for middle ages). |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 911: | Line 911: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Globalization; |
using System.Globalization; |
||
Line 951: | Line 951: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>01/27/2012 |
<pre>01/27/2012 |
||
Line 969: | Line 969: | ||
{{libheader|Boost}} |
{{libheader|Boost}} |
||
called with <code>./last_fridays 2012</code> |
called with <code>./last_fridays 2012</code> |
||
< |
<syntaxhighlight lang="cpp">#include <boost/date_time/gregorian/gregorian.hpp> |
||
#include <iostream> |
#include <iostream> |
||
#include <cstdlib> |
#include <cstdlib> |
||
Line 985: | Line 985: | ||
} |
} |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2012-Jan-27 |
<pre>2012-Jan-27 |
||
Line 1,004: | Line 1,004: | ||
{{libheader|clj-time}} |
{{libheader|clj-time}} |
||
< |
<syntaxhighlight lang="clojure">(use '[clj-time.core :only [last-day-of-the-month day-of-week minus days]] |
||
'[clj-time.format :only [unparse formatters]]) |
'[clj-time.format :only [unparse formatters]]) |
||
Line 1,014: | Line 1,014: | ||
(defn last-fridays-formatted [year] |
(defn last-fridays-formatted [year] |
||
(sort (map #(unparse (formatters :year-month-day) %) (last-fridays year))))</ |
(sort (map #(unparse (formatters :year-month-day) %) (last-fridays year))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,032: | Line 1,032: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang="cobol"> |
|||
<lang COBOL> |
|||
program-id. last-fri. |
program-id. last-fri. |
||
data division. |
data division. |
||
Line 1,081: | Line 1,081: | ||
. |
. |
||
end program last-fri. |
end program last-fri. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,100: | Line 1,100: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
last_friday_of_month = (year, month) -> |
last_friday_of_month = (year, month) -> |
||
# month is 1-based, JS API is 0-based, then we use |
# month is 1-based, JS API is 0-based, then we use |
||
Line 1,119: | Line 1,119: | ||
year = parseInt process.argv[2] |
year = parseInt process.argv[2] |
||
print_last_fridays_of_month year |
print_last_fridays_of_month year |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<lang> |
<syntaxhighlight lang="text"> |
||
> coffee last_friday.coffee 2012 |
> coffee last_friday.coffee 2012 |
||
Fri Jan 27 2012 |
Fri Jan 27 2012 |
||
Line 1,135: | Line 1,135: | ||
Fri Nov 30 2012 |
Fri Nov 30 2012 |
||
Fri Dec 28 2012 |
Fri Dec 28 2012 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 1,141: | Line 1,141: | ||
The command-line argument processing is the only CLISP-specific code. |
The command-line argument processing is the only CLISP-specific code. |
||
< |
<syntaxhighlight lang="lisp">(defun friday-before (year month day) |
||
(let* |
(let* |
||
((timestamp (encode-universal-time 0 0 12 day month year)) |
((timestamp (encode-universal-time 0 0 12 day month year)) |
||
Line 1,154: | Line 1,154: | ||
(let* ((year (read-from-string (car *args*)))) |
(let* ((year (read-from-string (car *args*)))) |
||
(format t "~{~{~a-~2,'0d-~2,'0d~}~%~}" (last-fridays year)))</ |
(format t "~{~{~a-~2,'0d-~2,'0d~}~%~}" (last-fridays year)))</syntaxhighlight> |
||
Sample run for the year 2015: |
Sample run for the year 2015: |
||
Line 1,172: | Line 1,172: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.datetime, std.traits; |
||
void lastFridays(in uint year) { |
void lastFridays(in uint year) { |
||
Line 1,186: | Line 1,186: | ||
void main() { |
void main() { |
||
lastFridays(2012); |
lastFridays(2012); |
||
}</ |
}</syntaxhighlight> |
||
<pre>2012-Jan-27 |
<pre>2012-Jan-27 |
||
2012-Feb-24 |
2012-Feb-24 |
||
Line 1,202: | Line 1,202: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Uses the standard Delphi library. |
Uses the standard Delphi library. |
||
< |
<syntaxhighlight lang="delphi">program LastFridayOfMonth; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,228: | Line 1,228: | ||
WriteLn(DateToStr(D1)); |
WriteLn(DateToStr(D1)); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter year: 2019 |
<pre>Enter year: 2019 |
||
Line 1,245: | Line 1,245: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def lastFriday(year) do |
def lastFriday(year) do |
||
Enum.map(1..12, fn month -> |
Enum.map(1..12, fn month -> |
||
Line 1,259: | Line 1,259: | ||
Enum.each(RC.lastFriday(y), fn {year, month, day} -> |
Enum.each(RC.lastFriday(y), fn {year, month, day} -> |
||
:io.format "~4b-~2..0w-~2..0w~n", [year, month, day] |
:io.format "~4b-~2..0w-~2..0w~n", [year, month, day] |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,278: | Line 1,278: | ||
=={{header|Elm}}== |
=={{header|Elm}}== |
||
< |
<syntaxhighlight lang="elm">import Html exposing (Html, Attribute, text, div, input) |
||
import Html.App exposing (beginnerProgram) |
import Html.App exposing (beginnerProgram) |
||
import Html.Attributes exposing (placeholder, value, style) |
import Html.Attributes exposing (placeholder, value, style) |
||
Line 1,344: | Line 1,344: | ||
, view = view |
, view = view |
||
, update = update |
, update = update |
||
}</ |
}</syntaxhighlight> |
||
Link to live demo: http://dc25.github.io/lastFridayOfMonthElm/ |
Link to live demo: http://dc25.github.io/lastFridayOfMonthElm/ |
||
Line 1,364: | Line 1,364: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(require 'calendar) |
||
(defun last-friday (year) |
(defun last-friday (year) |
||
Line 1,377: | Line 1,377: | ||
(number-sequence 1 12))) |
(number-sequence 1 12))) |
||
(last-friday 2012)</ |
(last-friday 2012)</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>2012-01-27 |
<pre>2012-01-27 |
||
Line 1,393: | Line 1,393: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( last_date_each_month ). |
-module( last_date_each_month ). |
||
Line 1,412: | Line 1,412: | ||
Months_days = [{X, Y} || X <- Months, Y <- lists:seq(calendar:last_day_of_the_month(Year, X), calendar:last_day_of_the_month(Year, X) - 7, -1), calendar:valid_date(Year, X, Y), calendar:day_of_the_week(Year, X, Y) =:= Week_day], |
Months_days = [{X, Y} || X <- Months, Y <- lists:seq(calendar:last_day_of_the_month(Year, X), calendar:last_day_of_the_month(Year, X) - 7, -1), calendar:valid_date(Year, X, Y), calendar:day_of_the_week(Year, X, Y) =:= Week_day], |
||
[{Year, X, proplists:get_value(X, Months_days)} || X <- Months]. |
[{Year, X, proplists:get_value(X, Months_days)} || X <- Months]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,433: | Line 1,433: | ||
{{works with|Factor|0.98}} |
{{works with|Factor|0.98}} |
||
The <code>last-friday-of-month</code> word in the <code>calendar</code> vocabulary does most of the work. This program expects the year as a command line argument. |
The <code>last-friday-of-month</code> word in the <code>calendar</code> vocabulary does most of the work. This program expects the year as a command line argument. |
||
< |
<syntaxhighlight lang="factor">USING: calendar calendar.format command-line io kernel math.parser sequences ; |
||
IN: rosetta-code.last-fridays |
IN: rosetta-code.last-fridays |
||
(command-line) second string>number <year> 12 <iota> |
(command-line) second string>number <year> 12 <iota> |
||
[ months time+ last-friday-of-month ] with map |
[ months time+ last-friday-of-month ] with map |
||
[ timestamp>ymd print ] each</ |
[ timestamp>ymd print ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,459: | Line 1,459: | ||
Algorithm: compute day of week for last day of month, then subtract just enough to get to the preceding friday. Do this for each month. To simplify computations further, we only need to compute day of week of january 1st (the others are found by adding month lengths). Since day of week need only be known modulo 7, we do not compute modulo at all except once when subtracting. |
Algorithm: compute day of week for last day of month, then subtract just enough to get to the preceding friday. Do this for each month. To simplify computations further, we only need to compute day of week of january 1st (the others are found by adding month lengths). Since day of week need only be known modulo 7, we do not compute modulo at all except once when subtracting. |
||
< |
<syntaxhighlight lang="fortran">program fridays |
||
implicit none |
implicit none |
||
integer :: days(1:12) = (/31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/) |
integer :: days(1:12) = (/31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/) |
||
Line 1,472: | Line 1,472: | ||
end do |
end do |
||
end program |
end program |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 23-06-2015 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,559: | Line 1,559: | ||
Loop |
Loop |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>For what year do you want to find the last Friday of the month |
<pre>For what year do you want to find the last Friday of the month |
||
Line 1,579: | Line 1,579: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">d = parseDate[ARGS@0] |
||
for m = 1 to 12 |
for m = 1 to 12 |
||
{ |
{ |
||
Line 1,585: | Line 1,585: | ||
n = d - (((parseInt[d -> ### u ###] + 1) mod 7) + 1) days |
n = d - (((parseInt[d -> ### u ###] + 1) mod 7) + 1) days |
||
println[n -> ### yyyy-MM-dd ###] |
println[n -> ### yyyy-MM-dd ###] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,611: | Line 1,611: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas">Public Sub Form_Open() |
||
Dim siYear As Short = InputBox("Please input a year", "Last Friday of each month") |
Dim siYear As Short = InputBox("Please input a year", "Last Friday of each month") |
||
Dim siMonth, siDay As Short |
Dim siMonth, siDay As Short |
||
Line 1,630: | Line 1,630: | ||
Me.Close |
Me.Close |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,648: | Line 1,648: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,669: | Line 1,669: | ||
fmt.Println(d.Format("2006-01-02")) |
fmt.Println(d.Format("2006-01-02")) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,691: | Line 1,691: | ||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def ymd = { it.format('yyyy-MM-dd') } |
||
def lastFridays = lastWeekDays.curry(Day.Fri) |
def lastFridays = lastWeekDays.curry(Day.Fri) |
||
lastFridays(args[0] as int).each { println (ymd(it)) }</ |
lastFridays(args[0] as int).each { println (ymd(it)) }</syntaxhighlight> |
||
Execution (Cygwin on Windows 7): |
Execution (Cygwin on Windows 7): |
||
Line 1,713: | Line 1,713: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Time.Calendar |
||
(Day, addDays, showGregorian, fromGregorian, gregorianMonthLength) |
(Day, addDays, showGregorian, fromGregorian, gregorianMonthLength) |
||
import Data.Time.Calendar.WeekDate (toWeekDate) |
import Data.Time.Calendar.WeekDate (toWeekDate) |
||
Line 1,738: | Line 1,738: | ||
mapM_ |
mapM_ |
||
putStrLn |
putStrLn |
||
(intercalate " " <$> transpose (weekDayDates 5 <$> [2012 .. 2017]))</ |
(intercalate " " <$> transpose (weekDayDates 5 <$> [2012 .. 2017]))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2012-01-27 2013-01-25 2014-01-31 2015-01-30 2016-01-29 2017-01-27 |
<pre>2012-01-27 2013-01-25 2014-01-31 2015-01-30 2016-01-29 2017-01-27 |
||
Line 1,755: | Line 1,755: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
This will write the last fridays for every year given as an argument. There is no error checking on the year. |
This will write the last fridays for every year given as an argument. There is no error checking on the year. |
||
< |
<syntaxhighlight lang="icon">procedure main(A) |
||
every write(lastfridays(!A)) |
every write(lastfridays(!A)) |
||
end |
end |
||
Line 1,774: | Line 1,774: | ||
end |
end |
||
link datetime, printf</ |
link datetime, printf</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,797: | Line 1,797: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">require 'dates' |
||
last_fridays=: 12 {. [: ({:/.~ }:"1)@(#~ 5 = weekday)@todate (i.366) + todayno@,&1 1</ |
last_fridays=: 12 {. [: ({:/.~ }:"1)@(#~ 5 = weekday)@todate (i.366) + todayno@,&1 1</syntaxhighlight> |
||
In other words, start from January 1 of the given year, and count forward for 366 days, keeping the Fridays. Then pick the last remaining day within each represented month (which will be a Friday because we only kept the Fridays). Then pick the first 12 (since on a non-leap year which ends on a Thursday we would get an extra Friday). |
In other words, start from January 1 of the given year, and count forward for 366 days, keeping the Fridays. Then pick the last remaining day within each represented month (which will be a Friday because we only kept the Fridays). Then pick the first 12 (since on a non-leap year which ends on a Thursday we would get an extra Friday). |
||
Line 1,804: | Line 1,804: | ||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> last_fridays 2012 |
||
2012 1 27 |
2012 1 27 |
||
2012 2 24 |
2012 2 24 |
||
Line 1,816: | Line 1,816: | ||
2012 10 26 |
2012 10 26 |
||
2012 11 30 |
2012 11 30 |
||
2012 12 28</ |
2012 12 28</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java5">import java.text.*; |
||
import java.util.*; |
import java.util.*; |
||
Line 1,845: | Line 1,845: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} (for <code>java LastFridays 2012</code>): |
{{out}} (for <code>java LastFridays 2012</code>): |
||
<pre>2012 Jan 27 |
<pre>2012 Jan 27 |
||
Line 1,864: | Line 1,864: | ||
====Iteration==== |
====Iteration==== |
||
{{works with|Nodejs}} |
{{works with|Nodejs}} |
||
< |
<syntaxhighlight lang="javascript">var last_friday_of_month, print_last_fridays_of_month; |
||
last_friday_of_month = function(year, month) { |
last_friday_of_month = function(year, month) { |
||
Line 1,891: | Line 1,891: | ||
year = parseInt(process.argv[2]); |
year = parseInt(process.argv[2]); |
||
return print_last_fridays_of_month(year); |
return print_last_fridays_of_month(year); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>>node lastfriday.js 2015 |
<pre>>node lastfriday.js 2015 |
||
Line 1,908: | Line 1,908: | ||
====Functional composition==== |
====Functional composition==== |
||
< |
<syntaxhighlight lang="javascript">(function () { |
||
'use strict'; |
'use strict'; |
||
Line 1,978: | Line 1,978: | ||
}) |
}) |
||
.join('\n'); |
.join('\n'); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,995: | Line 1,995: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 2,074: | Line 2,074: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2015-01-30 2016-01-29 2017-01-27 2018-01-26 2019-01-25 |
<pre>2015-01-30 2016-01-29 2017-01-27 2018-01-26 2019-01-25 |
||
Line 2,092: | Line 2,092: | ||
{{ works with|jq|1.4}} |
{{ works with|jq|1.4}} |
||
'''Foundations''' |
'''Foundations''' |
||
< |
<syntaxhighlight lang="jq"># In case your jq does not have "until" defined: |
||
def until(cond; next): |
def until(cond; next): |
||
Line 2,118: | Line 2,118: | ||
| if iso == "iso" or iso == "ISO" then 1 + ((. + 5) % 7) |
| if iso == "iso" or iso == "ISO" then 1 + ((. + 5) % 7) |
||
else . % 7 |
else . % 7 |
||
end ;</ |
end ;</syntaxhighlight> |
||
'''findLastFridays''' |
'''findLastFridays''' |
||
< |
<syntaxhighlight lang="jq"># year and month are numbered conventionally |
||
def findLastFriday(year; month): |
def findLastFriday(year; month): |
||
def isLeapYear: |
def isLeapYear: |
||
Line 2,141: | Line 2,141: | ||
(range(0;12) | "\(months[.]) \(findLastFriday($year; .+1))") ; |
(range(0;12) | "\(months[.]) \(findLastFriday($year; .+1))") ; |
||
$year|tonumber|findLastFridays</ |
$year|tonumber|findLastFridays</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq --arg year 2012 -n -r -f findLastFridays.jq |
||
YEAR: 2012 |
YEAR: 2012 |
||
January 27 |
January 27 |
||
Line 2,156: | Line 2,156: | ||
October 26 |
October 26 |
||
November 30 |
November 30 |
||
December 28</ |
December 28</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Dates |
||
const wday = Dates.Fri |
const wday = Dates.Fri |
||
Line 2,184: | Line 2,184: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
This script will print the last Fridays of each month of the year given. |
This script will print the last Fridays of each month of the year given. |
||
Line 2,211: | Line 2,211: | ||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang="k"> |
|||
<lang K> |
|||
/ List the dates of last Fridays of each month of |
/ List the dates of last Fridays of each month of |
||
/ a given year |
/ a given year |
||
Line 2,225: | Line 2,225: | ||
main: {[y]; lfd1[y];`0: ,"Dates of last Fridays of ",($y);12 10#arr} |
main: {[y]; lfd1[y];`0: ,"Dates of last Fridays of ",($y);12 10#arr} |
||
</syntaxhighlight> |
|||
</lang> |
|||
The output of a session is given below: |
The output of a session is given below: |
||
Line 2,251: | Line 2,251: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
import java.util.* |
import java.util.* |
||
Line 2,273: | Line 2,273: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,294: | Line 2,294: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">define isLeapYear(y::integer) => { |
||
#y % 400 == 0 ? return true |
#y % 400 == 0 ? return true |
||
#y % 100 == 0 ? return false |
#y % 100 == 0 ? return false |
||
Line 2,317: | Line 2,317: | ||
with f in fridays(2012) do => {^ |
with f in fridays(2012) do => {^ |
||
#f->format('%Q') + '\r' |
#f->format('%Q') + '\r' |
||
^}</ |
^}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,334: | Line 2,334: | ||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">function lastFriday yyyy |
||
-- year,month num,day of month,hour in 24-hour time,minute,second,numeric day of week. |
-- year,month num,day of month,hour in 24-hour time,minute,second,numeric day of week. |
||
convert the long date to dateitems |
convert the long date to dateitems |
||
Line 2,356: | Line 2,356: | ||
sort fridays ascending numeric |
sort fridays ascending numeric |
||
return fridays |
return fridays |
||
end lastFriday</ |
end lastFriday</syntaxhighlight> |
||
Example< |
Example<syntaxhighlight lang="livecode">put lastFriday("2012")</syntaxhighlight>Output<syntaxhighlight lang="livecode">1 27 |
||
2 24 |
2 24 |
||
3 30 |
3 30 |
||
Line 2,368: | Line 2,368: | ||
10 26 |
10 26 |
||
11 30 |
11 30 |
||
12 28</ |
12 28</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">; Determine if a Gregorian calendar year is leap |
||
to leap? :year |
to leap? :year |
||
output (and |
output (and |
||
Line 2,415: | Line 2,415: | ||
print reduce [(word ?1 "- ?2)] (list :year :month :day) |
print reduce [(word ?1 "- ?2)] (list :year :month :day) |
||
] |
] |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,433: | Line 2,433: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function isLeapYear (y) |
||
return (y % 4 == 0 and y % 100 ~=0) or y % 400 == 0 |
return (y % 4 == 0 and y % 100 ~=0) or y % 400 == 0 |
||
end |
end |
||
Line 2,452: | Line 2,452: | ||
end |
end |
||
lastWeekdays("Friday", tonumber(arg[1]))</ |
lastWeekdays("Friday", tonumber(arg[1]))</syntaxhighlight> |
||
Command line session: |
Command line session: |
||
<pre>>lua lastFridays.lua 2012 |
<pre>>lua lastFridays.lua 2012 |
||
Line 2,471: | Line 2,471: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">fridays := proc(year) |
||
local i, dt, change, last_days; |
local i, dt, change, last_days; |
||
last_days := [31,28,31,30,31,30,31,31,30,31,30,31]; |
last_days := [31,28,31,30,31,30,31,31,30,31,30,31]; |
||
Line 2,488: | Line 2,488: | ||
end proc; |
end proc; |
||
fridays(2012);</ |
fridays(2012);</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre>2012-1-27 |
<pre>2012-1-27 |
||
Line 2,504: | Line 2,504: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">FridaysOfYear[Y_] := |
||
NestWhile[(DaysPlus[#, - 1]) &, #, (DateString[#, "DayName"] != "Friday") &] & /@ |
NestWhile[(DaysPlus[#, - 1]) &, #, (DateString[#, "DayName"] != "Friday") &] & /@ |
||
Most@Reverse@NestList [DaysPlus[# /. {x_, y_, X_} -> {x, y, 1}, - 1] &, {Y + 1, 1, 1}, 12] |
Most@Reverse@NestList [DaysPlus[# /. {x_, y_, X_} -> {x, y, 1}, - 1] &, {Y + 1, 1, 1}, 12] |
||
Column@FridaysOfYear[2012]</ |
Column@FridaysOfYear[2012]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{2012,1,27} |
<pre>{2012,1,27} |
||
Line 2,524: | Line 2,524: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> function t = last_fridays_of_year(y) |
||
t1 = datenum([y,1,1,0,0,0]); |
t1 = datenum([y,1,1,0,0,0]); |
||
t2 = datenum([y,12,31,0,0,0]); |
t2 = datenum([y,12,31,0,0,0]); |
||
Line 2,533: | Line 2,533: | ||
datestr(last_fridays_of_year(2012),'yyyy-mm-dd') |
datestr(last_fridays_of_year(2012),'yyyy-mm-dd') |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,551: | Line 2,551: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">weekday(year, month, day) := block([m: month, y: year, k], |
||
if m < 3 then (m: m + 12, y: y - 1), |
if m < 3 then (m: m + 12, y: y - 1), |
||
k: 1 + remainder(day + quotient((m + 1)*26, 10) + y + quotient(y, 4) |
k: 1 + remainder(day + quotient((m + 1)*26, 10) + y + quotient(y, 4) |
||
Line 2,570: | Line 2,570: | ||
lastfridays(2012); |
lastfridays(2012); |
||
["2012-1-27", "2012-2-24", "2012-3-30", "2012-4-27", "2012-5-25", "2012-6-29", |
["2012-1-27", "2012-2-24", "2012-3-30", "2012-4-27", "2012-5-25", "2012-6-29", |
||
"2012-7-27","2012-8-31", "2012-9-28", "2012-10-26", "2012-11-30", "2012-12-28"]</ |
"2012-7-27","2012-8-31", "2012-9-28", "2012-10-26", "2012-11-30", "2012-12-28"]</syntaxhighlight> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">import Nanoquery.Util |
||
// a function to check if a year is a leap year |
// a function to check if a year is a leap year |
||
Line 2,623: | Line 2,623: | ||
print form(friday.getMonth()) + "-" |
print form(friday.getMonth()) + "-" |
||
println form(friday.getDay()) |
println form(friday.getDay()) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
Line 2,629: | Line 2,629: | ||
{{trans|C}} |
{{trans|C}} |
||
Implements the algorithms from both the [[#Java|Java]] and [[#C|C]] implementations. |
Implements the algorithms from both the [[#Java|Java]] and [[#C|C]] implementations. |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 2,705: | Line 2,705: | ||
end |
end |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,740: | Line 2,740: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import os, strutils, times |
||
const |
const |
||
Line 2,753: | Line 2,753: | ||
var date = initDateTime(lastDay, month, year, 0, 0, 0) |
var date = initDateTime(lastDay, month, year, 0, 0, 0) |
||
date = date - days(DayDiffs[date.weekday]) |
date = date - days(DayDiffs[date.weekday]) |
||
echo date.format("yyyy-MM-dd")</ |
echo date.format("yyyy-MM-dd")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,775: | Line 2,775: | ||
Using the module [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html Unix] from the standard OCaml library: |
Using the module [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html Unix] from the standard OCaml library: |
||
< |
<syntaxhighlight lang="ocaml">#load "unix.cma" |
||
open Unix |
open Unix |
||
Line 2,810: | Line 2,810: | ||
done; |
done; |
||
done; |
done; |
||
Array.iter print_date fridays</ |
Array.iter print_date fridays</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,830: | Line 2,830: | ||
{{libheader|OCaml Calendar Library}} |
{{libheader|OCaml Calendar Library}} |
||
< |
<syntaxhighlight lang="ocaml">open CalendarLib |
||
let usage() = |
let usage() = |
||
Line 2,854: | Line 2,854: | ||
aux num_days |
aux num_days |
||
done; |
done; |
||
List.iter print_date (List.rev !fridays)</ |
List.iter print_date (List.rev !fridays)</syntaxhighlight> |
||
Run this script with the command: |
Run this script with the command: |
||
Line 2,862: | Line 2,862: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">import: date |
||
: lastFridays(y) |
: lastFridays(y) |
||
Line 2,870: | Line 2,870: | ||
while(dup dayOfWeek Date.FRIDAY <>) [ addDays(-1) ] |
while(dup dayOfWeek Date.FRIDAY <>) [ addDays(-1) ] |
||
println |
println |
||
] ;</ |
] ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,890: | Line 2,890: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">\\ Normalized Julian Day Number from date |
||
njd(D) = |
njd(D) = |
||
{ |
{ |
||
Line 2,916: | Line 2,916: | ||
} |
} |
||
for (m=1, 12, a=njd([2012,m+1,0]); print(njdate(a-(a+1)%7)))</ |
for (m=1, 12, a=njd([2012,m+1,0]); print(njdate(a-(a+1)%7)))</syntaxhighlight> |
||
Output:<pre> |
Output:<pre> |
||
Line 2,935: | Line 2,935: | ||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
Using Free Pascal's DateUtils library would dramatically simplify the coding (see the Delphi example) but for older Pascal implementations the needed routines are the programmer's responsibility. |
Using Free Pascal's DateUtils library would dramatically simplify the coding (see the Delphi example) but for older Pascal implementations the needed routines are the programmer's responsibility. |
||
<syntaxhighlight lang="pascal"> |
|||
<lang Pascal> |
|||
program LastFriday; |
program LastFriday; |
||
Line 3,021: | Line 3,021: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,041: | Line 3,041: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl -w |
||
use strict ; |
use strict ; |
||
use DateTime ; |
use DateTime ; |
||
Line 3,052: | Line 3,052: | ||
} |
} |
||
say $dt->ymd ; |
say $dt->ymd ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2012-01-27 |
<pre>2012-01-27 |
||
Line 3,069: | Line 3,069: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">last_day_of_month</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dow</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">procedure</span> <span style="color: #000000;">last_day_of_month</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dow</span><span style="color: #0000FF;">)</span> |
||
Line 3,082: | Line 3,082: | ||
--last_day_of_month(prompt_number("Year:",{1752,9999}),FRIDAY)</span> |
--last_day_of_month(prompt_number("Year:",{1752,9999}),FRIDAY)</span> |
||
<span style="color: #000000;">last_day_of_month</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2012</span><span style="color: #0000FF;">,</span><span style="color: #000000;">FRIDAY</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">last_day_of_month</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2012</span><span style="color: #0000FF;">,</span><span style="color: #000000;">FRIDAY</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,102: | Line 3,102: | ||
PHP is generally used for web apps, so I am not implementing the command-line component of this task. |
PHP is generally used for web apps, so I am not implementing the command-line component of this task. |
||
< |
<syntaxhighlight lang="php"><?php |
||
function last_friday_of_month($year, $month) { |
function last_friday_of_month($year, $month) { |
||
$day = 0; |
$day = 0; |
||
Line 3,123: | Line 3,123: | ||
$year = 2012; |
$year = 2012; |
||
print_last_fridays_of_month($year); |
print_last_fridays_of_month($year); |
||
?></ |
?></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,142: | Line 3,142: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">% for command line argument |
||
main(ARGV) => |
main(ARGV) => |
||
if ARGV.length > 0 then |
if ARGV.length > 0 then |
||
Line 3,200: | Line 3,200: | ||
(Year mod 4 == 0, Year mod 100 != 0) |
(Year mod 4 == 0, Year mod 100 != 0) |
||
; |
; |
||
Year mod 400 == 0. </ |
Year mod 400 == 0. </syntaxhighlight> |
||
===Running the program=== |
===Running the program=== |
||
Line 3,233: | Line 3,233: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de lastFridays (Y) |
||
(for M 12 |
(for M 12 |
||
(prinl |
(prinl |
||
Line 3,239: | Line 3,239: | ||
(find '((D) (= "Friday" (day D))) |
(find '((D) (= "Friday" (day D))) |
||
(mapcar '((D) (date Y M D)) `(range 31 22)) ) |
(mapcar '((D) (date Y M D)) `(range 31 22)) ) |
||
"-" ) ) ) )</ |
"-" ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">: (lastFridays 2012) |
||
2012-01-27 |
2012-01-27 |
||
2012-02-24 |
2012-02-24 |
||
Line 3,253: | Line 3,253: | ||
2012-10-26 |
2012-10-26 |
||
2012-11-30 |
2012-11-30 |
||
2012-12-28</ |
2012-12-28</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int(0..1) last_friday(object day) |
||
{ |
{ |
||
return day->week_day() == 5 && |
return day->week_day() == 5 && |
||
Line 3,267: | Line 3,267: | ||
write("%{%s\n%}", days->format_ymd()); |
write("%{%s\n%}", days->format_ymd()); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
Fridays: procedure (year) options (main); /* 8 January 2013 */ |
Fridays: procedure (year) options (main); /* 8 January 2013 */ |
||
declare year character (4) varying; |
declare year character (4) varying; |
||
Line 3,293: | Line 3,293: | ||
end; |
end; |
||
end Fridays; |
end Fridays; |
||
</syntaxhighlight> |
|||
</lang> |
|||
The command: FRIDAYS /2008 produces: |
The command: FRIDAYS /2008 produces: |
||
<pre> |
<pre> |
||
Line 3,328: | Line 3,328: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function last-dayofweek { |
function last-dayofweek { |
||
param( |
param( |
||
Line 3,342: | Line 3,342: | ||
} |
} |
||
last-dayofweek 2012 "Friday" |
last-dayofweek 2012 "Friday" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 3,362: | Line 3,362: | ||
This script finds the first and/or last or all dates of any of the days of week; accepts <code>[Int32]</code> and <code>[DateTime]</code> values for Month and Year parameters; outputs <code>[DateTime]</code> objects by default but has an option to output time strings in various formats. This script also allows for pipeline input based mainly upon the Month parameter. |
This script finds the first and/or last or all dates of any of the days of week; accepts <code>[Int32]</code> and <code>[DateTime]</code> values for Month and Year parameters; outputs <code>[DateTime]</code> objects by default but has an option to output time strings in various formats. This script also allows for pipeline input based mainly upon the Month parameter. |
||
This script has a syntax as complex as any PowerShell Cmdlet because it attempts to do everything. |
This script has a syntax as complex as any PowerShell Cmdlet because it attempts to do everything. |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Get-Date0fDayOfWeek |
function Get-Date0fDayOfWeek |
||
{ |
{ |
||
Line 3,460: | Line 3,460: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
The default is to return <code>[DateTime]</code> objects: |
The default is to return <code>[DateTime]</code> objects: |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday |
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,481: | Line 3,481: | ||
</pre> |
</pre> |
||
Return the <code>[DateTime]</code> objects as strings (using the default string format): |
Return the <code>[DateTime]</code> objects as strings (using the default string format): |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString |
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,500: | Line 3,500: | ||
</pre> |
</pre> |
||
Return the <code>[DateTime]</code> objects as strings (specifying the string format): |
Return the <code>[DateTime]</code> objects as strings (specifying the string format): |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString -Format yyyy-MM-dd |
1..12 | Get-Date0fDayOfWeek -Year 2012 -Last -Friday -AsString -Format yyyy-MM-dd |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,520: | Line 3,520: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure LastFridayOfEachMonth(yyyy.i,List lfem.i()) |
||
Define dv.i=ParseDate("%yyyy",Str(yyyy)), mv.i=1 |
Define dv.i=ParseDate("%yyyy",Str(yyyy)), mv.i=1 |
||
NewList d.i() |
NewList d.i() |
||
Line 3,553: | Line 3,553: | ||
EndIf |
EndIf |
||
Print("...End") |
Print("...End") |
||
Input()</ |
Input()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input Year [ 1971 < y < 2038 ]: 2017 |
<pre>Input Year [ 1971 < y < 2038 ]: 2017 |
||
Line 3,572: | Line 3,572: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">import calendar |
||
def last_fridays(year): |
def last_fridays(year): |
||
Line 3,578: | Line 3,578: | ||
last_friday = max(week[calendar.FRIDAY] |
last_friday = max(week[calendar.FRIDAY] |
||
for week in calendar.monthcalendar(year, month)) |
for week in calendar.monthcalendar(year, month)) |
||
print('{:4d}-{:02d}-{:02d}'.format(year, month, last_friday))</ |
print('{:4d}-{:02d}-{:02d}'.format(year, month, last_friday))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,595: | Line 3,595: | ||
2012-12-28</pre> |
2012-12-28</pre> |
||
Another solution |
Another solution |
||
< |
<syntaxhighlight lang="python">import calendar |
||
c=calendar.Calendar() |
c=calendar.Calendar() |
||
fridays={} |
fridays={} |
||
Line 3,609: | Line 3,609: | ||
for item in sorted((month+"-"+day for month,day in fridays.items()), |
for item in sorted((month+"-"+day for month,day in fridays.items()), |
||
key=lambda x:int(x.split("-")[1])): |
key=lambda x:int(x.split("-")[1])): |
||
print item</ |
print item</syntaxhighlight> |
||
Using reduce |
Using reduce |
||
< |
<syntaxhighlight lang="python">import calendar |
||
c=calendar.Calendar() |
c=calendar.Calendar() |
||
fridays={} |
fridays={} |
||
Line 3,626: | Line 3,626: | ||
for item in sorted((month+"-"+day for month,day in fridays.items()), |
for item in sorted((month+"-"+day for month,day in fridays.items()), |
||
key=lambda x:int(x.split("-")[1])): |
key=lambda x:int(x.split("-")[1])): |
||
print item</ |
print item</syntaxhighlight> |
||
using itertools |
using itertools |
||
< |
<syntaxhighlight lang="python">import calendar |
||
from itertools import chain |
from itertools import chain |
||
f=chain.from_iterable |
f=chain.from_iterable |
||
Line 3,646: | Line 3,646: | ||
for item in sorted((month+"-"+day for month,day in fridays.items()), |
for item in sorted((month+"-"+day for month,day in fridays.items()), |
||
key=lambda x:int(x.split("-")[1])): |
key=lambda x:int(x.split("-")[1])): |
||
print item</ |
print item</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ over 3 < if [ 1 - ] |
||
dup 4 / over + |
dup 4 / over + |
||
over 100 / - |
over 100 / - |
||
Line 3,697: | Line 3,697: | ||
[ 5 lastwkdays ] is lastfridays ( year --> ) |
[ 5 lastwkdays ] is lastfridays ( year --> ) |
||
2012 lastfridays</ |
2012 lastfridays</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,715: | Line 3,715: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="rsplus">year = commandArgs(T) |
||
d = as.Date(paste0(year, "-01-01")) |
d = as.Date(paste0(year, "-01-01")) |
||
fridays = d + seq(by = 7, |
fridays = d + seq(by = 7, |
||
Line 3,721: | Line 3,721: | ||
364 + (months(d + 30 + 29) == "February")) |
364 + (months(d + 30 + 29) == "February")) |
||
message(paste(collapse = "\n", fridays[tapply( |
message(paste(collapse = "\n", fridays[tapply( |
||
seq_along(fridays), as.POSIXlt(fridays)$mon, max)]))</ |
seq_along(fridays), as.POSIXlt(fridays)$mon, max)]))</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require srfi/19 math) |
(require srfi/19 math) |
||
Line 3,766: | Line 3,766: | ||
(for ([d (last-fridays 2012)]) |
(for ([d (last-fridays 2012)]) |
||
(displayln (~a (date->string d "~a ~d ~b ~Y")))) |
(displayln (~a (date->string d "~a ~d ~b ~Y")))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,785: | Line 3,785: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub MAIN (Int $year = Date.today.year) { |
||
my @fri; |
my @fri; |
||
for Date.new("$year-01-01") .. Date.new("$year-12-31") { |
for Date.new("$year-01-01") .. Date.new("$year-12-31") { |
||
Line 3,791: | Line 3,791: | ||
} |
} |
||
.say for @fri[1..12]; |
.say for @fri[1..12]; |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
Line 3,810: | Line 3,810: | ||
A solution without a result array to store things in: |
A solution without a result array to store things in: |
||
<lang |
<syntaxhighlight lang="raku" line>sub MAIN (Int $year = Date.today.year) { |
||
say ~.value.reverse.first: *.day-of-week == 5 |
say ~.value.reverse.first: *.day-of-week == 5 |
||
for classify *.month, Date.new("$year-01-01") .. Date.new("$year-12-31"); |
for classify *.month, Date.new("$year-01-01") .. Date.new("$year-12-31"); |
||
}</ |
}</syntaxhighlight> |
||
Here, <code>classify</code> sorts the dates into one bin per month (but preserves the order in each bin). We then take the list inside each bin (<code>.value</code>) and find the last (<code>.reverse.first</code>) date which is a Friday. |
Here, <code>classify</code> sorts the dates into one bin per month (but preserves the order in each bin). We then take the list inside each bin (<code>.value</code>) and find the last (<code>.reverse.first</code>) date which is a Friday. |
||
Line 3,819: | Line 3,819: | ||
Another variation where the data flow can be read left to right using feed operators: |
Another variation where the data flow can be read left to right using feed operators: |
||
<lang |
<syntaxhighlight lang="raku" line>sub MAIN (Int $year = Date.today.year) { |
||
.say for Date.new("$year-01-01") .. Date.new("$year-12-31") ==> classify *.month ==> |
.say for Date.new("$year-01-01") .. Date.new("$year-12-31") ==> classify *.month ==> |
||
map *.value.reverse.first: *.day-of-week == 5 |
map *.value.reverse.first: *.day-of-week == 5 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
The longer version: |
The longer version: |
||
< |
<syntaxhighlight lang="rebol">leap-year?: function [year] [to-logic attempt [to-date reduce [29 2 year]]] |
||
days-in-feb: function [year] [either leap-year? year [29] [28]] |
days-in-feb: function [year] [either leap-year? year [29] [28]] |
||
Line 3,848: | Line 3,848: | ||
year: to-integer input |
year: to-integer input |
||
repeat month 12 [print last-friday-of-month month year] |
repeat month 12 [print last-friday-of-month month year] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>rebol last-fridays.reb <<< 2012 |
<pre>rebol last-fridays.reb <<< 2012 |
||
Line 3,865: | Line 3,865: | ||
</pre> |
</pre> |
||
A shorter version: |
A shorter version: |
||
< |
<syntaxhighlight lang="rebol">last-fridays-of-year: function [year] [ |
||
collect [ |
collect [ |
||
repeat month 12 [ |
repeat month 12 [ |
||
Line 3,877: | Line 3,877: | ||
foreach friday last-fridays-of-year to-integer input [print friday] |
foreach friday last-fridays-of-year to-integer input [print friday] |
||
</syntaxhighlight> |
|||
</lang> |
|||
NB. See "Find the last Sunday of each month" Rosetta for alternative (even more succinct) solution |
NB. See "Find the last Sunday of each month" Rosetta for alternative (even more succinct) solution |
||
Line 3,910: | Line 3,910: | ||
║ last day─of─week is then obtained straightforwardly, or via subtraction. ║ |
║ last day─of─week is then obtained straightforwardly, or via subtraction. ║ |
||
╚════════════════════════════════════════════════════════════════════════════════════════════════╝ |
╚════════════════════════════════════════════════════════════════════════════════════════════════╝ |
||
< |
<syntaxhighlight lang="rexx">/*REXX program displays the dates of the last Fridays of each month for any given year.*/ |
||
parse arg yyyy /*obtain optional argument from the CL.*/ |
parse arg yyyy /*obtain optional argument from the CL.*/ |
||
do j=1 for 12 /*traipse through all the year's months*/ |
do j=1 for 12 /*traipse through all the year's months*/ |
||
Line 3,959: | Line 3,959: | ||
.er: arg ,_; say; say '***error*** (in LASTDOW)'; say /*tell error, and */ |
.er: arg ,_; say; say '***error*** (in LASTDOW)'; say /*tell error, and */ |
||
say word('day-of-week month year excess', arg(2)) arg(1) a._ /*plug in a choice.*/ |
say word('day-of-week month year excess', arg(2)) arg(1) a._ /*plug in a choice.*/ |
||
say; exit 13 /*··· then exit. */</ |
say; exit 13 /*··· then exit. */</syntaxhighlight> |
||
{{out|output|text= when using the following input of: <tt> 2012 </tt> or <tt> 12 </tt>}} |
{{out|output|text= when using the following input of: <tt> 2012 </tt> or <tt> 12 </tt>}} |
||
<pre> |
<pre> |
||
Line 3,977: | Line 3,977: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "What year to calculate (yyyy) : " |
see "What year to calculate (yyyy) : " |
||
give year |
give year |
||
Line 3,999: | Line 3,999: | ||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,019: | Line 4,019: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'date' |
||
def last_friday(year, month) |
def last_friday(year, month) |
||
Line 4,028: | Line 4,028: | ||
year = Integer(ARGV.shift) |
year = Integer(ARGV.shift) |
||
(1..12).each {|month| puts last_friday(year, month)}</ |
(1..12).each {|month| puts last_friday(year, month)}</syntaxhighlight> |
||
Friday is <code>d.wday == 5</code>; the expression <code>(d.wday - 5) % 7</code> counts days after Friday. |
Friday is <code>d.wday == 5</code>; the expression <code>(d.wday - 5) % 7</code> counts days after Friday. |
||
Line 4,048: | Line 4,048: | ||
Or get the last day of the month and go to the previous day until it's a Friday. |
Or get the last day of the month and go to the previous day until it's a Friday. |
||
< |
<syntaxhighlight lang="ruby">require 'date' |
||
def last_friday(year, month) |
def last_friday(year, month) |
||
Line 4,055: | Line 4,055: | ||
d |
d |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">input "Year:";yr |
||
dayOne$ = "01-01-";yr |
dayOne$ = "01-01-";yr |
||
n1 = date$(dayOne$) |
n1 = date$(dayOne$) |
||
Line 4,072: | Line 4,072: | ||
wend |
wend |
||
print date$(n1) ' print last Friday's date |
print date$(n1) ' print last Friday's date |
||
next i</ |
next i</syntaxhighlight> |
||
<pre>Year:?2013 |
<pre>Year:?2013 |
||
01/25/2013 |
01/25/2013 |
||
Line 4,088: | Line 4,088: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">use std::env::args; |
||
use time::{Date, Duration}; |
use time::{Date, Duration}; |
||
Line 4,101: | Line 4,101: | ||
println!("{}", date - days_back); |
println!("{}", date - days_back); |
||
}); |
}); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,120: | Line 4,120: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">import java.util.Calendar |
||
import java.text.SimpleDateFormat |
import java.text.SimpleDateFormat |
||
Line 4,143: | Line 4,143: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2012-Jan-27 |
<pre>2012-Jan-27 |
||
Line 4,163: | Line 4,163: | ||
Applicable to any day of the week, cf. [[http://rosettacode.org/wiki/Find_last_sunday_of_each_month#Seed7]]. |
Applicable to any day of the week, cf. [[http://rosettacode.org/wiki/Find_last_sunday_of_each_month#Seed7]]. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "time.s7i"; |
include "time.s7i"; |
||
include "duration.s7i"; |
include "duration.s7i"; |
||
Line 4,192: | Line 4,192: | ||
end for; |
end for; |
||
end if; |
end if; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} when called with <tt>s7 rosetta/lastWeekdayInMonth 5 2013</tt>: |
{{out}} when called with <tt>s7 rosetta/lastWeekdayInMonth 5 2013</tt>: |
||
Line 4,211: | Line 4,211: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang="sensetalk"> |
||
ask "What year?" |
ask "What year?" |
||
put it into year |
put it into year |
||
Line 4,229: | Line 4,229: | ||
add a month to lastDayOfMonth -- advance to last day of next month |
add a month to lastDayOfMonth -- advance to last day of next month |
||
end repeat |
end repeat |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,249: | Line 4,249: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">require('DateTime') |
||
var (year=2016) = ARGV.map{.to_i}... |
var (year=2016) = ARGV.map{.to_i}... |
||
Line 4,258: | Line 4,258: | ||
} |
} |
||
say dt.ymd |
say dt.ymd |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,278: | Line 4,278: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<syntaxhighlight lang="smalltalk"> |
|||
<lang Smalltalk> |
|||
Pharo Smalltalk |
Pharo Smalltalk |
||
Line 4,288: | Line 4,288: | ||
thenSelect: [ :each | |
thenSelect: [ :each | |
||
(((Date daysInMonth: each monthIndex forYear: yr) - each dayOfMonth) <= 6) and: [ each year = yr ] ] ] |
(((Date daysInMonth: each monthIndex forYear: yr) - each dayOfMonth) <= 6) and: [ each year = yr ] ] ] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,296: | Line 4,296: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
<syntaxhighlight lang="sql"> |
|||
<lang SQL> |
|||
select to_char( next_day( last_day( add_months( to_date( |
select to_char( next_day( last_day( add_months( to_date( |
||
:yr||'01','yyyymm' ),level-1))-7,'Fri') ,'yyyy-mm-dd Dy') lastfriday |
:yr||'01','yyyymm' ),level-1))-7,'Fri') ,'yyyy-mm-dd Dy') lastfriday |
||
from dual |
from dual |
||
connect by level <= 12; |
connect by level <= 12; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
LASTFRIDAY |
LASTFRIDAY |
||
Line 4,322: | Line 4,322: | ||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">program last_fridays |
||
args year |
args year |
||
clear |
clear |
||
Line 4,348: | Line 4,348: | ||
| 30nov2012 | |
| 30nov2012 | |
||
| 28dec2012 | |
| 28dec2012 | |
||
+-----------+</ |
+-----------+</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang="swift"> |
|||
<lang Swift> |
|||
import Foundation |
import Foundation |
||
Line 4,400: | Line 4,400: | ||
print(lastFridays(of: 2013).map(dateFormatter.string).joined(separator: "\n")) |
print(lastFridays(of: 2013).map(dateFormatter.string).joined(separator: "\n")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
1/27/12 |
1/27/12 |
||
Line 4,417: | Line 4,417: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
set year [lindex $argv 0] |
set year [lindex $argv 0] |
||
foreach dm {02/1 03/1 04/1 05/1 06/1 07/1 08/1 09/1 10/1 11/1 12/1 12/32} { |
foreach dm {02/1 03/1 04/1 05/1 06/1 07/1 08/1 09/1 10/1 11/1 12/1 12/32} { |
||
Line 4,424: | Line 4,424: | ||
# Print the interesting part |
# Print the interesting part |
||
puts [clock format $t -format "%Y-%m-%d" -gmt 1] |
puts [clock format $t -format "%Y-%m-%d" -gmt 1] |
||
}</ |
}</syntaxhighlight> |
||
Sample execution: |
Sample execution: |
||
<pre> |
<pre> |
||
Line 4,443: | Line 4,443: | ||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
year=2012 |
year=2012 |
||
Line 4,455: | Line 4,455: | ||
ENDLOOP |
ENDLOOP |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style='height:30ex;overflow:scroll'> |
<pre style='height:30ex;overflow:scroll'> |
||
Line 4,474: | Line 4,474: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Using <code>ncal</code>. Will switch to Julian calender as ncal sees fit, and will not calculate past year 9999 (chances are you'll be too dead by then to worry about weekends anyway). |
Using <code>ncal</code>. Will switch to Julian calender as ncal sees fit, and will not calculate past year 9999 (chances are you'll be too dead by then to worry about weekends anyway). |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
if [ -z $1 ]; then exit 1; fi |
if [ -z $1 ]; then exit 1; fi |
||
Line 4,482: | Line 4,482: | ||
for m in 01 02 03 04 05 06 07 08 09 10 11 12; do |
for m in 01 02 03 04 05 06 07 08 09 10 11 12; do |
||
echo $1-$m-`ncal $m $1 | grep Fr | sed 's/.* \([0-9]\)/\1/'` |
echo $1-$m-`ncal $m $1 | grep Fr | sed 's/.* \([0-9]\)/\1/'` |
||
done</ |
done</syntaxhighlight> |
||
For systems without ncal: |
For systems without ncal: |
||
< |
<syntaxhighlight lang="sh">#!/bin/sh |
||
# usage: last_fridays [ year] |
# usage: last_fridays [ year] |
||
Line 4,502: | Line 4,502: | ||
# Strip leading zeros to avoid octal interpretation |
# Strip leading zeros to avoid octal interpretation |
||
month=$(( 1 + ${month#0} )) |
month=$(( 1 + ${month#0} )) |
||
done</ |
done</syntaxhighlight> |
||
Using <code>date --date</code> from GNU date??? This code is not portable. |
Using <code>date --date</code> from GNU date??? This code is not portable. |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
# Free code, no limit work |
# Free code, no limit work |
||
Line 4,576: | Line 4,576: | ||
# main |
# main |
||
last_fridays ${1:-2012}</ |
last_fridays ${1:-2012}</syntaxhighlight> |
||
Sample execution: |
Sample execution: |
||
Line 4,596: | Line 4,596: | ||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |
||
< |
<syntaxhighlight lang="vfp"> |
||
*!* OOP implementaion |
*!* OOP implementaion |
||
LOCAL lnYear As Integer, oCalc As fricalc |
LOCAL lnYear As Integer, oCalc As fricalc |
||
Line 4,631: | Line 4,631: | ||
ENDDEFINE |
ENDDEFINE |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">import time |
||
import os |
import os |
||
Line 4,663: | Line 4,663: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Please select a year: 2012 |
<pre>Please select a year: 2012 |
||
Line 4,684: | Line 4,684: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-date}} |
{{libheader|Wren-date}} |
||
< |
<syntaxhighlight lang="ecmascript">import "os" for Process |
||
import "/date" for Date |
import "/date" for Date |
||
Line 4,706: | Line 4,706: | ||
System.print(dt.addDays(-wd - 2)) |
System.print(dt.addDays(-wd - 2)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,742: | Line 4,742: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
func WeekDay(Year, Month, Day); \Return day of week (0=Sun, 1=Mon ... 6=Sat) |
func WeekDay(Year, Month, Day); \Return day of week (0=Sun, 1=Mon ... 6=Sat) |
||
Line 4,764: | Line 4,764: | ||
IntOut(0, LastDay); CrLf(0); |
IntOut(0, LastDay); CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,784: | Line 4,784: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Gregorian calendar |
Gregorian calendar |
||
< |
<syntaxhighlight lang="zkl">var [const] D=Time.Date; |
||
fcn lastDay(y,d){ |
fcn lastDay(y,d){ |
||
[1..12].pump(List,'wrap(m){ // 12 months, closure for y & d |
[1..12].pump(List,'wrap(m){ // 12 months, closure for y & d |
||
Line 4,793: | Line 4,793: | ||
}) |
}) |
||
} |
} |
||
lastDay(2012,D.Friday).concat("\n").println();</ |
lastDay(2012,D.Friday).concat("\n").println();</syntaxhighlight> |
||
For each month in year y, count back from the last day in the month |
For each month in year y, count back from the last day in the month |
||
until a Friday is found and print that date. |
until a Friday is found and print that date. |