SRL Archive - SRL Bugs

TheDate(1) bug


Quickmarch

2009-11-19 05:32:01

There's an bug in TheDate so that it adds an extra number.

Fix:


function TheDate(DateFormat: Integer): string;
var
Year, Month, Day: Word;
M, D: string;
Mnths, sfx: TStringArray;
begin
DecodeDate(Date, Year, Month, Day);
Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
sfx := ['st', 'nd', 'rd', 'th'];
case DateFormat of
1:
begin
m := Mnths[Month - 1];
D := (IntToStr(Day)[Length(IntToStr(Day))]);
if StrInArr(D, ['1', '2', '3']) then
D := sfx[Max(StrToInt(D), 4) - 1];
Result := M + ' ' + IntToStr(Day) + {Removed something here}', ' + IntToStr(Year);
end;
2: Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
3: Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
else
srl_Warn('TheDate', 'DateFormat can only be 1, 2, or 3!', warn_AllVersions);
end;
end;

Gu3st...

2009-11-19 07:33:10

Now D isn't used… What about doing "Result := M + ' ' + D +', ' + IntToStr(Year);" instead?

Narcle

2009-11-19 13:21:07

Ya I removed the extra vars etc and this is what i get:

function TheDate(DateFormat: Integer): string;
var
Year, Month, Day: Word;
Mnths: TStringArray;
begin
DecodeDate(Date, Year, Month, Day);
Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
case DateFormat of
1: Result := Mnths[Month - 1] + ' ' + IntToStr(Day) + ', ' + IntToStr(Year);
2: Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
3: Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
else
srl_Warn('TheDate', 'DateFormat can only be 1, 2, or 3!', warn_AllVersions);
end;
end;

Still works correctly from my testings.

edit:
Ah I see what its suppose to do. Its suppose to add "st" "nd" "rd" "th" at the end of the day.

Quickmarch

2009-11-19 13:23:57

Yeah, works for me. :)

Narcle

2009-11-19 13:42:48

Edited my post, that's as basic as it can get. If we still want "st" "nd" "rd" "th" for the days then it would require extra case statements / conditions. Because for me you don't have 11st you have 11th, or 12nd you have 12th, until you get to the 20's/30s do u have 21st, 22nd, 23rd & 31st.

Would be kinda dumb to have it then for it to be wrong.

Nava2

2009-11-19 14:46:11

From: Narcle
Edited my post, that's as basic as it can get. If we still want "st" "nd" "rd" "th" for the days then it would require extra case statements / conditions. Because for me you don't have 11st you have 11th, or 12nd you have 12th, until you get to the 20's/30s do u have 21st, 22nd, 23rd & 31st.

Would be kinda dumb to have it then for it to be wrong.


English fail right there..

Perhaps just an if statement, if 13? or something along those lines. I think the suffixes are a nice addition.

Sir R. M8gic1an

2009-11-19 14:49:21

GAAAH constants, not 1, 2, 3!

~RM

Nava2

2009-11-19 14:51:11

From: Rasta Magician
GAAAH constants, not 1, 2, 3!

~RM


To be quite honest, I could have sworn I added them..

Narcle

2009-11-19 15:38:25

I guess I failed to mention that the function bases "st" etc on the last digit of the day. So therefor it would add st to 11 = 11st which is wrong.

Nava2

2009-11-19 15:43:37

From: Narcle
I guess I failed to mention that the function bases "st" etc on the last digit of the day. So therefor it would add st to 11 = 11st which is wrong.


Not really hard to avoid.. if > 13?

Narcle

2009-11-19 16:14:39

From: Nava2
Not really hard to avoid.. if > 13?


1st, 2nd, 3rd, 11th, 12th, 13th, 21st, 22nd, 23rd, 31st

Yes I know it can be done, just have to add bunch of cases/if-then's like I mentioned.

Bobarkinator

2009-11-19 19:43:02

function TheDate(DateFormat: Integer): string;
var
Year, Month, Day: Word;
Mnths: TStringArray;
DayStr: String;
begin
DecodeDate(Date, Year, Month, Day);
Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
Case (Day Mod 10) Of
1: If Not (Day = 11) Then DayStr := IntToStr(Day) + 'st';
2: If Not (Day = 12) Then DayStr := IntToStr(Day) + 'nd';
3: If Not (Day = 13) Then DayStr := IntToStr(Day) + 'rd';
Else
DayStr := IntToStr(Day) + 'th';
End;
case DateFormat of
1: Result := Mnths[Month - 1] + ' ' + DayStr + ', ' + IntToStr(Year);
2: Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
3: Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
else
srl_Warn('TheDate', 'DateFormat can only be 1, 2, or 3!', warn_AllVersions);
end;
end;

Just took one extra case statement.

Narcle

2009-11-19 22:44:13

@Bob
11th returns: November , 2009
12th returns: November , 2009
13th returns: November , 2009

It doesn't do anything when its 11,12 or 13 due to the If not then you don't tell it do do anything else if its 11,12 or 13.

I'll figure out a system later. Doing some stuff atm.

Bobarkinator

2009-11-19 22:47:12

Whoops, my bad. Not a hard fix, but by all means, come up with a better system.

Narcle

2009-11-21 19:01:45

Fixed in open-dev svn now. Also added const's for it.

{*******************************************************************************
function TheDate(DateFormat : Integer) : String;
By: Ron, Nava2 & Narcle
Description: TheDate will return the current date. DateFormats can be...
Date_Formal = April 2nd, 2007 Month Day, Year
Date_Month = 04/02/07 Month/Day/Year
Date_Day = 02-04-07 Day-Month-Year
*******************************************************************************}

function TheDate(DateFormat: Integer): string;
var
Year, Month, Day: Word;
D: string;
Mnths, sfx: TStringArray;
begin
DecodeDate(Date, Year, Month, Day);
Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'];
sfx := ['st', 'nd', 'rd'];
case DateFormat of
1:
begin
if InRange(Day mod 10, 1, 3) and not InRange(Day, 11, 13) then
D := sfx[(Day mod 10)-1]
else
D := 'th';
Result := Mnths[Month - 1] + ' ' + IntToStr(Day) + D +', ' + IntToStr(Year);
end;
2: Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
3: Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
else
srl_Warn('TheDate', 'DateFormat can only be 1, 2, or 3!', warn_AllVersions);
end;
end;

Gu3st...

2009-11-21 19:08:19

I can't see any use of const's there…?

Narcle

2009-11-21 19:17:41

From: Rasta Magician
GAAAH constants, not 1, 2, 3!

~RM


From: Zyt3x
I can't see any use of const's there…?


Because we are going const based.

ZephyrsFury

2009-11-23 11:35:43

From: Narcle
Because we are going const based.


Maybe he means you should be using the constants in the function too:


case DateFormat of
Date_Formal:
begin
if InRange(Day mod 10, 1, 3) and not InRange(Day, 11, 13) then
D := sfx[(Day mod 10)-1]
else
D := 'th';
Result := Mnths[Month - 1] + ' ' + IntToStr(Day) + D +', ' + IntToStr(Year);
end;
Date_Month: Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
Date_Day: Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
else

Gu3st...

2009-11-23 12:44:38

From: ZephyrsFury
Maybe he means you should be using the constants in the function too:
Yes, that's what I though RM meant..?

Narcle

2009-11-23 19:52:24

Whoops, I'll change it later when something else needs to be committed.