11static int scan_absolute(
DateTime *,
const char *);
12static int more(
const char **);
13static int minus_sign(
const char **);
14static int is_bc(
const char **);
15static int is_relative(
const char *);
16static int relative_term(
const char **,
double *,
int *,
int *,
int *);
17static int scan_tz(
const char *,
int *);
18static int get_word(
const char **,
char *);
19static char lowercase(
char);
20static int which_month(
const char *,
int *);
21static int scan_relative(
DateTime *,
const char *);
22static int is_space(
char);
23static int is_digit(
char);
24static void skip_space(
const char **);
25static int get_int(
const char **,
int *,
int *);
26static int get_double(
const char **,
double *,
int *,
int *);
42 if (is_relative(buf)) {
43 if (scan_relative(dt, buf))
47 if (scan_absolute(dt, buf))
52static const char *month_names[] = {
"jan",
"feb",
"mar",
"apr",
"may",
"jun",
53 "jul",
"aug",
"sep",
"oct",
"nov",
"dec"};
55static int scan_absolute(
DateTime *dt,
const char *buf)
64 int year, month, day = 0, hour, minute;
72 if (!get_int(&p, &n, &
ndigits)) {
73 if (!get_word(&p,
word))
75 if (!which_month(
word, &month))
77 if (!get_int(&p, &year, &
ndigits))
86 if (
bc || !get_word(&p,
word)) {
93 if (!which_month(
word, &month))
95 if (!get_int(&p, &year, &
ndigits))
101 if (!get_int(&p, &hour, &
ndigits))
107 if (!get_int(&p, &minute, &
ndigits))
115 if (!get_double(&p, &second, &
ndigits, &fracsec))
122 if (!get_word(&p,
word))
124 if (!scan_tz(
word, &tz))
169static int scan_relative(
DateTime *dt,
const char *buf)
176 int year = 0, month = 0, day = 0, hour = 0, minute = 0, fracsec = 0;
181 neg = minus_sign(&p);
222 for (pos = from; pos <= to; pos++) {
256static int is_space(
char c)
258 return (c ==
' ' || c ==
'\t' || c ==
'\n');
261static int is_digit(
char c)
263 return (c >=
'0' && c <=
'9');
266static void skip_space(
const char **s)
268 while (is_space(**s))
272static int get_int(
const char **s,
int *n,
int *
ndigits)
279 for (*
ndigits = 0; is_digit(*p); (*ndigits)++) {
289static int get_double(
const char **s,
double *x,
303 for (*
ndigits = 0; is_digit(*p); (*ndigits)++)
307 while (is_digit(*p)) {
313 if (
sscanf(buf,
"%lf", x) != 1)
341static int get_word(
const char **s,
char *
word)
348 for (
any = 0; *p && !is_space(*p);
any = 1)
349 *
word++ = lowercase(*p++);
355static char lowercase(
char c)
357 if (c >=
'A' && c <=
'Z')
362static int which_month(
const char *
name,
int *n)
366 for (i = 0; i < 12; i++)
374static int is_bc(
const char **s)
380 if (!get_word(&p,
word))
388static int scan_tz(
const char *
word,
int *tz)
394 else if (
word[0] ==
'-')
399 if (!is_digit(
word[1]))
401 if (!is_digit(
word[2]))
403 if (!is_digit(
word[3]))
405 if (!is_digit(
word[4]))
408 *tz = (
word[1] -
'0') * 600 + (
word[2] -
'0') * 60 + (
word[3] -
'0') * 10 +
419static int relative_term(
const char **s,
double *x,
int *
ndigits,
int *
ndecimal,
450static int minus_sign(
const char **s)
460static int is_relative(
const char *buf)
467 (
void)minus_sign(&p);
468 return relative_term(&p, &x, &n, &n, &n) != 0;
471static int more(
const char **s)
#define DATETIME_ABSOLUTE
#define DATETIME_RELATIVE
int datetime_error(int code, char *msg)
record 'code' and 'msg' as error code/msg (in static variables) code==0 will clear the error (ie set ...
void datetime_set_negative(DateTime *dt)
Makes the DateTime negative. (B.C. for ABSOLUTE DateTimes)
int datetime_set_day(DateTime *dt, int day)
if dt.mode = ABSOLUTE, then the dt.year, dt.month:
int datetime_set_month(DateTime *dt, int month)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
int datetime_set_type(DateTime *dt, int mode, int from, int to, int fracsec)
int datetime_set_timezone(DateTime *dt, int minutes)
returns 0 on success
int datetime_set_hour(DateTime *dt, int hour)
returns 0 on success or negative value on error
int datetime_set_year(DateTime *dt, int year)
if dt.mode = ABSOLUTE, this also sets dt.day = 0
int datetime_set_second(DateTime *dt, double second)
returns 0 on success or negative value on error
int datetime_set_minute(DateTime *dt, int minute)
returns 0 on success or negative value on error
int datetime_scan(DateTime *dt, const char *buf)
Convert the ascii string into a DateTime. This determines the mode/from/to based on the string,...