forked from adesutherland/CMS-370-BREXX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
211 lines (188 loc) · 5.05 KB
/
date.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* $Id: date.c,v 1.7 2008/07/15 07:40:54 bnv Exp $
* $Log: date.c,v $
* Revision 1.7 2008/07/15 07:40:54 bnv
* #include changed from <> to ""
*
* Revision 1.6 2004/08/16 15:25:20 bnv
* float to double
*
* Revision 1.5 2002/06/11 12:37:15 bnv
* Added: CDECL
*
* Revision 1.4 2001/06/25 18:49:48 bnv
* Header changed to Id
*
* Revision 1.3 2000/10/09 07:17:19 bnv
* Corrected the year format
*
* Revision 1.2 1999/11/26 09:53:28 bnv
* Added Windows CE support
*
* Revision 1.1 1998/07/02 17:17:00 bnv
* Initial revision
*
*/
#ifndef WCE
# include <time.h>
#endif
#ifdef MSDOS
# include <dos.h>
#endif
#include <cmssys.h>
#include "lerror.h"
#include "lstring.h"
#ifdef WCE
static BYTE month_days[12] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
/* ---- day of year ---- */
static int
day_of_year(LPSYSTEMTIME time)
{
int m, d = 0;
/* fix the february */
if ((time->wYear%4==0) && (time->wYear%400 != 0))
month_days[1] = 29;
else
month_days[1] = 28;
for (m=1; m<time->wMonth; m++)
d += month_days[m-1];
return d+time->wDay;
} /* day_of_year */
#endif
/* Keep Global as constant */
static const TCHAR *WeekDays[] = {
TEXT("Sunday"), TEXT("Monday"), TEXT("Tuesday"), TEXT("Wednesday"),
TEXT("Thursday"), TEXT("Friday"), TEXT("Saturday")};
/* Keep Global as constant */
static const TCHAR *months[] = {
TEXT("January"), TEXT("February"), TEXT("March"),
TEXT("April"), TEXT("May"), TEXT("June"),
TEXT("July"), TEXT("August"), TEXT("September"),
TEXT("October"), TEXT("November"), TEXT("December")};
/* ----------------- Ldate ------------------ */
void __CDECL
Ldate(const PLstr datestr, char option) {
static TCHAR *fmt = TEXT("%02d/%02d/%02d");
static TCHAR *iso = TEXT("%4d%02d%02d");
long length;
const TCHAR *chptr;
Context *context = (Context *) CMSGetPG();
#ifndef WCE
time_t now;
struct tm *tmdata;
#else
TCHAR buf[30];
SYSTEMTIME time;
#endif
option = (context->lstring_l2u)[(byte) option];
Lfx(datestr, 30);
LZEROSTR(*datestr);
#ifndef WCE
now = time(NULL);
tmdata = localtime(&now);
#else
GetLocalTime(&time);
#endif
switch (option) {
case 'C':
#ifndef WCE
length = tmdata->tm_yday + 1 +
(long) (((double) tmdata->tm_year - 1) * 365.25) + 365;
sprintf(LSTR(*datestr), "%ld", length);
#else
length = day_of_year(&time) +
(long)((time.wYear%100-1)*365.25) + 365;
swprintf(buf,TEXT("%ld"),length) ;
#endif
break;
case 'D':
#ifndef WCE
sprintf(LSTR(*datestr), "%d", tmdata->tm_yday + 1);
#else
swprintf(buf, TEXT("%d"), day_of_year(&time)) ;
#endif
break;
case 'E':
#ifndef WCE
sprintf(LSTR(*datestr), fmt, tmdata->tm_mday,
tmdata->tm_mon + 1, tmdata->tm_year % 100);
#else
swprintf(buf, fmt, time.wDay,
time.wMonth, time.wYear%100) ;
#endif
break;
case 'J':
#ifndef WCE
sprintf(LSTR(*datestr), "%02d%03d",
tmdata->tm_year % 100, tmdata->tm_yday + 1);
#else
swprintf(buf,TEXT("%02d%03d"),
time.wYear%100, day_of_year(&time));
#endif
break;
case 'M':
#ifndef WCE
STRCPY(LSTR(*datestr), months[tmdata->tm_mon]);
#else
wcscpy(buf,months[time.wMonth-1]);
#endif
break;
case 'N':
#ifndef WCE
chptr = months[tmdata->tm_mon];
sprintf(LSTR(*datestr), "%d %c%c%c %4d",
tmdata->tm_mday, chptr[0], chptr[1],
chptr[2], tmdata->tm_year + 1900);
#else
chptr = months[time.wMonth-1] ;
swprintf(buf,TEXT("%d %c%c%c %4d"),
time.wDay, chptr[0], chptr[1],
chptr[2], time.wYear) ;
#endif
break;
case 'O':
#ifndef WCE
sprintf(LSTR(*datestr), fmt, tmdata->tm_year % 100,
tmdata->tm_mon + 1, tmdata->tm_mday);
#else
swprintf(buf, fmt, time.wYear%100,
time.wMonth, time.wDay);
#endif
break;
case 'S':
#ifndef WCE
sprintf(LSTR(*datestr), iso, tmdata->tm_year + 1900,
tmdata->tm_mon + 1, tmdata->tm_mday);
#else
swprintf(buf, iso, time.wYear,
time.wMonth, time.wDay) ;
#endif
break;
case 'U':
#ifndef WCE
sprintf(LSTR(*datestr), fmt, tmdata->tm_mon + 1,
tmdata->tm_mday, tmdata->tm_year % 100);
#else
swprintf(buf, fmt, time.wMonth,
time.wDay, time.wYear%100 ) ;
#endif
break;
case 'W':
#ifndef WCE
STRCPY(LSTR(*datestr), WeekDays[tmdata->tm_wday]);
#else
wcscpy(buf, WeekDays[time.wDayOfWeek]);
#endif
break;
default:
(context->lstring_Lerror)(ERR_INCORRECT_CALL, 0);
}
#ifndef WCE
LLEN(*datestr) = STRLEN(LSTR(*datestr));
#else
wcstombs(LSTR(*datestr),buf,LLEN(*datestr)=wcslen(buf));
#endif
} /* Ldate */