LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

wrong week number returned by strftime()

Hello colleagues,

 

On the link below, we have:

 

https://www.ni.com/docs/en-US/bundle/labwindows-cvi/page/cvi/usermanual/datetimespecifierslist.htm?s...

 

  • %U—Week of the year, as an integer, where Sunday is considered the first day of the week. Valid values are 00-53.
  • %W—Week of the year, as an integer, where Monday is considered the first day of the week. Valid values are 00-53.

Unfortunately the return is the same for both formats.

 

Source code:

 

#include <utility.h>
#include <ansi_c.h>
#include <stdio.h>
#include <time.h>

 

int main (int argc, char *argv[])
{
     time_t rawtime;
     struct tm *info;
     char buffer[80];

     time( &rawtime );
     info = localtime( &rawtime );

     strftime(buffer,80," %U %W ", info);
     printf("Week number: %s\n", buffer );

     printf("Press the Enter key to exit.");
     getchar ();
}

 

Results:

     Week number: 36 36
     Press the Enter key to exit.

 

Expectation:

     37 36

0 Kudos
Message 1 of 2
(196 Views)

In my opinion you should not use strftime () to calculate the week number.

 

On one hand it shows some oddity in the returned week number: here is how it defines week numbers for the first days of 2024

// This code:
strftime (buffer, 80, "%a %b %d: week %U (%W)", info);

// produces this output:
Mon Jan 01: week 00 (00)
Tue Jan 02: week 00 (00)
Wed Jan 03: week 00 (00)
Thu Jan 04: week 00 (00)
Fri Jan 05: week 00 (00)
Sat Jan 06: week 00 (00)
Sun Jan 07: week 01 (00)    // <-- !!
Mon Jan 08: week 01 (01)
Tue Jan 09: week 01 (01)

 

On the other hand it doesn't follow ISO8601 standard for date and time, according to which we are now in week 38 while strftime () returns 37.

Here you can find a useful discussion about this standard and some implementations of it in CVI: I suggest you to use one of them to calculate the week number instead of strftime ().



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(148 Views)