60 #include <glib/gstdio.h> 72 # define DIM(v) (sizeof(v)/sizeof((v)[0])) 73 # define DIMof(type,member) DIM(((type *)0)->member) 78 #define ISOTIME_SIZE 16 82 #define JD_DIFF 1721060L 85 #define spacep(p) (*(p) == ' ' || *(p) == '\t') 86 #define digitp(p) (*(p) >= '0' && *(p) <= '9') 89 #define atoi_1(p) (*(p) - '0' ) 90 #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1)) 91 #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2)) 98 if (atime == (time_t)(-1))
104 tp = gmtime (&atime);
105 if (snprintf (timebuf,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
106 1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
107 tp->tm_hour, tp->tm_min, tp->tm_sec) < 0)
119 epoch2isotime (timebuf, time (NULL));
136 for (s=atime, i=0; i < 8; i++, s++)
141 for (s++, i=9; i < 15; i++, s++)
153 isotime_p (
const char *
string)
160 for (s=
string, i=0; i < 8; i++, s++)
165 for (s++, i=9; i < 15; i++, s++)
168 if ( !(!*s || (isascii (*s) && isspace(*s)) || *s ==
':' || *s ==
','))
180 isotime_human_p (
const char *
string)
187 for (s=
string, i=0; i < 4; i++, s++)
205 if (!*s || *s ==
',')
218 if (!*s || *s ==
',')
229 if (!*s || *s ==
',')
240 if (!*s || *s ==
',' ||
spacep (s))
260 if (isotime_p (
string))
262 memcpy (atime,
string, 15);
266 if (!isotime_human_p (
string))
268 atime[0] =
string[0];
269 atime[1] =
string[1];
270 atime[2] =
string[2];
271 atime[3] =
string[3];
272 atime[4] =
string[5];
273 atime[5] =
string[6];
274 atime[6] =
string[8];
275 atime[7] =
string[9];
277 memset (atime+9,
'0', 6);
283 atime[9] =
string[11];
284 atime[10] =
string[12];
285 if (
string[13] !=
':')
287 atime[11] =
string[14];
288 atime[12] =
string[15];
289 if (
string[16] !=
':')
291 atime[13] =
string[17];
292 atime[14] =
string[18];
302 isotime2epoch (
const char *
string)
304 int year, month, day, hour, minu, sec;
307 if (!isotime_p (
string))
311 month =
atoi_2 (
string + 4);
312 day =
atoi_2 (
string + 6);
313 hour =
atoi_2 (
string + 9);
314 minu =
atoi_2 (
string + 11);
315 sec =
atoi_2 (
string + 13);
318 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31
319 || hour > 23 || minu > 59 || sec > 61 )
322 memset (&tmbuf, 0,
sizeof tmbuf);
325 tmbuf.tm_hour = hour;
327 tmbuf.tm_mon = month-1;
328 tmbuf.tm_year = year - 1900;
330 return timegm (&tmbuf);
337 days_per_year (
int y)
345 return s ? 366 : 365;
351 days_per_month (
int y,
int m)
357 case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
365 case 4:
case 6:
case 9:
case 11:
376 date2jd (
int year,
int month,
int day)
380 jd = 365L * year + 31 * (month-1) + day +
JD_DIFF;
384 jd -= (4 * month + 23) / 10;
386 jd += year / 4 - ((year / 100 + 1) *3) / 4;
397 jd2date (
unsigned long jd,
int *year,
int *month,
int *day)
404 if (jd < 1721425 || jd > 2843085)
410 while ((delta = jd - date2jd (y, m, d)) > days_per_year (y))
413 m = (delta / 31) + 1;
414 while( (delta = jd - date2jd (y, m, d)) > days_per_month (y,m))
422 if (d > days_per_month (y, m))
440 return (jd - date2jd (y, 1, 1)) + 1;
448 add_seconds_to_isotime (
my_isotime_t atime,
int nseconds)
450 int year, month, day, hour, minute, sec, ndays;
453 if (check_isotime (atime))
456 if (nseconds < 0 || nseconds >= (0x7fffffff - 61) )
463 minute=
atoi_2 (atime+11);
468 || (year == 1582 && month < 10)
469 || (year == 1582 && month == 10 && day < 15))
480 jd = date2jd (year, month, day) + ndays;
481 jd2date (jd, &year, &month, &day);
483 if (year > 9999 || month > 12 || day > 31
484 || year < 0 || month < 1 || day < 1)
487 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
488 year, month, day, hour, minute, sec);
497 int year, month, day, hour, minute, sec;
500 if (check_isotime (atime))
503 if (ndays < 0 || ndays >= 9999*366 )
510 minute=
atoi_2 (atime+11);
515 || (year == 1582 && month < 10)
516 || (year == 1582 && month == 10 && day < 15))
519 jd = date2jd (year, month, day) + ndays;
520 jd2date (jd, &year, &month, &day);
522 if (year > 9999 || month > 12 || day > 31
523 || year < 0 || month < 1 || day < 1)
526 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
527 year, month, day, hour, minute, sec);
536 int year, month, day, hour, minute, sec;
539 if (check_isotime (atime))
542 if (nyears < 0 || nyears >= 9999 )
549 minute=
atoi_2 (atime+11);
554 || (year == 1582 && month < 10)
555 || (year == 1582 && month == 10 && day < 15))
558 jd = date2jd (year + nyears, month, day);
559 jd2date (jd, &year, &month, &day);
561 if (year > 9999 || month > 12 || day > 31
562 || year < 0 || month < 1 || day < 1)
565 snprintf (atime,
ISOTIME_SIZE,
"%04d%02d%02dT%02d%02d%02d",
566 year, month, day, hour, minute, sec);
593 get_current_isotime (timebuf);
596 retc->
x.
str_val = g_strdup (timebuf);
597 retc->
size = strlen (timebuf);
642 if (isotime_p (
string) || isotime_human_p (
string))
694 if (!string2isotime (timebuf,
string))
702 retc->
x.
str_val = g_strdup (timebuf);
703 retc->
size = strlen (timebuf);
732 strcpy (helpbuf,
"[none]");
734 snprintf (helpbuf,
sizeof helpbuf,
735 "%.4s-%.2s-%.2s %.2s:%.2s:%.2s",
736 string,
string+4,
string+6,
string+9,
string+11,
string+13);
738 retc->
x.
str_val = g_strdup (helpbuf);
739 retc->
size = strlen (helpbuf);
781 int nyears, ndays, nseconds;
786 || check_isotime (
string))
795 if (nyears && add_years_to_isotime (timebuf, nyears))
797 if (ndays && add_days_to_isotime (timebuf, ndays))
799 if (nseconds && add_seconds_to_isotime (timebuf, nseconds))
802 if (!nyears && !ndays && !nseconds && add_years_to_isotime (timebuf, 0))
806 retc->
x.
str_val = g_strdup (timebuf);
807 retc->
size = strlen (timebuf);
char my_isotime_t[ISOTIME_SIZE]
tree_cell * nasl_isotime_print(lex_ctxt *lexic)
Convert an SIO time string into a better readable string.
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
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)
char * get_str_var_by_num(lex_ctxt *, int)
Protos and data structures for ISOTIME functions used by NASL scripts.
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.