1 //  This may look like C code, but it is really -*- C++ -*-
2 
3 //  ------------------------------------------------------------------
4 //  The Goldware Library
5 //  Copyright (C) 1990-1999 Odinn Sorensen
6 //  Copyright (C) 1999-2000 Alexander S. Aganichev
7 //  ------------------------------------------------------------------
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Library General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Library General Public License for more details.
17 //
18 //  You should have received a copy of the GNU Library General Public
19 //  License along with this program; if not, write to the Free
20 //  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 //  MA 02111-1307, USA
22 //  ------------------------------------------------------------------
23 //  $Id: gtimall.h,v 1.19 2011/02/21 13:09:11 stas_degteff Exp $
24 //  ------------------------------------------------------------------
25 //  Time utility functions.
26 //  ------------------------------------------------------------------
27 
28 #ifndef __gtimall_h
29 #define __gtimall_h
30 
31 
32 //  ------------------------------------------------------------------
33 
34 #include <ctime>
35 #include <cstddef>
36 #include <gdefs.h>
37 #ifdef __UNIX__
38 #include <unistd.h>
39 #include <sys/times.h>
40 #endif
41 #ifdef __OS2__
42 #define INCL_BASE
43 #include <os2.h>
44 #endif
45 #ifdef __WIN32__
46 #include <windows.h>
47 #endif
48 #if __VISUAL_C_NOT_LESS(14,0)
49 //#if defined(_MSC_VER) && (_MSC_VER >= 1400)
50 #include <time.h>
51 #endif
52 
53 #include "gstrall.h"
54 
55 //  ------------------------------------------------------------------
56 
57 typedef long Clock;
58 
59 
60 //  ------------------------------------------------------------------
61 
62 #if defined(GOLD_CANPACK)
63 #pragma pack(1)
64 #endif
65 
66 
67 //  ------------------------------------------------------------------
68 //  DOS "findfirst" timestamp
69 
70 struct gfiletime {
71   unsigned ft_tsec  : 5;   // Second / 2
72   unsigned ft_min   : 6;   // Minutes
73   unsigned ft_hour  : 5;   // Hours
74   unsigned ft_day   : 5;   // Days
75   unsigned ft_month : 4;   // Months
76   unsigned ft_year  : 7;   // Year - 80
77 
78   const char* c_str(char* buf);
numbergfiletime79   dword number() { return *(dword*)this; }
80 };
81 
82 typedef gfiletime FFTime;
83 
84 
85 //  ------------------------------------------------------------------
86 //  Opus DOS-style file timestamp
87 
88 struct gopustime {
89   unsigned ft_day   : 5;   // Days
90   unsigned ft_month : 4;   // Months
91   unsigned ft_year  : 7;   // Year - 80
92   unsigned ft_tsec  : 5;   // Second / 2
93   unsigned ft_min   : 6;   // Minutes
94   unsigned ft_hour  : 5;   // Hours
95 
96   const char* c_str(char* buf);
numbergopustime97   dword number() { return *(dword*)this; }
98 };
99 
100 typedef gopustime FTime;
101 
102 
103 //  ------------------------------------------------------------------
104 
105 #if defined(GOLD_CANPACK)
106 #pragma pack()
107 #endif
108 
109 
110 //  ------------------------------------------------------------------
111 //  Externs for strftimei()
112 
113 extern char* __gsweekday[];
114 extern char* __glweekday[];
115 extern char* __gsmonth[];
116 extern char* __glmonth[];
117 extern char* __gampm[];
118 
119 extern char* gsweekday[];
120 extern char* glweekday[];
121 extern char* gsmonth[];
122 extern char* glmonth[];
123 extern char* gampm[];
124 
125 extern const char* gmonths[];
126 
127 
128 //  ------------------------------------------------------------------
129 //  Prototypes
130 
ggmtime(struct tm * _tm,const time32_t * timep)131 inline void ggmtime(struct tm *_tm, const time32_t *timep)
132 {
133   const time_t temp(*timep);
134 #if __VISUAL_C_NOT_LESS(14,0)
135 //#if defined(_MSC_VER) && (_MSC_VER >= 1400)
136   if (0 != gmtime_s(_tm, &temp))
137   {
138     const time_t zero(0);
139     gmtime_s(_tm, &zero);
140   }
141 #else
142   struct tm *time = gmtime(&temp);
143 #if defined(__WIN32__)
144   if (NULL == time)
145   {
146     const time_t zero(0);
147     time = gmtime(&zero);
148   }
149 #endif
150   *_tm = *time;
151 #endif
152 }
153 
glocaltime(struct tm * _tm,const time32_t * timep)154 inline void glocaltime(struct tm *_tm, const time32_t *timep)
155 {
156   const time_t temp(*timep);
157 #if __VISUAL_C_NOT_LESS(14,0)
158 //#if defined(_MSC_VER) && (_MSC_VER >= 1400)
159   if (0 != localtime_s(_tm, &temp))
160   {
161     const time_t zero(0);
162     localtime_s(_tm, &zero);
163   }
164 #else
165   struct tm *time = localtime(&temp);
166 #if defined(__WIN32__)
167   if (NULL == time)
168   {
169     const time_t zero(0);
170     time = localtime(&zero);
171   }
172 #endif
173   *_tm = *time;
174 #endif
175 }
176 
gctime(TCHAR * buffer,size_t sizeInChars,const time32_t * timep)177 inline void gctime(TCHAR *buffer, size_t sizeInChars, const time32_t *timep)
178 {
179   const time_t temp(*timep);
180 #if __VISUAL_C_NOT_LESS(14,0)
181 //#if defined(_MSC_VER) && (_MSC_VER >= 1400)
182   if (0 != _tctime_s(buffer, sizeInChars, &temp))
183   {
184     const time_t zero(0);
185     _tctime_s(buffer, sizeInChars, &zero);
186   }
187 #else
188   const char *time = ctime(&temp);
189 #if defined(__WIN32__)
190   if (NULL == time)
191   {
192     const time_t zero(0);
193     time = _tctime(&zero);
194   }
195 #endif
196   strxcpy(buffer, time, sizeInChars);
197 #endif
198 }
199 
gtime(time32_t * timep)200 inline time32_t gtime(time32_t *timep)
201 {
202   time32_t temp = (time32_t)time(NULL);
203   return timep ? *timep = temp : temp;
204 }
205 
gmktime(struct tm * timep)206 inline time32_t gmktime(struct tm *timep)
207 {
208   return (time32_t)mktime(timep);
209 }
210 
211 
212 #if defined(__OS2__)
usleep(int duration)213 inline void usleep(int duration) { DosSleep(duration); }
214 #elif (defined(__MINGW32__) && __GNUC_LESS(3,4)) || defined(_MSC_VER)
215 //#elif (defined(__MINGW32__) && __GNUC__*100+__GNUC_MINOR__ < 304) || defined(_MSC_VER)
usleep(long duration)216 inline void usleep(long duration) { Sleep(duration); }
217 #endif
218 
219 #ifndef CLK_TCK
220 #define CLK_TCK CLOCKS_PER_SEC
221 #endif
222 
223 #ifdef __UNIX__
gclock()224 inline Clock gclock() { struct tms z; return Clock(times(&z)*10/sysconf(_SC_CLK_TCK)); }
225 #else
gclock()226 inline Clock gclock() { return Clock(clock()*10/CLK_TCK); }
227 #endif
228 
229 int str2mon(const char* ptr) __attribute__ ((const));
230 int tzoffset();
231 
232 //  In differ to ANSI C strftime() the function strftimei() supports reloading of field names (loaded from language file).
233 //  Return NULL if output truncated, otherwize return s.
234 char* strftimei(char* s, size_t maxsize, const char* fmt, const struct tm* t); // __attribute__ ((format (strftime, 3, 0)));
235 
236 FTime    TimeToFTime(time32_t __time) __attribute__ ((const));
237 time32_t FTimeToTime(FTime* __ftime, struct tm* __tm=NULL);
238 
239 time32_t FidoTimeToUnix(char* __fidotime);
240 
241 char* FTimeToStr(char* buf, FTime &t);
242 char* TimeToStr(char* buf, time32_t t);
243 
244 
245 //  ------------------------------------------------------------------
246 
247 long YMD2JDN(unsigned yr, unsigned mo, unsigned day) __attribute__ ((const));
248 void JDN2YMD(long scalar, unsigned *yr, unsigned *mo, unsigned *day);
249 
250 
251 //  ------------------------------------------------------------------
252 
253 #endif
254 
255 //  ------------------------------------------------------------------
256