This function should calculate week of year according to ISO standards (week 1 is the week that contains the first Thursday - week starts on Monday).
int CVICALLBACK CalcWeek (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int week0; // Adjusting days for first week
int dd, mm, yy;
int yday; // Day of year
time_t now;
struct tm *ts;
/*
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday; // Days: 1 ÷ 31
int tm_mon; // Months: 0 ÷ 11
int tm_year;
int tm_wday;
int tm_yday; // Day since Jan 01: 0 ÷ 365
int tm_isdst;
};
*/
if (event != EVENT_COMMIT) return 0;
GetCtrlVal (panel, PANEL_dd, &dd);
GetCtrlVal (panel, PANEL_mm, &mm
);
GetCtrlVal (panel, PANEL_yy, &yy);
// Inizialize the struct
now = time (NULL);
ts = localtime (&now);
// Calculate day of the year
ts->tm_mon = mm - 1;
ts->tm_mday = dd;
ts->tm_year = yy - 1900;
mktime (ts); // Adjust struct fields
yday = ts->tm_yday;
DebugPrintf ("\n%02d/%d is day %d of year %d\n", mm, dd, yday, yy);
// Calculate day of the week of jan 01
ts->tm_mon = 0;
ts->tm_mday = 1;
mktime (ts); // Adjust struct fields
// Adjust yday ofr first week in the year
if (ts->tm_wday > 4) {
week0 = 7 - ts->tm_wday;
DebugPrintf ("%d days of january pertain to the previous year.\n", week0);
// We must cut away all days until the first monday
yday -= week0;
}
else {
// We must add days from monday to Jan 01
DebugPrintf ("Jan 01 in in week 1.\n");
yday += ts->tm_wday;
}
// Calculate week of year
DebugPrintf ("We are now in week %.0f of the year,\n", ceil ((double)yday / 7.0));
return 0;
}
CVI should manag
e correctly the %W (week #1 has the first Monday) and %U (week #1 has the first Sunday) formatting codes.
Hope this helps
Roberto