Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Calendar week will be calculated wrongly by std::wcsftime function


This question is answered.


Permlink Replies: 6 - Last Post: Feb 4, 2016 5:25 AM Last Post By: Sean Hoffman
velrajan muthuvel

Posts: 28
Registered: 9/22/14
Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 2, 2016 9:19 PM
If I am using the below code, I am getting the wrong week number. For ex: Jan 2,2016 ---> Week number is coming as "0" instead of 52

 time_t rawtime;
 struct tm * timeinfo;
 char buffer [80];
 
 time (&rawtime);
 timeinfo = localtime (&rawtime);
 
 strftime (buffer,80,"%W",timeinfo);
 swprintf(txt_buffer, L"%d", atoi(buffer));

Could you anyone suggest to help to get the week number using different function?

Sean Hoffman

Posts: 126
Registered: 3/28/99
Re: Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 4:27 AM   in response to: velrajan muthuvel in response to: velrajan muthuvel
velrajan muthuvel wrote:
If I am using the below code, I am getting the wrong week number. For ex: Jan 2,2016 ---> Week number is coming as "0" instead of 52

 time_t rawtime;
 struct tm * timeinfo;
 char buffer [80];
 
 time (&rawtime);
 timeinfo = localtime (&rawtime);
 
 strftime (buffer,80,"%W",timeinfo);
 swprintf(txt_buffer, L"%d", atoi(buffer));

Could you anyone suggest to help to get the week number using different function?


That is the correct and anticipated output. Why would you think the 2nd day of the year would fall in the 52nd week?
Martin van der ...

Posts: 57
Registered: 7/14/02
Re: Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 6:11 AM   in response to: Sean Hoffman in response to: Sean Hoffman
Sean Hoffman wrote:
That is the correct and anticipated output. Why would you think the 2nd day of the year would fall in the 52nd week?

That all depends on the week numbering system that is used where you live.
The standard C library, unfortunately, only supports US date and time standards. For everything else, you'll need to find a different solution or make your own.
Ted Lyngmo

Posts: 117
Registered: 10/3/06
Re: Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 10:21 AM   in response to: Martin van der ... in response to: Martin van der ...
Martin van der Plas wrote:
Sean Hoffman wrote:
That is the correct and anticipated output. Why would you think the 2nd day of the year would fall in the 52nd week?

That all depends on the week numbering system that is used where you live.
The standard C library, unfortunately, only supports US date and time standards. For everything else, you'll need to find a different solution or make your own.

Yes. ...and it should really be week 53 according to ISO 8601.

Some strftime() implementations support the 'V' conversion specifier character to get the ISO week.

ISO 8601 week dates (copied from the linux man page):
In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week. Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday; or, the week that has 4 January in it). When three of fewer days of the first calendar week of the new year fall within that year, then the ISO 8601 week-based system counts those days as part of week 53 of the preceding year.

Br,
Ted
velrajan muthuvel

Posts: 28
Registered: 9/22/14
Re: Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 8:59 PM   in response to: Ted Lyngmo in response to: Ted Lyngmo
Ted Lyngmo wrote:
Martin van der Plas wrote:
Sean Hoffman wrote:
That is the correct and anticipated output. Why would you think the 2nd day of the year would fall in the 52nd week?

That all depends on the week numbering system that is used where you live.
The standard C library, unfortunately, only supports US date and time standards. For everything else, you'll need to find a different solution or make your own.

Yes. ...and it should really be week 53 according to ISO 8601.

Some strftime() implementations support the 'V' conversion specifier character to get the ISO week.

ISO 8601 week dates (copied from the linux man page):
In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week. Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday; or, the week that has 4 January in it). When three of fewer days of the first calendar week of the new year fall within that year, then the ISO 8601 week-based system counts those days as part of week 53 of the preceding year.

Br,
Ted

Currently I am using C++ 98 in my PC. You ask to use "%V" format. But it is not supported now. Please let me know is there any other function available in C++98.

Ted Lyngmo

Posts: 117
Registered: 10/3/06
Re: Calendar week will be calculated wrongly by std::wcsftime function
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 3, 2016 11:20 PM   in response to: velrajan muthuvel in response to: velrajan muthuvel
velrajan muthuvel wrote:
Ted Lyngmo wrote:
Martin van der Plas wrote:
Sean Hoffman wrote:
That is the correct and anticipated output. Why would you think the 2nd day of the year would fall in the 52nd week?

That all depends on the week numbering system that is used where you live.
The standard C library, unfortunately, only supports US date and time standards. For everything else, you'll need to find a different solution or make your own.

Yes. ...and it should really be week 53 according to ISO 8601.

Some strftime() implementations support the 'V' conversion specifier character to get the ISO week.

ISO 8601 week dates (copied from the linux man page):
In this system, weeks start on a Monday, and are numbered from 01, for the first week, up to 52 or 53, for the last week. Week 1 is the first week where four or more days fall within the new year (or, synonymously, week 01 is: the first week of the year that contains a Thursday; or, the week that has 4 January in it). When three of fewer days of the first calendar week of the new year fall within that year, then the ISO 8601 week-based system counts those days as part of week 53 of the preceding year.

Currently I am using C++ 98 in my PC. You ask to use "%V" format. But it is not supported now. Please let me know is there any other function available in C++98.


Ok, %V was added in C99 / C++11. Perhaps you can download a compiler that supports C99 / C++11 and create a DLL wrapping the newer version of strftime() and then use that DLL in your C++98 environment? Cygwin with gcc could perhaps work: https://cygwin.com/cygwin-ug-net/dll.html

Edit:
Cygwin/gcc isn't probably the best option since you probably don't want to distribute the cygwin1.dll etc.l tried creating a DLL with Cygwin/MinGW-gcc-core since that works without having to distribute extra DLL:s - but their strftime seems to be using the Windows built-in which seems be lacking %V too.

I ended up doing this:
#include <time.h>
// Derived from: http://www.opensource.apple.com/source/tcl/tcl-10/tcl/compat/strftime.c
int iso8601_week( const struct tm *t ) {
    int week = (((t)->tm_yday + 7 - ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7);
    int days = (((t)->tm_yday + 7 - ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
    if( days >= 4 ) {
	week++;
    } else if( week == 0 ) {
	week = 53;
    }
    return week;
}


Br,
Ted

Edited by: Ted Lyngmo on Feb 4, 2016 5:09 AM
Sean Hoffman

Posts: 126
Registered: 3/28/99
Re: Calendar week will be calculated wrongly by std::wcsftime function  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 4, 2016 5:25 AM   in response to: velrajan muthuvel in response to: velrajan muthuvel
Gosh I really had no idea that there were different ways of calculating the week count; I stand corrected.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02