File: date.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (298 lines) | stat: -rw-r--r-- 7,139 bytes parent folder | download | duplicates (3)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*	Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.

This file is part of Oleo, the GNU Spreadsheet.

Oleo is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Oleo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Oleo; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */



#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "funcdef.h"
#include "sysdef.h"

#include "global.h"
#include "cell.h"
#include "eval.h"
#include "errors.h"

struct value
  {
    int type;
    union vals x;
  };

#define Float	x.c_d
#define String	x.c_s
#define Int	x.c_l
#define Value	x.c_i
#define Rng	x.c_r



struct timeb;
extern time_t get_date (char *, struct timeb *);
extern time_t posixtime (char *);



/* These functions simply provide convenient syntax for expressing intervals 
 * of time.
 */

static long
dt_hms_to_time (long h, long m, long s)
{
  return ((h * 60) + m) * 60 + s;
}

static long
dt_dhms_to_time (long d, long h, long m, long s)
{
  return ((((d * 24) + h) * 60) + m) * 60 + s;
}

static long
dt_time_to_d (long t)
{
  return t / (60 * 60 * 24);
}

static long
dt_time_to_h (long t)
{
  return (t / (60 * 60)) % 24;
}

static long
dt_time_to_m (long t)
{
  return (t / 60) % 60;
}

static long
dt_time_to_s (long t)
{
  return t % 60;
}



/* These relate time values to calendar dates using localtime, gmtime, 
 * strftime, mktime, etc.
 */

/* mktime: */


static long
dt_ymd_dst (long y, long mo, long d, long dst)
{
  struct tm tm;
  tm.tm_year = y - 1900;
  tm.tm_mon = mo;
  tm.tm_mday = d;
  tm.tm_hour = 0;
  tm.tm_min = 0;
  tm.tm_sec = 0;
  tm.tm_isdst = dst;
  return mktime (&tm);
}

static long
dt_ymd (long y, long mo, long d)
{
  return dt_ymd_dst (y, mo, d, -1);
}




#define TM_ACCESS(FN,FIELD,TMFN)			\
static long						\
FN (clk)						\
     long clk;					\
{							\
  time_t t = (time_t)clk;				\
  struct tm * tm = TMFN(&t);				\
  return (long)tm->FIELD;				\
}

#define TM_ACCESS_LOCAL(FN,FIELD)  TM_ACCESS(FN, FIELD, localtime)

 TM_ACCESS_LOCAL (dt_local_year, tm_year + 1900)
 TM_ACCESS_LOCAL (dt_local_month, tm_mon)
 TM_ACCESS_LOCAL (dt_local_date, tm_mday)
 TM_ACCESS_LOCAL (dt_local_hour, tm_hour)
 TM_ACCESS_LOCAL (dt_local_min, tm_min)
 TM_ACCESS_LOCAL (dt_local_sec, tm_sec)
 TM_ACCESS_LOCAL (dt_local_isdst, tm_isdst)
 TM_ACCESS_LOCAL (dt_local_yday, tm_yday)
 TM_ACCESS_LOCAL (dt_local_wday, tm_wday)


#define TM_ACCESS_GMT(FN,FIELD)  TM_ACCESS(FN, FIELD, gmtime)

 TM_ACCESS_GMT (dt_gmt_year, tm_year + 1900)
 TM_ACCESS_GMT (dt_gmt_month, tm_mon)
 TM_ACCESS_GMT (dt_gmt_date, tm_mday)
 TM_ACCESS_GMT (dt_gmt_hour, tm_hour)
 TM_ACCESS_GMT (dt_gmt_min, tm_min)
 TM_ACCESS_GMT (dt_gmt_sec, tm_sec)
 TM_ACCESS_GMT (dt_gmt_isdst, tm_isdst)
 TM_ACCESS_GMT (dt_gmt_yday, tm_yday)
 TM_ACCESS_GMT (dt_gmt_wday, tm_wday)



static char *
dt_strftime (char * format, long clk)
{
  int len_used = 0;
  int len = 32;
  char * buf = (char *)ck_malloc (len);
  time_t ck = (time_t)clk;
  struct tm * tm = localtime (&ck);
  while (!len_used)
    {
      len *= 2;
      buf = (char *)ck_remalloc (buf, len);
      len_used = strftime (buf, len - 1, format, tm);
    }
  if (len - 1 > len_used)
    buf = (char *)ck_remalloc (buf, len_used + 1);
  buf [len_used] = 0;
  return buf;
}



static long
dt_get_date (char * date)
{
  return get_date (date, NULL);
}

static long
dt_posix_date (char * date)
{
  return posixtime (date);
}



static void
l_l (long int (*fn) (/* ??? */), struct value *p)
{
  p[0].Int = fn (p->Int);
}

static void
l_lll (long int (*fn) (/* ??? */), struct value *p)
{
  p[0].Int = fn (p[0].Int, p[1].Int, p[2].Int);
}

