File: iso8601.c

package info (click to toggle)
msort 8.53-2.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,360 kB
  • sloc: sh: 10,138; ansic: 10,031; makefile: 51
file content (259 lines) | stat: -rw-r--r-- 6,738 bytes parent folder | download | duplicates (4)
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
/* Time-stamp: <2008-10-19 00:14:01 poser> */

/*
 * Copyright (C) 2007, 2008 William J. Poser.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * or go to the web page:  http://www.gnu.org/licenses/gpl.txt.
 */

/* These are functions for interpreting ISO8601 time/date stamps */

#include "config.h"
#include "compdefs.h"
#include <stdlib.h>
#ifdef HAVE_UNINUM_UNICODE_H
#include <uninum/unicode.h>
#else
#include "unicode.h"
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef LOCALE_GETTEXT
#include <libintl.h>
#define _(x) gettext(x)
#else
#define _(x) (x)
#endif
#include <time.h>
#include <wchar.h>
#include <errno.h>
#include "retcodes.h"

#ifdef HAVE_LONGDOUBLE
#define DOUBLE (long double)
#else
#define DOUBLE double
#endif


/*
 * Return the difference between UTC and the local time zone, in seconds.
 * Add this value to local time to obtain UTC.
 *
 * We call localtime() rather than tzset because localtime is guaranteed by
 * POSIX to set timezone while tzset is not.
 */

static long
TimezoneOffset(void) {
  time_t lt;
  /* timezone is defined in time.h */

  lt = time(NULL);
  (void) localtime(&lt);
  return((long) timezone);
}


/* 
 * Given a time as a string in one of the following formats, return the value in seconds:
 * 	hh
 * 	hhmm
 *	hh:mm
 * If the expression is ill-formed, set the error flag.
 */

static long BrokenTimeToSeconds (char *s, short *eflag) {
  int hours = 0;
  int minutes = 0;
  int FieldsFound;
  short status = 1;		/* No valid return value yet */


  FieldsFound = sscanf((char *)s,"%2u:%2u",&hours,&minutes);
  if(FieldsFound == 2) {
    status = 0;
    goto end;
  }

  FieldsFound = sscanf((char *)s,"%2u%2u",&hours,&minutes);
  if(FieldsFound == 2) {
    status = 0;
    goto end;
  }
  if(FieldsFound == 1) status = 0;

end:
  if (minutes > 59) status = 1;
  if (hours > 24) status = 1;
  *eflag = status;
  if (status == 0)  return (((hours * 60) + minutes) * 60);
  else return 0;
}

/* Extract the time zone info if any and return the number of seconds
 * that must be added to normalize the time to UTC. 
 * The time zone marker (Z,z,-,+) is ovewritten with a null,
 * modifying the original string.
 */

long ExtractTimeZone(UTF8 *s, short *eflag) {
  char *sep;
  long Offset;
  short err = 0;

  /* Z/z means the value is already UTC  */
  if ((sep = strchr((char *) s,'Z'))) {
    *sep = '\0';
    return 0;
  }

  if ((sep = strchr((char *) s,'z'))) {
    *sep = '\0';
    return 0;
  }

  /* + means subtract the following offset to get UTC  */
  if ((sep = strchr((char *) s,'+'))) {
    Offset = BrokenTimeToSeconds(sep+1,&err);
    *eflag = err;
    *sep = '\0';
    return (-Offset);
  }
  
  /* - means add the following offset to get UTC  */
  if ((sep = strchr((char *) s,'-'))) {
    Offset = BrokenTimeToSeconds(sep+1,&err);
    *eflag = err;
    *sep = '\0';
    return (Offset);
  }

  /*
   * If there is no time zone indicator, we default to the time zone of the
   * environment of this process.
   */

  return(TimezoneOffset());
}

/*
 * Remove hyphen-minus and colon from time-date strings
 * Return the adjusted location of the T.
 */

char *
StripTimeDateSeparators(char *s, char *tloc) {
  char *src;
  char *tgt;
  char c;
  int cnt = 0;			/* Number of hyphens stripped */

  src = tgt = s;
  while (src < tloc) {
    c = *src++;
    if (c == '-') {cnt++;continue;}
    *tgt++ = c;
  }
  while ( (c = *src++) != '\0') {
    if (c == ':') continue;
    *tgt++ = c;
  }
  *tgt = '\0';
  return(tloc-cnt);
}

/* 
 * This produces a value that is suitable for ordering dates and times.
 * It is NOT accurate for computing differences between dates and times
 * because the conversion of dates to days is not exact. For simplicity's
 * sake it treats years as containing 366 days and months as all containing
 * 31 days.
 *
 * This handles both basic and extended formats. It also permits an optional leading sign,
 * restricts the year to four digits.
 */
#define DTITYPE unsigned long
#define SECONDSINDAY (60*60*24)
int
GetISO8601Key(wchar_t *field, DOUBLE *key)
{
  unsigned int year = 0;
  unsigned int month =0;
  unsigned int day = 0;
  unsigned int hour = 0;
  unsigned int minute = 0;
  unsigned int second = 0;

  long TZOffset;		/* Number of seconds to add to normalize to UTC */
  DTITYPE Days;
  DTITYPE DaySeconds;
  int f1, f2;
  char *cfield;
  char *nscfield;		/* cfield without optional leading sign */
  char * Tloc;
  short err;
  int YearSign = 1;

  extern UTF8 * ws2u8(wchar_t *);

  cfield = (char *) ws2u8(field);
  Tloc = strchr(cfield,'T');
  if(!Tloc) {free(cfield);return(ERROR);}
  Tloc = StripTimeDateSeparators(cfield,Tloc);
  err=0;
  TZOffset = ExtractTimeZone(((UTF8 *)Tloc)+1,&err);
  if(err) return(ERROR);

  if(cfield[0] == '-') {
    YearSign = -1;
    nscfield = cfield + 1;
  }
  else if(cfield[0] == '+') nscfield = cfield + 1;
  else nscfield = cfield;

  f1=sscanf(nscfield,"%4u%2u%2uT%2u%2u.%2u", &year,&month,&day,&hour,&minute,&second);
  if(f1 != 6) {
    f2=sscanf(nscfield,"%4u%2u%2uT%2u%2u", &year,&month,&day,&hour,&minute);
    if(f2 != 5) {free(cfield);return(ERROR);}
  }
  free(cfield);
  if(month < 0 || day < 0) return (ERROR);
  if(month > 12) return (ERROR);
  if(day > 31) return (ERROR);
  if(hour < 0 || minute < 0 || second < 0) return (ERROR);
  if(hour > 24) return(ERROR);
  if(minute > 60 || second > 60) return (ERROR);

#ifdef UNNECESSARY
  /* 
   * If making actual date calculations, the following is necessary as per the spec.
   * That is, 2007-04-05T24:00 is the same instant as 2007-04-06T00:00
   * For our purposes it is unnecessary as it has no effect on ordering,
   * so we'd might as well be slightly more efficient and omit it.
   */
  if ((hour == 24) && (minute == 0)) {
    hour = 0;
    day += 1;
  }
#endif

  Days = (DTITYPE) ( (366 * (DTITYPE) year) + (31 * month) + day);
  DaySeconds = (DTITYPE) (((((DTITYPE) hour * 60) + (DTITYPE) minute) * 60) + (DTITYPE) second);
  *key = (DOUBLE) ((DOUBLE) YearSign * ( ( (DOUBLE)Days * (DOUBLE)SECONDSINDAY) + (DOUBLE)DaySeconds + (DOUBLE)TZOffset));
  return(SUCCESS);
}