From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ISO week number

Hello,

does anybody has a source for a function which calculates the ISO week
number?
The ansi.c "strftime" funcion with format string "%V" is supposed to do
that, - but this format specifier is not supported by CVI (using 7.0).

Thanks,
Greg Schmidle
0 Kudos
Message 1 of 3
(3,126 Views)
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


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?
Message 2 of 3
(3,126 Views)
Thank you very much Roberto! Works perfect!
Greg


"Roberto Bozzolo" schrieb im Newsbeitrag
news:506500000005000000285B0100-1075935269000@exchange.ni.com...
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
((dou
ble)yday / 7.0));

return 0;
}

CVI should manage correctly the %W (week #1 has the first Monday) and
%U (week #1 has the first Sunday) formatting codes.

Hope this helps
Roberto
0 Kudos
Message 3 of 3
(3,126 Views)