static void
l_llll (long int (*fn) (/* ??? */), struct value *p)
{
  p[0].Int = fn (p[0].Int, p[1].Int, p[2].Int, p[3].Int);
}

static void
l_s (long int (*fn) (/* ??? */), struct value *p)
{
  p[0].Int = fn (p[0].String);
  p[0].type = TYP_INT;
}



#define CALLER(FN,CALL,VIA) static void FN (p) struct value * p; { VIA(CALL,p); }

 CALLER(do_hms_to_time, dt_hms_to_time, l_lll)
 CALLER(do_dhms_to_time, dt_dhms_to_time, l_llll)
 CALLER(do_time_to_d, dt_time_to_d, l_l)
 CALLER(do_time_to_h, dt_time_to_h, l_l)
 CALLER(do_time_to_m, dt_time_to_m, l_l)
 CALLER(do_time_to_s, dt_time_to_s, l_l)
 CALLER(do_ymd_dst, dt_ymd_dst, l_llll)
 CALLER(do_ymd, dt_ymd, l_lll)
 CALLER(do_local_year, dt_local_year, l_l)
 CALLER(do_local_month, dt_local_month, l_l)
 CALLER(do_local_date, dt_local_date, l_l)
 CALLER(do_local_hour, dt_local_hour, l_l)
 CALLER(do_local_min, dt_local_min, l_l)
 CALLER(do_local_sec, dt_local_sec, l_l)
 CALLER(do_local_isdst, dt_local_isdst, l_l)
 CALLER(do_local_yday, dt_local_yday, l_l)
 CALLER(do_local_wday, dt_local_wday, l_l)
 CALLER(do_gmt_year, dt_gmt_year, l_l)
 CALLER(do_gmt_month, dt_gmt_month, l_l)
 CALLER(do_gmt_date, dt_gmt_date, l_l)
 CALLER(do_gmt_hour, dt_gmt_hour, l_l)
 CALLER(do_gmt_min, dt_gmt_min, l_l)
 CALLER(do_gmt_sec, dt_gmt_sec, l_l)
 CALLER(do_gmt_isdst, dt_gmt_isdst, l_l)
 CALLER(do_gmt_yday, dt_gmt_yday, l_l)
 CALLER(do_gmt_wday, dt_gmt_wday, l_l)
 CALLER(do_get_date, dt_get_date, l_s)
 CALLER(do_posix_date, dt_posix_date, l_s)



void
do_strftime (struct value *p)
{
  p[0].String = dt_strftime (p[0].String, p[1].Int);
}




struct function date_funs[] =
{
  {C_FN3, X_A3, "III", do_hms_to_time, "hms_to_time"},
  {C_FN4, X_A4, "IIII", do_dhms_to_time, "dhms_to_time"},
  {C_FN1, X_A1, "I", do_time_to_d, "time_to_d"},
  {C_FN1, X_A1, "I", do_time_to_h, "time_to_h"},
  {C_FN1, X_A1, "I", do_time_to_m, "time_to_m"},
  {C_FN1, X_A1, "I", do_time_to_s, "time_to_s"},
  {C_FN3, X_A3, "III", do_ymd, "ymd"},
  {C_FN4, X_A4, "IIII", do_ymd_dst, "ymd_dst"},
  {C_FN1, X_A1, "I", do_local_year, "local_year"},
  {C_FN1, X_A1, "I", do_local_month, "local_month"},
  {C_FN1, X_A1, "I", do_local_date, "local_date"},
  {C_FN1, X_A1, "I", do_local_hour, "local_hour"},
  {C_FN1, X_A1, "I", do_local_min, "local_min"},
  {C_FN1, X_A1, "I", do_local_sec, "local_sec"},
  {C_FN1, X_A1, "I", do_local_isdst, "local_isdst"},
  {C_FN1, X_A1, "I", do_local_yday, "local_yday"},
  {C_FN1, X_A1, "I", do_local_wday, "local_wday"},
  {C_FN1, X_A1, "I", do_gmt_year, "gmt_year"},
  {C_FN1, X_A1, "I", do_gmt_month, "gmt_month"},
  {C_FN1, X_A1, "I", do_gmt_date, "gmt_date"},
  {C_FN1, X_A1, "I", do_gmt_hour, "gmt_hour"},
  {C_FN1, X_A1, "I", do_gmt_min, "gmt_min"},
  {C_FN1, X_A1, "I", do_gmt_sec, "gmt_sec"},
  {C_FN1, X_A1, "I", do_gmt_isdst, "gmt_isdst"},
  {C_FN1, X_A1, "I", do_gmt_yday, "gmt_yday"},
  {C_FN1, X_A1, "I", do_gmt_wday, "gmt_wday"},
  {C_FN1, X_A1, "S", do_get_date, "get_date"},
  {C_FN1, X_A1, "S", do_posix_date, "posix_date"},
  {C_FN2, X_A2, "SI", do_strftime, "strftime"},
  {0, 0, 0, 0}
};