Calendar week from CTime

Posted on 1998-12-10
Last Modified: 2013-11-20
I have got the following problem:
I´ve got a function that should return the calendar week (1-53) that includes the day specified by a CTime object:

int WeekNo( CTime tTime )
  int nWeek = ...
  return nWeek;

ISO Norm: "The first week of a year is the one which includes january 4".

But this is not really the problem. The problem is, that a calendar week begins at monday. So if the monday of the week which includes january 4 of the NEXT year is not in the NEXT year but in the CURRENT, the 53. week of the CURRENT year  is the 1. of the NEXT year. And thats not all. The project I am working at the moment is old old 16Bit Win 3.1x application so I have to work with VC++1.52.

Can anybody help me please?

Thank you in advance!
Question by:logis
  • 6

Accepted Solution

Answers2000 earned 200 total points
ID: 1326003
See answer in my comment below

Expert Comment

ID: 1326004
1. First off you probably want to make your WeekNo function use a const reference,  this doesn't really make any difference, but it avoids generating an extra CTime object

int WeekNo( const CTime& tTime )

2. Righjt now on to the maths,

CTime has a member GetTime which returns a time_t.  This is the number of seconds since 1970.  Now it also has a constructor which can take year, month, day, etc.

Therefore you can use this to do your maths (by subtracting these time_t values and converting back)

// get start of year
CTime tsoy( tTime.GetYear(), tTime.GetMonth(), tTime.GetDay(), 0, 0, 0 ) ;
time_t xStartOfYear = tsoy.GetTime() ;

This gives you the start of year as a seconds since 1970


Expert Comment

ID: 1326005
Oops.  Ignore item #2 above, I think I'll start from #2 again.

2. Now one other thing, is that your return a week number, that's fine, except you want to know the year as well (e.g. 1 January 19xx could count as being week 53 of year xx-1), so let's change your returning methods, to use references

void WeekNo( const CTime& tTime, int& year, int& weekno )

Basically the year and weekno will get changed by the function, so you'd call it like this

CTime someTime(...etc..)
int nYear ;
int nWeekNo ;
WeekNo( someTime, nYear, nWeekNo ) ;
printf( "Week %d of Year %d\n", nWeekNo, nYear ) ; // obviously change this to however you want to display - e.g don't use printf if not a console program.

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 1326006
3. Now the maths, we want to calculate the time_t value for the 1 January of the year, and what day of the week

CTime jan1( tTime.GetYear(), 1, 1, 0, 0, 0 ) ;
time_t tjan1 = jan1.GetTime() ;
int wdayjan1 = jan1.GetDayOfWeek() ;

The GetDayOfWeek function returns 1=Sun, 2=Mon, 3=Tue, 4=Wed, etc..

Now Jan4 will count as the same week as Jan1 if, Jan1 is not Friday, Saturday or Sunday

BOOL bExtraWeek = TRUE ;
if ( ( wdayjan1 == 6 ) || ( wday == 7 ) || ( wday == 1 ) )
bExtraWeek = FALSE ;


Expert Comment

ID: 1326007
Now the next thing is to calculate the time_t for the first Monday in the year

// day of month of first Monday in the year
int nDayFirstMonday ;
switch ( wdayjan1 )
case 1 : nDayFirstMonday = 2 ; break ; // 2nd Jan if 1 Jan is Sunday
case 2 : nDayFirstMonday = 1 ; break ; // 1st Jan if 1 Jan is Monday
case 3 : nDayFirstMonday = 7 ; break ;
case 4 : nDayFirstMonday = 6 ; break ;
case 5 : nDayFirstMonday = 5 ; break ;
case 6 : nDayFirstMonday = 4 : break ;
case 7 : nDayFirstMonday = 3 ; break ;
CTime firstmonday( tTime.GetYear(), 1, nDayFirstMonday, 0, 0, 0 ) ;
time_t tfirstmonday = firstmonday.GetTime() ;

Now calculate the seconds elapses between the first monday and the date your concerned with

int secs = tTime.GetTime() - tfirstmonday ;

// convert this to weeks
int weeks = secs / ( 24 * 60 * 60 * 7 ) ;

The week numbers start from 1, therefore Now allowing for the fact that the year has an extra, week, (sorry I didn't fully understand how you want to handle the dates of the first week, maybe you can elaborate, but I think you mean)

if ( !bExtraWeek)
weeks++ ;

Finally make the function return the stuff you need


Expert Comment

ID: 1326008
if ( weeks == 0 )
 // week 0 is week 53 of the previous year
year = tTime.GetYear() - 1 ;
weekno = 53 ;
} else
year = tTime.GetYear() ;
weekno = weeks ;


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
method notes when mouse over in eclipse 5 86
maven archtype selection in eclipse 1 57
haveThree challenge 22 116
canBalance challenge 34 80
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now