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 if (snprintf (timebuf,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
100 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday,
101 tp->tm_hour, tp->tm_min, tp->tm_sec)
130 for (s = atime, i = 0; i < 8; i++, s++)
135 for (s++, i = 9; i < 15; i++, s++)
153 for (s =
string, i = 0; i < 8; i++, s++)
158 for (s++, i = 9; i < 15; i++, s++)
161 if (!(!*s || (isascii (*s) && isspace (*s)) || *s ==
':' || *s ==
','))
179 for (s =
string, i = 0; i < 4; i++, s++)
197 if (!*s || *s ==
',')
210 if (!*s || *s ==
',')
221 if (!*s || *s ==
',')
232 if (!*s || *s ==
',' ||
spacep (s))
254 memcpy (atime,
string, 15);
260 atime[0] =
string[0];
261 atime[1] =
string[1];
262 atime[2] =
string[2];
263 atime[3] =
string[3];
264 atime[4] =
string[5];
265 atime[5] =
string[6];
266 atime[6] =
string[8];
267 atime[7] =
string[9];
269 if (!
spacep (
string + 10))
273 atime[9] =
string[11];
274 atime[10] =
string[12];
275 if (
string[13] !=
':')
277 atime[11] =
string[14];
278 atime[12] =
string[15];
279 if (
string[16] !=
':')
281 atime[13] =
string[17];
282 atime[14] =
string[18];
296 return s ? 366 : 365;
338 jd = 365L * year + 31 * (month - 1) + day +
JD_DIFF;
342 jd -= (4 * month + 23) / 10;
344 jd += year / 4 - ((year / 100 + 1) * 3) / 4;
354 jd2date (
unsigned long jd,
int *year,
int *month,
int *day)
361 if (jd < 1721425 || jd > 2843085)
370 m = (delta / 31) + 1;
397 return (jd -
date2jd (y, 1, 1)) + 1;
406 int year, month, day, hour, minute, sec, ndays;
412 if (nseconds < 0 || nseconds >= (0x7fffffff - 61))
415 year =
atoi_4 (atime + 0);
416 month =
atoi_2 (atime + 4);
418 hour =
atoi_2 (atime + 9);
419 minute =
atoi_2 (atime + 11);
420 sec =
atoi_2 (atime + 13);
423 if (year < 1582 || (year == 1582 && month < 10)
424 || (year == 1582 && month == 10 && day < 15))
435 jd =
date2jd (year, month, day) + ndays;
436 jd2date (jd, &year, &month, &day);
438 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
441 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
442 day, hour, minute, sec)
453 int year, month, day, hour, minute, sec;
459 if (ndays < 0 || ndays >= 9999 * 366)
462 year =
atoi_4 (atime + 0);
463 month =
atoi_2 (atime + 4);
465 hour =
atoi_2 (atime + 9);
466 minute =
atoi_2 (atime + 11);
467 sec =
atoi_2 (atime + 13);
470 if (year < 1582 || (year == 1582 && month < 10)
471 || (year == 1582 && month == 10 && day < 15))
474 jd =
date2jd (year, month, day) + ndays;
475 jd2date (jd, &year, &month, &day);
477 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
480 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
481 day, hour, minute, sec)
491 int year, month, day, hour, minute, sec;
497 if (nyears < 0 || nyears >= 9999)
500 year =
atoi_4 (atime + 0);
501 month =
atoi_2 (atime + 4);
503 hour =
atoi_2 (atime + 9);
504 minute =
atoi_2 (atime + 11);
505 sec =
atoi_2 (atime + 13);
508 if (year < 1582 || (year == 1582 && month < 10)
509 || (year == 1582 && month == 10 && day < 15))
512 jd =
date2jd (year + nyears, month, day);
513 jd2date (jd, &year, &month, &day);
515 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
518 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
519 day, hour, minute, sec)
551 retc->
x.
str_val = g_strdup (timebuf);
552 retc->
size = strlen (timebuf);
655 retc->
x.
str_val = g_strdup (timebuf);
656 retc->
size = strlen (timebuf);
684 strcpy (helpbuf,
"[none]");
686 snprintf (helpbuf,
sizeof helpbuf,
"%.4s-%.2s-%.2s %.2s:%.2s:%.2s",
string,
687 string + 4,
string + 6,
string + 9,
string + 11,
string + 13);
689 retc->
x.
str_val = g_strdup (helpbuf);
690 retc->
size = strlen (helpbuf);
731 int nyears, ndays, nseconds;
755 retc->
x.
str_val = g_strdup (timebuf);
756 retc->
size = strlen (timebuf);
static unsigned long date2jd(int year, int month, int day)
static int check_isotime(const my_isotime_t atime)
static int string2isotime(my_isotime_t atime, const char *string)
char my_isotime_t[ISOTIME_SIZE]
static int isotime_p(const char *string)
tree_cell * nasl_isotime_print(lex_ctxt *lexic)
Convert an SIO time string into a better readable string.
static int add_days_to_isotime(my_isotime_t atime, int ndays)
static int days_per_month(int y, int m)
tree_cell * alloc_typed_cell(int typ)
tree_cell * nasl_isotime_add(lex_ctxt *lexic)
Add days or seconds to an ISO time string.
int get_var_size_by_num(lex_ctxt *, int)
static int add_seconds_to_isotime(my_isotime_t atime, int nseconds)
static int isotime_human_p(const char *string)
static int days_per_year(int y)
char * get_str_var_by_num(lex_ctxt *, int)
static void epoch2isotime(my_isotime_t timebuf, time_t atime)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
static void get_current_isotime(my_isotime_t timebuf)
Protos and data structures for ISOTIME functions used by NASL scripts.
static int add_years_to_isotime(my_isotime_t atime, int nyears)
static int jd2date(unsigned long jd, int *year, int *month, int *day)
tree_cell * nasl_isotime_is_valid(lex_ctxt *lexic)
Check whether an ISO time string is valid.
tree_cell * nasl_isotime_scan(lex_ctxt *lexic)
Convert a string into an ISO time string.
tree_cell * nasl_isotime_now(lex_ctxt *lexic)
Return the current time in ISO format.
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.