author | unc0rr |
Thu, 12 Nov 2015 23:38:01 +0300 | |
changeset 11375 | 55360683db75 |
parent 10564 | 0cb20aa8877a |
child 14918 | 68e1783762bc |
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 |
||
83 |
/* |
|
84 |
* XXX: dummy |
|
85 |
*/ |
|
86 |
string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime) |
|
87 |
{ |
|
88 |
string255 result = STRINIT("2012 01-01"); |
|
89 |
return result; |
|
90 |
} |
|
91 |
||
92 |
string255 fpcrtl_trim(string255 s) |
|
93 |
{ |
|
94 |
int left, right; |
|
95 |
||
96 |
if(s.len == 0){ |
|
97 |
return s; |
|
98 |
} |
|
99 |
||
100 |
for(left = 0; left < s.len; left++) |
|
101 |
{ |
|
102 |
if(s.str[left] != ' '){ |
|
103 |
break; |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
for(right = s.len - 1; right >= 0; right--) |
|
108 |
{ |
|
109 |
if(s.str[right] != ' '){ |
|
110 |
break; |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
if(left > right){ |
|
115 |
s.len = 0; |
|
116 |
s.str[0] = 0; |
|
117 |
return s; |
|
118 |
} |
|
119 |
||
120 |
s.len = right - left + 1; |
|
121 |
memmove(s.str, s.str + left, s.len); |
|
122 |
||
123 |
s.str[s.len] = 0; |
|
124 |
||
125 |
return s; |
|
126 |
} |
|
127 |
||
128 |
Integer fpcrtl_strToInt(string255 s) |
|
129 |
{ |
|
130 |
s.str[s.len] = 0; |
|
131 |
return atoi(s.str); |
|
132 |
} |
|
133 |
||
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
|
134 |
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
|
135 |
{ |
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
|
136 |
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
|
137 |
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
|
138 |
|
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
} |
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 |
} |
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 |
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
|
147 |
} |
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 |
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
|
149 |
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
|
150 |
} |
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 |
|
10015 | 152 |
//function ExtractFileName(const FileName: string): string; |
153 |
//var |
|
154 |
// i : longint; |
|
155 |
// EndSep : Set of Char; |
|
156 |
//begin |
|
157 |
// I := Length(FileName); |
|
158 |
// EndSep:=AllowDirectorySeparators+AllowDriveSeparators; |
|
159 |
// while (I > 0) and not (FileName[I] in EndSep) do |
|
160 |
// Dec(I); |
|
161 |
// Result := Copy(FileName, I + 1, MaxInt); |
|
162 |
//end; |
|
163 |
||
164 |
string255 fpcrtl_extractFileName(string255 f) |
|
165 |
{ |
|
166 |
const char sep[] = {'\\', '/', ':'}; |
|
167 |
LongInt i,j; |
|
168 |
||
169 |
i = f.len - 1; |
|
170 |
while(i >= 0){ |
|
171 |
for(j = 0; j < sizeof(sep); j++){ |
|
172 |
if(f.str[i] == sep[j]){ |
|
173 |
goto FPCRTL_EXTRACTFILENAME_END; |
|
174 |
} |
|
175 |
} |
|
176 |
i--; |
|
177 |
} |
|
178 |
FPCRTL_EXTRACTFILENAME_END: |
|
179 |
return fpcrtl_copy(f, i + 2, 256); |
|
180 |
} |
|
181 |
||
182 |
string255 fpcrtl_strPas(PChar p) |
|
183 |
{ |
|
10137 | 184 |
return fpcrtl_pchar2str(p); |
10015 | 185 |
} |