Re: Re[2]: [Dev-C++] OT: week number
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2002-11-10 14:53:52
|
A nice program should check which day is first of week in current locale.
Here is code updated with such check:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define ALLOW_S2_LESS_THAN_S1
bool is_same_week(SYSTEMTIME& s1,SYSTEMTIME& s2) {
TCHAR buf[2];
FILETIME f1,f2;
ULARGE_INTEGER u1,u2;
#ifdef ALLOW_S2_LESS_THAN_S1
ULONGLONG tmp;
#endif
int size,firstday,wday1,wday2;
/* Check which weekday that starts the week. 0=monday */
size = GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_IFIRSTDAYOFWEEK,buf,2);
firstday = size==0 ? 6 : atoi(buf);
printf("First day of week is %d (0=monday)\n",firstday);
/* Adjust the weekdays given in the SYSTEMTIME struct (0=sunday)
* so that the first day of the week according to locale
* becomes 0.
*/
wday1 = (s1.wDayOfWeek + (6 - firstday)) % 7;
wday2 = (s2.wDayOfWeek + (6 - firstday)) % 7;
printf("w1 = %d (%d)\n",wday1,s1.wDayOfWeek);
printf("w2 = %d (%d)\n",wday2,s2.wDayOfWeek);
#ifndef ALLOW_S2_LESS_THAN_S1
if (wday2 < wday1)
/* Given the assumption that s2 must be larger than s1,
* they can't be in same week unless s2 has weekday
* >= s1.
*/
return false;
#endif /* ALLOW_S2_LESS_THAN_S1 */
/* Convert from SYSTEMTIME to a single numeric value */
SystemTimeToFileTime(&s1,&f1);
SystemTimeToFileTime(&s2,&f2);
u1.LowPart = f1.dwLowDateTime;
u1.HighPart = f1.dwHighDateTime;
u2.LowPart = f2.dwLowDateTime;
u2.HighPart = f2.dwHighDateTime;
/* Now have time in 100-nanosecond steps */
#ifdef ALLOW_S2_LESS_THAN_S1
/* if s1 <= s2 isn't a requirement then have to check if the two
* values must be swapped.
*/
if (u1.QuadPart < u2.QuadPart) {
if (wday2 < wday1) return false;
} else {
if (wday1 < wday2) return false;
tmp = u1.QuadPart;
u1.QuadPart = u2.QuadPart;
u2.QuadPart = tmp;
}
#else
if (u2.QuadPart < u1.QuadPart)
/* End of time interval before start... */
return false;
#endif /* !ALLOW_S2_LESS_THAN_S1 */
if (u2.QuadPart > (u1.QuadPart + 7*24*3600*(ULONGLONG)10000000))
/* more than 7 days between the two time stamps, so they can't
* be in same week.
*/
return false;
return true;
}
/Per W
|