Post Reply 
A tiny New Year's programming challenge
01-01-2015, 04:51 PM (This post was last modified: 01-03-2015 06:49 AM by Dieter.)
Post: #1
A tiny New Year's programming challenge
The year has just begun - maybe it's time for a programming challenge that deals with dates and calendars, and even especially with the days around New Year. ;-)

Instead of a particular date (e.g. "14.03.2014"), referring to a certain week of the year ("week 11/2014") often makes more sense, and so week numbers are commonly used in trade and business (at least over here in Europe). There is a international standard (ISO 8601) that defines the start of the first week of the year: it's the one with the major part (i.e. at least four days) belonging to January. So week #1 may start as late as 4 Jan or as early as 29 Dec of the previous year. Generally, weeks are defined as starting on Monday and ending on the following Sunday. More details can be found on Wikipedia.

And here is your task: Write a program that accepts a valid Gregorian Date and returns the corresponding ISO week number, as well as the day number within that week.

Some test cases:

Code:
04 Jul 1979  => week 27/1979, day 3
02 Jan 2010  => week 53/2009, day 6
04 Jan 2010  => week 01/2010, day 1
01 Jan 2012  => week 52/2011, day 7   // edit: year corrected, of course it's not 2012 #-)
31 Dec 2012  => week 01/2013, day 1

The basic idea is very simple: determine the start of week #1, calculate the number of days between that Monday and the given date, and finally divide the result by 7. However, this may result in a negative value for the first days of a year, or it may return week #53 – which may or may not exist in that year. This is the "...challenging" part of the challenge. ;-)

Maybe your program even accepts any input that is valid for the current date format setting (i.e. dd.mmyyyy, mm.ddyyyy or even yyyy.mmdd), and the output is formatted according to the ISO standard (e.g. 1979-W27-3).

Of course calculators with date functions are very helpful here, that's why I would suggest using a 34s. Which does not mean that others cannot be used as well. ;-)

Enjoy!

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
A tiny New Year's programming challenge - Dieter - 01-01-2015 04:51 PM



User(s) browsing this thread: 1 Guest(s)