1 #include <time.h>
2 #include "ptoc.h"
3
timestamp(integer * day,integer * month,integer * year,integer * hour,integer * min,integer * sec)4 void timestamp(integer* day, integer* month, integer* year,
5 integer* hour, integer* min, integer* sec)
6 {
7 time_t ctime;
8 struct tm* tmp;
9
10 time(&ctime);
11 tmp = localtime(&ctime);
12
13 *day = tmp->tm_mday;
14 *month = tmp->tm_mon + 1;
15 *year = 1900 + tmp->tm_year;
16 *hour = tmp->tm_hour;
17 *min = tmp->tm_min;
18 *sec = tmp->tm_sec;
19 }
20
get_realtime()21 real get_realtime()
22 {
23 time_t ctime;
24 struct tm* tmp;
25
26 time(&ctime);
27 tmp = localtime(&ctime);
28
29 return (real)(tmp->tm_hour + tmp->tm_min / 60.0);
30 }
31
round(real x)32 integer round(real x)
33 {
34 return x >= 0 ? trunc(x + 0.5) : trunc(x - 0.5);
35 }
36
37 #define CIRCULAR_BUFFER_SIZE 4096
38
lpsz(int low,int high,const char * str)39 char* lpsz(int low, int high, const char* str)
40 {
41 static char buffer[CIRCULAR_BUFFER_SIZE];
42 static int pos = 0;
43 int size;
44 char* ptr;
45
46 while (high >= low && str[high-low] == ' ') {
47 high -= 1;
48 }
49 size = high - low + 1;
50 if (pos + size >= sizeof(buffer)) {
51 pos = 0;
52 }
53 ptr = &buffer[pos];
54 pos += size + 1;
55 memcpy(ptr, str, size);
56 ptr[size] = '\0';
57 return ptr;
58 }
59
60
61 unsigned randseed;
62 const unsigned modulus = 2147483647;
63 const unsigned factor = 397204094;
64
Randomize()65 void Randomize()
66 {
67 randseed = time(NULL);
68 }
69
randint(unsigned range)70 unsigned randint(unsigned range)
71 {
72 randseed = randseed * factor % modulus;
73 return range ? randseed % range : 0;
74 }
75
randreal()76 double randreal()
77 {
78 randseed = randseed * factor % modulus;
79 return (double)randseed / modulus;
80 }
81
get_date()82 char* get_date()
83 {
84 static char buf[16];
85
86 time_t ctime;
87 struct tm* tmp;
88
89 time(&ctime);
90 tmp = localtime(&ctime);
91
92 sprintf(buf, "%02d.%02d.%4d", tmp->tm_mday, tmp->tm_mon + 1, 1900 + tmp->tm_year);
93 return buf;
94 }
95
get_time()96 char* get_time()
97 {
98 static char buf[8];
99
100 time_t ctime;
101 struct tm* tmp;
102
103 time(&ctime);
104 tmp = localtime(&ctime);
105
106 sprintf(buf, "%d:%d", tmp->tm_hour, tmp->tm_min);
107 return buf;
108 }
109
110