A tiny New Year's programming challenge
|
01-05-2015, 07:45 PM
(This post was last modified: 01-05-2015 07:56 PM by Dieter.)
Post: #12
|
|||
|
|||
RE: A tiny New Year's programming challenge
(01-05-2015 10:55 AM)Werner Wrote: Isn't it easier then to test Jan 1st of Y+1? If it's a Friday, Y has 53 weeks, else it's 52. No need for leap year testing. Sorry, but this does not cover all years with 53 weeks. Example: Y=2004. Here 1 Jan 2005 is a Saturday, not a Friday, but nevertheless 2004 had 53 weeks. The same is true for 2032 (1 Jan 2033 is a Saturday, but 2032 will have 53 weeks as well). Checking whether 1 January of the following year is a Friday means that the current year ends on a Thursday. This is only one of the two possible conditions for a 53-week-year. A common 53-week-year starts and ends on a Thursday. The other possible condition is a leap year starting on a Thursday and thus ending on a Friday. Which means that New Year of the following year is a Saturday, as shown in the two examples above. If a year has 53 weeks, it starts and/or ends on a Thursday. In common years both 1 Jan and 31 Dec are Thursdays. On the other hand, leap years with 53 weeks either start on a Wednesday (and therefore end on a Thursday – this case is covered by your suggestion), but they may as well start on a Thursday and end on a Friday. And this is what the routine checks: Is 1 Jan a Thursday OR (is it a leap year AND 1 Jan is a Wednesday). Cf. Wikipedia. Of course it is possible that the two tests can be implemented more elegantly. Any suggestions? Dieter |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)