Debian Codegraph @

project_files/hwc/rtl/sysutils.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 // Semi-dummy implementation of FormatDateTime
84 string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime)
85 {
86 string255 buffer = STRINIT(FormatStr.str);
87 time_t rawtime;
88 struct tm* my_tm;
89
90 // DateTime is ignored, always uses current time.
91 // TODO: Use DateTime argument properly.
92 time(&rawtime);
93 my_tm = localtime(&rawtime);
94
95 // Currently uses a hardcoded format string, FormatStr is ignored.
96 // The format strings for C and Pascal differ!
97 // TODO: Use FormatStr argument properly.
98 size_t len = strftime(buffer.str, sizeof(buffer.str), "%Y-%m-%d-%H-%M-%S-0", my_tm);
99 buffer.len = len;
100 return buffer;
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
145 string255 fpcrtl_extractFileDir(string255 f)
146 {
147 const char sep[] = {'\\', '/', ':'};
148 LongInt i,j;
149
150 i = f.len - 1;
151 while(i >= 0){
152 for(j = 0; j < sizeof(sep); j++){
153 if(f.str[i] == sep[j]){
154 goto FPCRTL_EXTRACTFILEDIR_END;
155 }
156 }
157 i--;
158 }
159 FPCRTL_EXTRACTFILEDIR_END:
160 return fpcrtl_copy(f, 1, i);
161 }
162
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 {
195 return fpcrtl_pchar2str(p);
196 }