summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/units/sysutils/strtotimetest.pp
blob: fec6d0e17d66910266725cb1ec3537c52053c3c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
program strtmtest;

{$ifdef FPC}
  {$mode objfpc}{$H+}
{$else}
  {$apptype console}
{$endif}

uses sysutils,sysconst{$ifndef fpc},windows{$endif};

{$ifndef fpc}
function defaultformatsettings:TFormatSettings;
begin
  GetLocaleFormatSettings(getsystemdefaultlcid,result);
end;
{$endif}
var exitwitherror:integer =0;
    fmt : TFormatSettings;

Procedure Check(TestNo : Integer; inputstr : String;shouldfailstrtotime:boolean=false;shouldfailcomparison:boolean=false;resultstr:string='');

var dt :TDateTime;
    outputstr:ansistring;

begin
  if TryStrToTime(inputstr,dt,fmt) then
   begin
     if shouldfailstrtotime then
       begin
         writeln('test ',TestNo,' should fail on strtotime while it didn''t ',timetostr(dt,fmt));
         exitwitherror:=1;
       end
     else
       begin
         outputstr:=TimeToStr(dt,fmt); // note because of this bugs can also be in timetostr
         if resultstr<>'' then
            begin
              if outputstr<>resultstr then
                begin
                  writeln('test ',TestNo,' should be "',resultstr,'" is "',outputstr,'"');
                  exitwitherror:=1;
                end;
              exit; // don't do other comparisons
            end;

         if inputstr<>outputstr then
           begin
            if not shouldfailcomparison then
              begin
                writeln('test ',TestNo,' failed "',inputstr,'" <> "',outputstr,'"');
                exitwitherror:=1;
              end;
           end
         else
           begin
            if shouldfailcomparison then
              begin
                writeln('test ',TestNo,' succeeded "',inputstr,'" = "',outputstr,'", while it shouldn''t');
                exitwitherror:=1;
              end;
           end;
       end;
   end
  else
    if not shouldfailstrtotime then
     begin
       Writeln('Test ',TestNo,' failed: ',inputstr);
       exitwitherror:=1;
    end;
end;

procedure setdecimalsep(c:char);
begin
  fmt.DecimalSeparator:=c;
  fmt.longtimeformat:='hh:nn:ss'+fmt.DecimalSeparator+'zzz';
end;

var value: word;
  code : longint;
begin
  fmt:=defaultformatsettings;
  fmt.TimeSeparator:=':';
  fmt.TimeAmstring:='AM';
  fmt.TimePmstring:='PM';

  setdecimalsep('.');
  Check( 0,'12:34:45.789',false,false);
  Check( 1,'12:34:45,789',true,false);

  setdecimalsep(',');
  Check( 2,'12:34:45.789',true,false);
  Check( 3,'12:34:45,789',false,false);

  Check( 4,'12 am',false,false,'00:00:00,000');
  Check( 5,'pm 12:34',false,false,'12:34:00,000');
  Check( 6,'12::45',true,false);
  Check( 7,'12:34:56 px',true,false);
  Check( 8,'12:34:5x',true,false);
  Check( 9,'12:34:56:78:90',true,false);
  Check(10,'5 am',false,false,'05:00:00,000');
  Check(11,'5 pm',false,false,'17:00:00,000');
  Check(12,'am 5',false,false,'05:00:00,000');
  Check(13,'pm 5',false,false,'17:00:00,000');
  fmt.longtimeformat:='hh:nn:ss'+fmt.DecimalSeparator+'zzz am/pm';
  Check(14,'5 am',false,false,'05:00:00,000 am');
  Check(15,'5 pm',false,false,'05:00:00,000 pm');
  Check(16,'am 5',false,false,'05:00:00,000 am');
  Check(17,'pm 5',false,false,'05:00:00,000 pm');
  fmt.TimeAmstring:='AM';
  fmt.TimePmstring:='PM';
  fmt.longtimeformat:='hh:nn:ss'+fmt.DecimalSeparator+'zzz a/p';
  Check(18,'am 5',false,false,'05:00:00,000 a');
  Check(19,'pm 5',false,false,'05:00:00,000 p');

  fmt.TimeAMString:='a'; fmt.TimePMString:='p';

  Check(20,'a 5',false,false,'05:00:00,000 a');
  Check(21,'p 5',false,false,'05:00:00,000 p');
  Check(22,'12:',True,false);
  Check(23,'13:14:',True,false);
  Check(24,'a 17:00',True,false);
  Check(25,'p 19:00',True,false);
  Check(26,'1:2:3',false,false,'01:02:03,000 a');
  Check(27,'1:4',false,false,'01:04:00,000 a');
  Check(28,'111:2:3',True,false);
  Check(29,'1:444',True,false);
  Check(30,'1:2:333',True,false);
  Check(31,'1:4:55,4',False,false,'01:04:55,004 a');
  Check(32,'1:4:55,12',False,false,'01:04:55,012 a');
  Check(33,'1:4:55,004',False,false,'01:04:55,004 a');
  Check(34,'1:4:55,0012',False,false,'01:04:55,012 a');
  Check(35,'1:4:55,004'#9'am',true,false,'01:04:55,004'#9'am');
  Check(36,#9'1:4:55,0012',true,false,'01:04:55,012 a');
  Check(37,' 1:4:55,4',False,false,'01:04:55,004 a');
  Check(38,'1: 4:55,12',False,false,'01:04:55,012 a');
  Check(39,'1:4: 55,004',False,false,'01:04:55,004 a');
  Check(40,'1:4:55, 2',False,false,'01:04:55,002 a');
  Check(41,'1:4:55,   4',False,false,'01:04:55,004 a'); // note more padding then needed
  Check(42,'1:    4:55,   4',False,false,'01:04:55,004 a'); // note more padding then needed
  Check(43,'1:  4:   55,   4',False,false,'01:04:55,004 a'); // note more padding then needed
  Check(44,'1:  4:  55,   4',False,false,'01:04:55,004 a'); // note more padding then needed
  Check(45,'1 4 55 4',True,false);
  fmt.timeseparator:=' ';
  Check(46,'01 04 55',True,false);
  Check(47,'a 01',false,false,'01 00 00,000 a');
  Check(52,'a01',false,false,'01 00 00,000 a');
  fmt.TimeSeparator:=':';
  Check(48,'1:4:55,0000000000000000000000012',false,false,'01:04:55,012 a');
  Check(49,'1:4:55,0000100012',True,false);
  Check(50,'1:4:55,000001012',True,false);
  Check(51,'12:034:00056',false,false,'12:34:56,000 p');

  exitcode:=exitwitherror;

 {$ifndef fpc} // halt in delphi ide  
  readln;
 {$endif}
end.