44#include <glib/gstdio.h>
55#define G_LOG_DOMAIN "lib nasl"
58#define DIM(v) (sizeof (v) / sizeof ((v)[0]))
59#define DIMof(type, member) DIM (((type *) 0)->member)
64#define ISOTIME_SIZE 16
68#define JD_DIFF 1721060L
71#define spacep(p) (*(p) == ' ' || *(p) == '\t')
72#define digitp(p) (*(p) >= '0' && *(p) <= '9')
75#define atoi_1(p) (*(p) - '0')
76#define atoi_2(p) ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))
77#define atoi_4(p) ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))
83 if (atime == (time_t) (-1))
89 gmtime_r (&atime, &tp);
90 if (snprintf (timebuf,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
91 1900 + tp.tm_year, tp.tm_mon + 1, tp.tm_mday, tp.tm_hour,
121 for (s = atime, i = 0; i < 8; i++, s++)
126 for (s++, i = 9; i < 15; i++, s++)
144 for (s =
string, i = 0; i < 8; i++, s++)
149 for (s++, i = 9; i < 15; i++, s++)
152 if (!(!*s || (isascii (*s) && isspace (*s)) || *s ==
':' || *s ==
','))
170 for (s =
string, i = 0; i < 4; i++, s++)
188 if (!*s || *s ==
',')
201 if (!*s || *s ==
',')
212 if (!*s || *s ==
',')
223 if (!*s || *s ==
',' ||
spacep (s))
245 memcpy (atime,
string, 15);
251 atime[0] =
string[0];
252 atime[1] =
string[1];
253 atime[2] =
string[2];
254 atime[3] =
string[3];
255 atime[4] =
string[5];
256 atime[5] =
string[6];
257 atime[6] =
string[8];
258 atime[7] =
string[9];
260 if (!
spacep (
string + 10))
264 atime[9] =
string[11];
265 atime[10] =
string[12];
266 if (
string[13] !=
':')
274 atime[11] =
string[14];
275 atime[12] =
string[15];
276 if (
string[16] !=
':')
282 atime[13] =
string[17];
283 atime[14] =
string[18];
297 return s ? 366 : 365;
339 jd = 365L * year + 31 * (month - 1) + day +
JD_DIFF;
343 jd -= (4 * month + 23) / 10;
345 jd += year / 4 - ((year / 100 + 1) * 3) / 4;
355jd2date (
unsigned long jd,
int *year,
int *month,
int *day)
362 if (jd < 1721425 || jd > 2843085)
371 m = (delta / 31) + 1;
398 return (jd -
date2jd (y, 1, 1)) + 1;
407 int year, month, day, hour, minute, sec, ndays;
413 if (nseconds < 0 || nseconds >= (0x7fffffff - 61))
416 year =
atoi_4 (atime + 0);
417 month =
atoi_2 (atime + 4);
419 hour =
atoi_2 (atime + 9);
420 minute =
atoi_2 (atime + 11);
421 sec =
atoi_2 (atime + 13);
424 if (year < 1582 || (year == 1582 && month < 10)
425 || (year == 1582 && month == 10 && day < 15))
436 jd =
date2jd (year, month, day) + ndays;
437 jd2date (jd, &year, &month, &day);
439 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
442 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
443 day, hour, minute, sec)
454 int year, month, day, hour, minute, sec;
460 if (ndays < 0 || ndays >= 9999 * 366)
463 year =
atoi_4 (atime + 0);
464 month =
atoi_2 (atime + 4);
466 hour =
atoi_2 (atime + 9);
467 minute =
atoi_2 (atime + 11);
468 sec =
atoi_2 (atime + 13);
471 if (year < 1582 || (year == 1582 && month < 10)
472 || (year == 1582 && month == 10 && day < 15))
475 jd =
date2jd (year, month, day) + ndays;
476 jd2date (jd, &year, &month, &day);
478 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
481 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
482 day, hour, minute, sec)
492 int year, month, day, hour, minute, sec;
498 if (nyears < 0 || nyears >= 9999)
501 year =
atoi_4 (atime + 0);
502 month =
atoi_2 (atime + 4);
504 hour =
atoi_2 (atime + 9);
505 minute =
atoi_2 (atime + 11);
506 sec =
atoi_2 (atime + 13);
509 if (year < 1582 || (year == 1582 && month < 10)
510 || (year == 1582 && month == 10 && day < 15))
513 jd =
date2jd (year + nyears, month, day);
514 jd2date (jd, &year, &month, &day);
516 if (year > 9999 || month > 12 || day > 31 || year < 0 || month < 1 || day < 1)
519 if (snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d", year, month,
520 day, hour, minute, sec)
552 retc->
x.
str_val = g_strdup (timebuf);
553 retc->
size = strlen (timebuf);
656 retc->
x.
str_val = g_strdup (timebuf);
657 retc->
size = strlen (timebuf);
685 strcpy (helpbuf,
"[none]");
687 snprintf (helpbuf,
sizeof helpbuf,
"%.4s-%.2s-%.2s %.2s:%.2s:%.2s",
string,
688 string + 4,
string + 6,
string + 9,
string + 11,
string + 13);
690 retc->
x.
str_val = g_strdup (helpbuf);
691 retc->
size = strlen (helpbuf);
732 int nyears, ndays, nseconds;
756 retc->
x.
str_val = g_strdup (timebuf);
757 retc->
size = strlen (timebuf);
static int days_per_month(int y, int m)
static int check_isotime(const my_isotime_t atime)
static unsigned long date2jd(int year, int month, int day)
tree_cell * nasl_isotime_add(lex_ctxt *lexic)
Add days or seconds to an ISO time string.
static int add_seconds_to_isotime(my_isotime_t atime, int nseconds)
static int add_days_to_isotime(my_isotime_t atime, int ndays)
tree_cell * nasl_isotime_is_valid(lex_ctxt *lexic)
Check whether an ISO time string is valid.
char my_isotime_t[ISOTIME_SIZE]
tree_cell * nasl_isotime_print(lex_ctxt *lexic)
Convert an SIO time string into a better readable string.
static int isotime_p(const char *string)
tree_cell * nasl_isotime_now(lex_ctxt *lexic)
Return the current time in ISO format.
static int string2isotime(my_isotime_t atime, const char *string)
static int isotime_human_p(const char *string)
static void epoch2isotime(my_isotime_t timebuf, time_t atime)
static int add_years_to_isotime(my_isotime_t atime, int nyears)
tree_cell * nasl_isotime_scan(lex_ctxt *lexic)
Convert a string into an ISO time string.
static int jd2date(unsigned long jd, int *year, int *month, int *day)
static int days_per_year(int y)
static void get_current_isotime(my_isotime_t timebuf)
Protos and data structures for ISOTIME functions used by NASL scripts.
long int get_var_size_by_num(lex_ctxt *, int)
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
struct struct_lex_ctxt lex_ctxt
char * get_str_var_by_num(lex_ctxt *, int)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
tree_cell * alloc_typed_cell(int typ)
union TC::@332262321161220155002104006201360276211317150140 x
Define a string struct for storing the response.