Board index » cppbuilder » Getting the WeekOfYear

Getting the WeekOfYear


2004-05-20 04:07:58 PM
cppbuilder101
Hi,
is there already a method in the vcl to obtain the week of the year (1-52)
or
do I have to implement it by myself?
Thanks
Helmut
 
 

Re:Getting the WeekOfYear

Hi,
I found this on newsgroups some time ago. It's Delphi but you should be
able to translate.
WeekNumber := (EncodeDate(year, month, day) - EncodeDate(year, 1, 1))
div 7 + 1
Olivier Bertini
Helmut Weismeier wrote:
Quote
Hi,

is there already a method in the vcl to obtain the week of the year (1-52)
or
do I have to implement it by myself?

Thanks
Helmut


 

Re:Getting the WeekOfYear

In C++ (this group is not Delphi) :-
TDateTime whatever=Now();
unsigned short ddd,mmm,yyy;
whatever.DecodeDate(&yyy,&mmm,&ddd);
int WeekNumber=(whatever-EncodeDate(yyy,1,1))/7+1;
--
Mark Jacobs
DK Computing
www.dkcomputing.co.uk
markj atty critical dotty co dotty uk
"Olivier Bertini" < XXXX@XXXXX.COM >wrote in message
Quote
WeekNumber := (EncodeDate(year, month, day) - EncodeDate(year, 1, 1))
div 7 + 1
Helmut Weismeier wrote:
>is there already a method in the vcl to obtain the week of the year (1-52)
>or do I have to implement it by myself?
 

{smallsort}

Re:Getting the WeekOfYear

"Helmut Weismeier" < XXXX@XXXXX.COM >wrote:
Quote
is there already a method in the vcl to obtain the week of
the year (1-52)
Word Year, WeekOfYear, ADayOfWeek;
DecodeDateWeek( Now(), &Year, &WeekOfYear, &DayOfWeek );
~ JD
 

Re:Getting the WeekOfYear

Mark Jacobs wrote:
Quote
TDateTime whatever=Now();
unsigned short ddd,mmm,yyy;
whatever.DecodeDate(&yyy,&mmm,&ddd);
int WeekNumber=(whatever-EncodeDate(yyy,1,1))/7+1;
That would indeed be the translation of:
Quote
WeekNumber := (EncodeDate(year, month, day) - EncodeDate(year, 1, 1))
div 7 + 1
But the formula is not correct.
One may of course argue about differences between 'the week' or 'what
is the weeknumber' or 'how many weeks (multiples of 7 days) went by
since the start of the year'. (The formula does the latest).
The formule does not account for a first day of a year that is not the
first day of a week.
If you want the weeknumber then try this:
int GetWeekNr ( TDateTime DateTime )
{
// new weeknumbers on Sundays
unsigned short year = 0 ;
unsigned short month = 0;
unsigned short day = 0;
DateTime.DecodeDate ( &year, &month, &day );
int daynr = DateTime - EncodeDate(year, 1, 1);
int dayofweek_firstdayofyear = EncodeDate(year, 1, 1).DayOfWeek();
//\a first day of a week has 1 == Sunday
return ( 7 - dayofweek_firstdayofyear + 2 + daynr ) / 7 + 1;
}
JD's solution with DecodeDateWeek() is the simplest, but it
is not available in bcb3.
Hans.
 

Re:Getting the WeekOfYear

"Helmut Weismeier" < XXXX@XXXXX.COM >wrote in message
Quote
is there already a method in the vcl to obtain the
week of the year (1-52) or do I have to implement it by myself?
Which version of BCB are you actually using? BCB6 has WeekOf() and
WeekOfTheYear() functions for that purpose.
Gambit
 

Re:Getting the WeekOfYear

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
Word Year, WeekOfYear, ADayOfWeek;
DecodeDateWeek( Now(), &Year, &WeekOfYear, &DayOfWeek );
FYI, that is only available in BCB6.
Gambit
 

Re:Getting the WeekOfYear

Hi altogether,
thank you for your help. I'm using BCB 5.
But I hope the shown solutions are suitable
for my needs.
Helmut
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im
Newsbeitrag news:40acf328$ XXXX@XXXXX.COM ...
Quote

"Helmut Weismeier" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>is there already a method in the vcl to obtain the
>week of the year (1-52) or do I have to implement it by myself?

Which version of BCB are you actually using? BCB6 has WeekOf() and
WeekOfTheYear() functions for that purpose.


Gambit


 

Re:Getting the WeekOfYear

