59 #include <glib/gstdio.h>
67 #define DIM(v) (sizeof (v) / sizeof ((v)[0]))
68 #define DIMof(type, member) DIM (((type *) 0)->member)
73 #define ISOTIME_SIZE 19
77 #define JD_DIFF 1721060L
80 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
81 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
84 #define atoi_1(p) (*(p) - '0')
85 #define atoi_2(p) ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))
86 #define atoi_4(p) ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))
92 if (atime == (time_t) (-1))
99 snprintf (timebuf,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
100 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour,
101 tp->tm_min, tp->tm_sec);
125 for (s = atime, i = 0; i < 8; i++, s++)
130 for (s++, i = 9; i < 15; i++, s++)
148 for (s =
string, i = 0; i < 8; i++, s++)
153 for (s++, i = 9; i < 15; i++, s++)
156 if (!(!*s || (isascii (*s) && isspace (*s)) || *s ==
':' || *s ==
','))
174 for (s =
string, i = 0; i < 4; i++, s++)
192 if (!*s || *s ==
',')
205 if (!*s || *s ==
',')
216 if (!*s || *s ==
',')
227 if (!*s || *s ==
',' ||
spacep (s))
249 memcpy (atime,
string, 15);
255 atime[0] =
string[0];
256 atime[1] =
string[1];
257 atime[2] =
string[2];
258 atime[3] =
string[3];
259 atime[4] =
string[5];
260 atime[5] =
string[6];
261 atime[6] =
string[8];
262 atime[7] =
string[9];
264 if (!
spacep (
string + 10))
268 atime[9] =
string[11];
269 atime[10] =
string[12];
270 if (
string[13] !=
':')
272 atime[11] =
string[14];
273 atime[12] =
string[15];
274 if (
string[16] !=
':')
276 atime[13] =
string[17];
277 atime[14] =
string[18];
291 return s ? 366 : 365;
333 jd = 365L * year + 31 * (month - 1) + day +
JD_DIFF;
337 jd -= (4 * month + 23) / 10;
339 jd += year / 4 - ((year / 100 + 1) * 3) / 4;
349 jd2date (
unsigned long jd,
int *year,
int *month,
int *day)
356 if (jd < 1721425 || jd > 2843085)
365 m = (delta / 31) + 1;
392 return (jd -
date2jd (y, 1, 1)) + 1;
401 int year, month, day, hour, minute, sec, ndays;
407 if (nseconds < 0 || nseconds >= (0x7fffffff - 61))
410 year =
atoi_4 (atime + 0);
411 month =
atoi_2 (atime + 4);
413 hour =
atoi_2 (atime + 9);
414 minute =
atoi_2 (atime + 11);
415 sec =
atoi_2 (atime + 13);
418 if (year < 1582 || (year == 1582 && month < 10)
419 || (year == 1582 && month == 10 && day < 15))
430 jd =
date2jd (year, month, day) + ndays;
431 jd2date (jd, &year, &month, &day);
433 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
436 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month, day,
445 int year, month, day, hour, minute, sec;
451 if (ndays < 0 || ndays >= 9999 * 366)
454 year =
atoi_4 (atime + 0);
455 month =
atoi_2 (atime + 4);
457 hour =
atoi_2 (atime + 9);
458 minute =
atoi_2 (atime + 11);
459 sec =
atoi_2 (atime + 13);
462 if (year < 1582 || (year == 1582 && month < 10)
463 || (year == 1582 && month == 10 && day < 15))
466 jd =
date2jd (year, month, day) + ndays;
467 jd2date (jd, &year, &month, &day);
469 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
472 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month, day,
481 int year, month, day, hour, minute, sec;
487 if (nyears < 0 || nyears >= 9999)
490 year =
atoi_4 (atime + 0);
491 month =
atoi_2 (atime + 4);
493 hour =
atoi_2 (atime + 9);
494 minute =
atoi_2 (atime + 11);
495 sec =
atoi_2 (atime + 13);
498 if (year < 1582 || (year == 1582 && month < 10)
499 || (year == 1582 && month == 10 && day < 15))
502 jd =
date2jd (year + nyears, month, day);
503 jd2date (jd, &year, &month, &day);
505 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
508 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month, day,
538 retc->
x.
str_val = g_strdup (timebuf);
539 retc->
size = strlen (timebuf);
642 retc->
x.
str_val = g_strdup (timebuf);
643 retc->
size = strlen (timebuf);
671 strcpy (helpbuf,
"[none]");
673 snprintf (helpbuf,
sizeof helpbuf,
"%.4s-%.2s-%.2s %.2s:%.2s:%.2s",
string,
674 string + 4,
string + 6,
string + 9,
string + 11,
string + 13);
676 retc->
x.
str_val = g_strdup (helpbuf);
677 retc->
size = strlen (helpbuf);
718 int nyears, ndays, nseconds;
742 retc->
x.
str_val = g_strdup (timebuf);
743 retc->
size = strlen (timebuf);