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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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