"Helmut Weismeier" < XXXX@XXXXX.COM >wrote in message
Quote
thank you for your help. I'm using BCB 5.
The following is the algorithm that BCB6 uses for the WeekOfTheYear()
function:
typedef Word TDayTable[12];
typedef TDayTable* PDayTable;
TDayTable MonthDays[2] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
Word CDayMap[7] = {7, 1, 2, 3, 4, 5, 6};
const int DayMonday = 1;
const int DayTuesday = 2;
const int DayWednesday = 3;
const int DayThursday = 4;
const int DayFriday = 5;
const int DaySaturday = 6;
const int DaySunday = 7;
void __fastcall DivMod(int Dividend, Word Divisor, Word &Result, Word
&Remainder)
{
Result = (Word) (Dividend / Divisor);
Remainder = (Word) (Dividend % Divisor);
}
bool __fastcall DecodeDateFully(const TDateTime &AValue, Word &AYear,
Word &AMonth, Word &ADay, Word &ADayOfWeek)
{
const int D1 = 365;
const int D4 = D1 * 4 + 1;
const int D100 = D4 * 25 - 1;
const int D400 = D100 * 4 + 1;
Word Y, M, D, I;
int T;
PDayTable DayTable;
bool Result;
T = DateTimeToTimeStamp(AValue).Date;
if( T <= 0 )
{
AYear = 0;
AMonth = 0;
ADay = 0;
ADayOfWeek = 0;
Result = false;
}
else
{
ADayOfWeek = ((T % 7) + 1);
--T;
Y = 1;
while( T>= D400 )
{
T -= D400;
Y += 400;
}
DivMod(T, D100, I, D);
if( I == 4 )
{
--I;
D += D100;
}
Y += (I * 100);
DivMod(D, D4, I, D);
Y += (I * 4);
DivMod(D, D1, I, D);
if( I == 4 )
{
--I;
D += D1;
}
Y += I;
Result = IsLeapYear(Y);
DayTable = &MonthDays[Result];
M = 1;
while( true )
{
I = DayTable[M-1];
if( D < I ) break;
D -= I;
++M;
}
AYear = Y;
AMonth = M;
ADay = (D + 1);
}
return Result;
}
Word __fastcall DayOfTheWeek(const TDateTime &AValue)
{
return ((DateTimeToTimeStamp(AValue).Date - 1) % 7) + 1;
}
void __fastcall DecodeDateWeek(const TDateTime &AValue, Word &AYear,
Word &AWeekOfYear, Word &ADayOfWeek)
{
int LDayOfYear;
Word LMonth, LDay;
TDateTime LStart;
Word LStartDayOfWeek, LEndDayOfWeek;
bool LLeap;
LLeap = DecodeDateFully(AValue, AYear, LMonth, LDay, ADayOfWeek);
ADayOfWeek = CDayMap[ADayOfWeek-1];
LStart = EncodeDate(AYear, 1, 1);
LDayOfYear = (int)(AValue - LStart + 1);
LStartDayOfWeek = DayOfTheWeek(LStart);
if( (LStartDayOfWeek>= DayFriday) && (LStartDayOfWeek <=
DaySunday) )
LDayOfYear -= (8 - LStartDayOfWeek);
else
LDayOfYear += (LStartDayOfWeek - 1);
if( LDayOfYear <= 0 )
DecodeDateWeek(LStart - 1, AYear, AWeekOfYear, LDay);
else
{
AWeekOfYear = (LDayOfYear / 7);
if( (LDayOfYear % 7) != 0 )
++AWeekOfYear;
if( AWeekOfYear>52 )
{
LEndDayOfWeek = LStartDayOfWeek;
if( LLeap )
{
if( LEndDayOfWeek == DaySunday )
LEndDayOfWeek = DayMonday;
else
++LEndDayOfWeek;
}
if( (LEndDayOfWeek>= DayMonday) && (LEndDayOfWeek <=
DayWednesday) )
{
++AYear;
AWeekOfYear = 1;
}
}
}
}
Word __fastcall WeekOfTheYear(const TDateTime &AValue)
{
Word LYear, LDOW, LResult;
DecodeDateWeek(AValue, LYear, LResult, LDOW);
return LResult;
}
Complicated enough, huh? :-)
Gambit
 

Re:Getting the WeekOfYear

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
JD's solution with DecodeDateWeek() is the
simplest, but it is not available in bcb3.
See my other reply for code that you should be able to use in BCB3.
Gambit
 

Re:Getting the WeekOfYear

Remy Lebeau (TeamB) wrote:
Quote
The following is the algorithm that BCB6 uses for the WeekOfTheYear()
function:
typedef Word TDayTable[12];
typedef TDayTable* PDayTable;

TDayTable MonthDays[2] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
That is already in bcb3 too. In Sysutils. So I commented it out.
Quote
Word CDayMap[7] = {7, 1, 2, 3, 4, 5, 6};

const int DayMonday = 1;
const int DayTuesday = 2;
const int DayWednesday = 3;
const int DayThursday = 4;
const int DayFriday = 5;
const int DaySaturday = 6;
const int DaySunday = 7;
That is new.
Quote
DayTable = &MonthDays[Result];
M = 1;
while( true )
{
I = DayTable[M-1];
The last line does not compile. Had to change that in:
I = *DayTable[M-1];
Quote
Complicated enough, huh? :-)
Yes. That is a lot of code to add to bcb3 for the same result as
my little function GetWeekNr().
And what about using code from bcb6 in bcb3 without paying for it ?
Hans.
 

Re:Getting the WeekOfYear

Remy Lebeau (TeamB) wrote:
Quote
See my other reply for code that you should be able to use in BCB3.
Having done that I prefer my solution.
Hans.
 

Re:Getting the WeekOfYear

Hans Galema wrote:
Quote
Having done that I prefer my solution.
Another reason: GetWeekNr is 10% faster then WeekOfTheYear().
<g>
Hans.
 

Re:Getting the WeekOfYear

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
Remy Lebeau (TeamB) wrote:

>See my other reply for code that you should be able to use in BCB3.

Having done that I prefer my solution.
Does it produce the correct results? Did you try to break it?
I'm just wondering because I don't think that Borland would
ever produce code that was overly complicated - would they??
~ JD
 

Re:Getting the WeekOfYear

JD wrote:
Quote
Does it produce the correct results?
As far as I can see: Yes. The algorithm is correct. The
implementation too.
Quote
Did you try to break it?
How could I try ? Is't that something you would like to do ?
You would not miss an opportunity to put me wrong would you ? <g>.
Quote
I'm just wondering because I don't think that Borland would
ever produce code that was overly complicated - would they??
Did n't they already ?
Hans.