|
From: Zodiaq <zo...@ac...> - 2002-11-09 22:56:44
|
Hello Dev-cpp-users, yeah, I know this is an off topic question - it is not directly related to development od Dev-C++ but I also know that this mailing list reads quite few people so - do you know a good way to check if two dates are inside one week... dates are in SYSTEMTIME or FILETIME format... my present idea is not as good as I wish it to be - I start from first date and try to get to the second one - if I pass sutarday and still not in second I know that weeks are different... but this forces some comparisons... know I think that I might check in which week of the year both dates are - but how to do that? So - if you have any brilliant ideas share them with me! -- regards, Zodiaq |
|
From: Charles <cha...@at...> - 2002-11-09 23:22:45
|
Zodiaq - > ...do you know a good way to check if two dates are inside one week... You might check in Jeff Garland's Date_Time library which is now part of the Boost Library as of version 1.29.0. See more at www.boost.org. Check especially date_period. "The class boost::gregorian::date_period provides direct representation for ranges between two dates. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program. For example, testing if a date is within an irregular schedule such as a weekend or holiday can be accomplished using collections of date periods." Chuck |
|
From: Zodiaq <zo...@ac...> - 2002-11-09 23:36:59
|
Hello Charles and all Dev-C++ users, Sunday, November 10, 2002, 12:16:22 AM, Charles wrote as follows: C> You might check in Jeff Garland's Date_Time library which is now part of the C> Boost Library as of version 1.29.0. See more at www.boost.org. Thanks for quick help, but I'm trying my best to make my little progs as small as posible - so this means that I wouldn't like to use other libs... and I want to write own code optimized for specyfic problem... waiting for other help :-) C> Chuck -- regards, Zodiaq |
|
From: Per W. <pw...@ia...> - 2002-11-10 00:20:04
|
This is a bit naive, since it doesn't take into account that a week
changes at different times depending on locale (sunday or monday being
first day of week), but I don't have the WIN32 help files available right
now - and now win32 compiler either... :-)
bool is_same_week(SYSTEMTIME& s1,SYSTEMTIME& s2) {
FILETIME f1,f2;
ULARGE_INTEGER u1,u2;
#ifndef ALLOW_S2_LESS_THAN_S1
if (s2.wDayofWeek < s1.wDayOfWeek)
/* 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 remove first if case and
* insert this instead
*/
if (u1.QuadPart < u2.QuadPart) {
if (s2.wDayOfWeek < s1.wDayOfWeek) return false;
} else {
if (s1.wDayOfWeek < s2.wDayOfWeek) return false;
tmp = u1.QuadPart;
u1.QuadPart = u2.QuadPart;
u2.QuadPart = tmp;
}
#endif // ALLOW_S2_LESS_THAN_S1
if (u2.QuadPart < u1.QuadPart)
/* End of time interval before start... */
return false;
if (u2.QuadPart > u1.QuadPart > 7*24*3600*(ULONGLONG)(10000000))
/* s2 is more than 7 days away, so can't be in same week. */
return false;
return true;
}
/Per W
On Sat, 9 Nov 2002, Zodiaq wrote:
> Hello Dev-cpp-users,
>
> yeah, I know this is an off topic question - it is not directly
> related to development od Dev-C++ but I also know that this mailing
> list reads quite few people so - do you know a good way to check if
> two dates are inside one week... dates are in SYSTEMTIME or FILETIME
> format... my present idea is not as good as I wish it to be - I start
> from first date and try to get to the second one - if I pass sutarday
> and still not in second I know that weeks are different... but this
> forces some comparisons... know I think that I might check in which
> week of the year both dates are - but how to do that? So - if you have
> any brilliant ideas share them with me!
>
> --
> regards, Zodiaq
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Zodiaq <zo...@ac...> - 2002-11-10 08:40:31
|
Hello Per and all Dev-C++ users, Sunday, November 10, 2002, 1:19:36 AM, Per wrote as follows: [some quite nice code] Yeah this looks like a good idea... I'll have to correct few bugs (like not checking if we have more than a week between two dates - in that case code will work bad...) or first I'll try to get job done by checking in wkich week of the year this days are... maybe will get less comparisons... PW> /Per W -- regards, Zodiaq |
|
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
|