14
1
Write a function which takes a date and returns the day of the week of the next February 29th after that date.
The input is a string in the ISO Extended format: YYYY-MM-DD (e.g. May 27th, 2010 would be "2010-05-27").
The output is a string which is the name of the day of the week (e.g. "Monday"). Capitalization doesn't matter, but do give the full name in English.
If the given date is February 29th, then return the day of the week of the next Feburary 29th.
Use the calculations for the Proleptic Gregorian Calendar (so, it uses the Gregorian leap year calculations for it's entire length). Don't worry about the Julian Calendar or when the switch from Julian to Gregorian occurred. Just assume Gregorian for everything.
The function should work for at least the range of "0001-01-01" - "2100-01-01".
Feel free to use whatever standard libraries your language of choice provides, but do not use 3rd party libraries unless you want to include that code as part of your solution.
Shortest code (fewest characters) wins.
Examples:
func("0001-01-01")
->"Sunday"
func("1899-12-03")
->"Monday"
func("1970-01-01")
->"Tuesday"
func("1999-07-06")
->"Tuesday"
func("2003-05-22")
->"Sunday"
func("2011-02-17")
->"Wednesday"
func("2100-01-01")
->"Friday"
(and no, you don't have to name the function func
)
Hints:
- Remember that years ending in 00 which aren't divisable by 400 aren't leap years.
- January 1st, 0001 is a Monday.
What path is necessary for this to work? I have GnuWin date in my path, which is breaking it. – Peter Taylor – 2011-02-22T12:31:20.727
@Peter: It might clash with
date
. Just remove GNUWin32 from the PATH and it should work. Or changedate
toGet-Date
(that kind of fallback behavior only works when no command is found – to check, just usegcm date
). Anyway, I don't consider that a particular problem with this script as GNUWin32 is not part of any standard Windows installation. – Joey – 2011-02-22T16:46:18.780The reason I asked was because I already tried using
get-date
and got the error messageMethod invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
Does it need PS2 or .Net 4 or something? – Peter Taylor – 2011-02-22T16:53:12.310@Peter: I tried PowerShell v2. .NET 4 doesn't play into it since PowerShell is linked to the 2.0 runtime. In any case, there shouldn't be a PSObject coming back from
Get-Date
but aSystem.DateTime
. – Joey – 2011-02-22T17:19:30.187It's very weird.
$d=Get-Date "0400-01-01"; $d++
gives an error message about++
not being defined on DateTime. The same replacing++
with+=1
or+="1"
or+='1'
gives the error message about PSObject. And just+"1"
works. – Peter Taylor – 2011-02-22T17:29:21.423It seems to be broken with PS1 and work with PS2, so maybe you should update the header. – Peter Taylor – 2011-02-24T14:11:40.513
Peter: Please just upgrade. PowerShell v2 was released 1½ years ago and is the current version. That scripts created recently won't necessarily run under old versions should be common sense. – Joey – 2011-02-24T16:21:12.097