Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Objective C

CWeek - A Class for Getting the weekday and weeknumber

Rate me:
Please Sign up or sign in to vote.
3.93/5 (10 votes)
22 Mar 20041 min read 71.1K   819   15   12
Getting the weekday and weeknumber

Introduction

This is my first attempt ever to write an article of any kind. I don't have much experience in programming, but I would love some feedback about this article in order to learn. English is not my primary language, so be nice to me. :-)

Background

I'm currently working on a calendar application with a friend of mine, and knowing the weekday and the weeknumber is somehow fundamental. ;)

I can't remember where I found the algorithms and information necessary to know how to retrieve the weekday and weeknumber, but if you know, then contact me and I will give the person credits for it.

How to use CWeek

CWeek is easy to use. First, declare an instance of the object.

C++
CWeek date;

After this, you should set the date. For example, October 15, 2004.

C++
date.setDate(2004,10,15);

To retrieve the weekday or the weeknumber, simply use:

C++
nWeeknumber = date.getWeeknumber();
nWeekday = date.getWeekday();

History

  • 23rd March, 2004: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhich week number Pin
Willzyba22-Nov-04 4:12
Willzyba22-Nov-04 4:12 
GeneralSome Comments Pin
Johann Gerell25-Mar-04 11:37
Johann Gerell25-Mar-04 11:37 
Hi!

The nitpicker is here. Smile | :)

I'm Swedish, so I don't exactly have any syntactical problems reading the source code documentation and comments. But it sure looks strange when you are used to read code and comments in English.

My first tip for you on your future programming trip is to never, ever again write comments in any other language than English (or, perhaps, in a meta-programming language...). That is mainly for your own sake. You need to get used to formulate yourself in English to be able to define precise questions on programming problems for others to solve, if that issue ever comes up - which it will. Writing code comments in English will help you with this. I know you have it in a TODO. But, the next time, don't use a TODO for it, rather do it right immediately.

To my second tip: Without mentioning anything about the correctness of your leap year algorithm, writing
if((nYear%4)==0)
	bIsLeapYear = true;
else
	bIsLeapYear = false;
instead of
bIsLeapYear = (nYear % 4) == 0;
will not make the code easier to read when the statement is as short as that. For longer statements and complex boolean evaluations, I agree that your version can make the code more readable, but not here.

Finishing off with a third advice - translated from Swedish:
// If it is leap year and the month > 2, then increase the number of days by 1.
if(bIsLeapYear && nMonth > 2)
	nNumDaysOfYear++;
That's not a documented statement. You've just wasted some electrons on the row above the code, which is perfectly self documenting - a school example of such a thing, really. But, please, only add comments that add something beyond what the code clearly says. If you had written the code as bad as this:
if(l&&m>2)n++;

Then your comment would probably be needed, but as you wrote it self documenting, there was no need to!

Wink | ;)


--
Dad, how strange it is that the pig can speak.
*thoughtful pause* It must have lost its "oink".
(my 3-year old daughter Moa, while watching Babe)

Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so. (Douglas Adams)

GeneralRe: Some Comments Pin
Patrik Svensson26-Mar-04 4:54
Patrik Svensson26-Mar-04 4:54 
GeneralWhy not base this class on CDateTime Pin
Gerard Nicol23-Mar-04 13:47
Gerard Nicol23-Mar-04 13:47 
GeneralRe: Why not base this class on CDateTime Pin
Gerard Nicol23-Mar-04 13:55
Gerard Nicol23-Mar-04 13:55 
GeneralRe: Why not base this class on CDateTime Pin
Eric Lapouge23-Mar-04 20:37
Eric Lapouge23-Mar-04 20:37 
GeneralRe: Why not base this class on CDateTime Pin
Johann Gerell25-Mar-04 11:39
Johann Gerell25-Mar-04 11:39 
GeneralMisc comments Pin
Eric Lapouge23-Mar-04 5:50
Eric Lapouge23-Mar-04 5:50 
GeneralRe: Misc comments Pin
Patrik Svensson23-Mar-04 6:00
Patrik Svensson23-Mar-04 6:00 
GeneralRe: Misc comments Pin
Eric Lapouge23-Mar-04 6:26
Eric Lapouge23-Mar-04 6:26 
GeneralKISS - Keep it simply stated Pin
NGS 54967223-Mar-04 5:36
NGS 54967223-Mar-04 5:36 
GeneralRe: KISS - Keep it simply stated Pin
Patrik Svensson23-Mar-04 6:08
Patrik Svensson23-Mar-04 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.