author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 07 Jun 2020 00:44:13 +0200 | |
changeset 15604 | 22d0a3d6e2be |
parent 14913 | 68e1783762bc |
child 15877 | 6cb7330113d8 |
permissions | -rw-r--r-- |
10015 | 1 |
#include "SysUtils.h" |
2 |
||
3 |
#include <time.h> |
|
4 |
#include <stdio.h> |
|
5 |
#include <stdlib.h> |
|
6 |
#include <string.h> |
|
7 |
||
8 |
#include "system.h" |
|
9 |
#include "misc.h" |
|
10 |
||
11 |
TDateTime fpcrtl_date() |
|
12 |
{ |
|
13 |
const int num_days_between_1900_1980 = 29220; |
|
14 |
||
15 |
struct tm ref_date; |
|
16 |
struct tm cur_date; |
|
17 |
time_t local_time; |
|
18 |
time_t ref_time, cur_time; |
|
19 |
||
20 |
double timeDiff; |
|
21 |
double day_time_frac; //fraction that represents the time in one day |
|
22 |
int num_seconds; |
|
23 |
int numDays; |
|
24 |
||
25 |
// unix epoch doesn't work, choose Jan 1st 1980 instead |
|
26 |
ref_date.tm_year = 80; |
|
27 |
ref_date.tm_mon = 0; |
|
28 |
ref_date.tm_mday = 1; |
|
29 |
ref_date.tm_hour = 0; |
|
30 |
ref_date.tm_min = 0; |
|
31 |
ref_date.tm_sec = 0; |
|
32 |
ref_date.tm_isdst = 0; |
|
33 |
ref_date.tm_wday = 0; // ignored |
|
34 |
ref_date.tm_yday = 0; // ignored |
|
35 |
||
36 |
local_time = time(NULL); |
|
37 |
cur_date = *localtime(&local_time); |
|
38 |
||
39 |
cur_date.tm_hour = 0; |
|
40 |
cur_date.tm_min = 0; |
|
41 |
cur_date.tm_sec = 0; |
|
42 |
||
43 |
ref_time = mktime(&ref_date); |
|
44 |
cur_time = mktime(&cur_date); |
|
45 |
||
46 |
timeDiff = difftime(cur_time, ref_time); |
|
47 |
numDays = fpcrtl_round(timeDiff / 3600 / 24) + num_days_between_1900_1980 + 1; |
|
48 |
||
49 |
fpcrtl_printf("[date] tim diff: %f\n", timeDiff); |
|
50 |
fpcrtl_printf("[date] num days between 1980 and today: %d\n", fpcrtl_round(timeDiff/3600/24)); |
|
51 |
fpcrtl_printf("[date] current date: %s\n", asctime(&cur_date)); |
|
52 |
fpcrtl_printf("[date] reference date: %s\n", asctime(&ref_date)); |
|
53 |
fpcrtl_printf("[date] num days: %d\n", numDays); |
|
54 |
||
55 |
return numDays; |
|
56 |
} |
|
57 |
||
58 |
TDateTime fpcrtl_time() |
|
59 |
{ |
|
60 |
struct tm cur_date; |
|
61 |
time_t local_time; |
|
62 |
time_t cur_time; |
|
63 |
||
64 |
double day_time_frac; //fraction that represents the time in one day |
|
65 |
int num_seconds; |
|
66 |
||
67 |
local_time = time(NULL); |
|
68 |
cur_date = *localtime(&local_time); |
|
69 |
||
70 |
num_seconds = cur_date.tm_hour * 3600 + cur_date.tm_min * 60 + cur_date.tm_sec; |
|
71 |
day_time_frac = num_seconds / 3600.0 / 24.0; |
|
72 |
||
73 |
fpcrtl_printf("%f\n", day_time_frac); |
|
74 |
||
75 |
return day_time_frac; |
|
76 |
} |
|
77 |
||
78 |
TDateTime fpcrtl_now() |
|
79 |
{ |
|
80 |
return fpcrtl_date() + fpcrtl_time(); |
|
81 |
} |
|
82 |
||
14913
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
83 |
// Semi-dummy implementation of FormatDateTime |
10015 | 84 |
string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime) |
85 |
{ |
|
14913
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
86 |
string255 buffer = STRINIT(FormatStr.str); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
87 |
time_t rawtime; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
88 |
struct tm* my_tm; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
89 |
|
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
90 |
// DateTime is ignored, always uses current time. |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
91 |
// TODO: Use DateTime argument properly. |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
92 |
time(&rawtime); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
93 |
my_tm = localtime(&rawtime); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
94 |
|
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
95 |
// Currently uses a hardcoded format string, FormatStr is ignored. |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
96 |
// The format strings for C and Pascal differ! |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
97 |
// TODO: Use FormatStr argument properly. |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
98 |
size_t len = strftime(buffer.str, sizeof(buffer.str), "%Y-%m-%d-%H-%M-%S-0", my_tm); |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
99 |
buffer.len = len; |
68e1783762bc
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents:
10564
diff
changeset
|
100 |
return buffer; |
10015 | 101 |
} |
102 |
||
103 |
string255 fpcrtl_trim(string255 s) |
|
104 |
{ |
|
105 |
int left, right; |
|
106 |
||
107 |
if(s.len == 0){ |
|
108 |
return s; |
|
109 |
} |
|
110 |
||
111 |
for(left = 0; left < s.len; left++) |
|
112 |
{ |
|
113 |
if(s.str[left] != ' '){ |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
for(right = s.len - 1; right >= 0; right--) |
|
119 |
{ |
|
120 |
if(s.str[right] != ' '){ |
|
121 |
break; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
if(left > right){ |
|
126 |
s.len = 0; |
|
127 |
s.str[0] = 0; |
|
128 |
return s; |
|
129 |
} |
|
130 |
||
131 |
s.len = right - left + 1; |
|
132 |
memmove(s.str, s.str + left, s.len); |
|
133 |
||
134 |
s.str[s.len] = 0; |
|
135 |
||
136 |
return s; |
|
137 |
} |
|
138 |
||
139 |
Integer fpcrtl_strToInt(string255 s) |
|
140 |
{ |
|
141 |
s.str[s.len] = 0; |
|
142 |
return atoi(s.str); |
|
143 |
} |
|
144 |
||
10564
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
145 |
string255 fpcrtl_extractFileDir(string255 f) |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
146 |
{ |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
147 |
const char sep[] = {'\\', '/', ':'}; |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
148 |
LongInt i,j; |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
149 |
|
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
150 |
i = f.len - 1; |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
151 |
while(i >= 0){ |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
152 |
for(j = 0; j < sizeof(sep); j++){ |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
153 |
if(f.str[i] == sep[j]){ |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
154 |
goto FPCRTL_EXTRACTFILEDIR_END; |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
155 |
} |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
156 |
} |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
157 |
i--; |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
158 |
} |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
159 |
FPCRTL_EXTRACTFILEDIR_END: |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
160 |
return fpcrtl_copy(f, 1, i); |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
161 |
} |
0cb20aa8877a
more fixing and allow pas2c to run tests. they will still fail though - engine does not exit with the specified exit codes, also data types are messed up
sheepluva
parents:
10137
diff
changeset
|
162 |
|
10015 | 163 |
//function ExtractFileName(const FileName: string): string; |
164 |
//var |
|
165 |
// i : longint; |
|
166 |
// EndSep : Set of Char; |
|
167 |
//begin |
|
168 |
// I := Length(FileName); |
|
169 |
// EndSep:=AllowDirectorySeparators+AllowDriveSeparators; |
|
170 |
// while (I > 0) and not (FileName[I] in EndSep) do |
|
171 |
// Dec(I); |
|
172 |
// Result := Copy(FileName, I + 1, MaxInt); |
|
173 |
//end; |
|
174 |
||
175 |
string255 fpcrtl_extractFileName(string255 f) |
|
176 |
{ |
|
177 |
const char sep[] = {'\\', '/', ':'}; |
|
178 |
LongInt i,j; |
|
179 |
||
180 |
i = f.len - 1; |
|
181 |
while(i >= 0){ |
|
182 |
for(j = 0; j < sizeof(sep); j++){ |
|
183 |
if(f.str[i] == sep[j]){ |
|
184 |
goto FPCRTL_EXTRACTFILENAME_END; |
|
185 |
} |
|
186 |
} |
|
187 |
i--; |
|
188 |
} |
|
189 |
FPCRTL_EXTRACTFILENAME_END: |
|
190 |
return fpcrtl_copy(f, i + 2, 256); |
|
191 |
} |
|
192 |
||
193 |
string255 fpcrtl_strPas(PChar p) |
|
194 |
{ |
|
10137 | 195 |
return fpcrtl_pchar2str(p); |
10015 | 196 |
} |