OpenVAS Scanner  6.0.1
Macros | Typedefs | Functions
nasl_isotime.c File Reference

Implementation of an API for ISOTIME values. More...

#include "nasl_isotime.h"
#include "nasl_debug.h"
#include "nasl_global_ctxt.h"
#include "nasl_lex_ctxt.h"
#include "nasl_tree.h"
#include "nasl_var.h"
#include <ctype.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

Macros

#define DIM(v)   (sizeof (v) / sizeof ((v)[0]))
 
#define DIMof(type, member)   DIM (((type *) 0)->member)
 
#define ISOTIME_SIZE   19
 
#define JD_DIFF   1721060L
 
#define spacep(p)   (*(p) == ' ' || *(p) == '\t')
 
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
 
#define atoi_1(p)   (*(p) - '0')
 
#define atoi_2(p)   ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))
 
#define atoi_4(p)   ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))
 

Typedefs

typedef char my_isotime_t[ISOTIME_SIZE]
 

Functions

static void epoch2isotime (my_isotime_t timebuf, time_t atime)
 
static void get_current_isotime (my_isotime_t timebuf)
 
static int check_isotime (const my_isotime_t atime)
 
static int isotime_p (const char *string)
 
static int isotime_human_p (const char *string)
 
static int string2isotime (my_isotime_t atime, const char *string)
 
static int days_per_year (int y)
 
static int days_per_month (int y, int m)
 
static unsigned long date2jd (int year, int month, int day)
 
static int jd2date (unsigned long jd, int *year, int *month, int *day)
 
static int add_seconds_to_isotime (my_isotime_t atime, int nseconds)
 
static int add_days_to_isotime (my_isotime_t atime, int ndays)
 
static int add_years_to_isotime (my_isotime_t atime, int nyears)
 
tree_cellnasl_isotime_now (lex_ctxt *lexic)
 Return the current time in ISO format. More...
 
tree_cellnasl_isotime_is_valid (lex_ctxt *lexic)
 Check whether an ISO time string is valid. More...
 
tree_cellnasl_isotime_scan (lex_ctxt *lexic)
 Convert a string into an ISO time string. More...
 
tree_cellnasl_isotime_print (lex_ctxt *lexic)
 Convert an SIO time string into a better readable string. More...
 
tree_cellnasl_isotime_add (lex_ctxt *lexic)
 Add days or seconds to an ISO time string. More...
 

Detailed Description

Implementation of an API for ISOTIME values.

This file contains the implementation of the isotime_* NASL builtin functions.

Background:

Most 32 bit systems use a signed 32 bit time_t to represent the system time. The problem is that in 2038 this time type will overflow. However, we sometimes need to compute dates in the future; for example some certificates are (for whatever reasons) valid for 30 years. To solve this problem in a platform independent way, we represent the time as a string and provide functions to work with them. This is not an elegant solution, but all proposed new time APIs have never been implemented on main stream systems - we can't expect that this will happen any time soon.

Macro Definition Documentation

◆ atoi_1

#define atoi_1 (   p)    (*(p) - '0')

◆ atoi_2

#define atoi_2 (   p)    ((atoi_1 (p) * 10) + atoi_1 ((p) + 1))

◆ atoi_4

#define atoi_4 (   p)    ((atoi_2 (p) * 100) + atoi_2 ((p) + 2))

◆ digitp

#define digitp (   p)    (*(p) >= '0' && *(p) <= '9')

◆ DIM

#define DIM (   v)    (sizeof (v) / sizeof ((v)[0]))

◆ DIMof

#define DIMof (   type,
  member 
)    DIM (((type *) 0)->member)

◆ ISOTIME_SIZE

#define ISOTIME_SIZE   19

◆ JD_DIFF

#define JD_DIFF   1721060L

◆ spacep

#define spacep (   p)    (*(p) == ' ' || *(p) == '\t')

Typedef Documentation

◆ my_isotime_t

typedef char my_isotime_t[ISOTIME_SIZE]

Function Documentation

◆ add_days_to_isotime()

static int add_days_to_isotime ( my_isotime_t  atime,
int  ndays 
)
static

◆ add_seconds_to_isotime()

static int add_seconds_to_isotime ( my_isotime_t  atime,
int  nseconds 
)
static

◆ add_years_to_isotime()

static int add_years_to_isotime ( my_isotime_t  atime,
int  nyears 
)
static

◆ check_isotime()

static int check_isotime ( const my_isotime_t  atime)
static

◆ date2jd()

static unsigned long date2jd ( int  year,
int  month,
int  day 
)
static

◆ days_per_month()

static int days_per_month ( int  y,
int  m 
)
static

◆ days_per_year()

static int days_per_year ( int  y)
static

◆ epoch2isotime()

static void epoch2isotime ( my_isotime_t  timebuf,
time_t  atime 
)
static

◆ get_current_isotime()

static void get_current_isotime ( my_isotime_t  timebuf)
static

◆ isotime_human_p()

static int isotime_human_p ( const char *  string)
static

◆ isotime_p()

static int isotime_p ( const char *  string)
static

◆ jd2date()

static int jd2date ( unsigned long  jd,
int *  year,
int *  month,
int *  day 
)
static

◆ nasl_isotime_add()

tree_cell* nasl_isotime_add ( lex_ctxt lexic)

Add days or seconds to an ISO time string.

NASL Function: isotime_add\n

This function adds days or seconds to an ISO time string and returns the resulting time string. The number of days or seconds are given using the named parameters; if none are given nothing is added; if both are given both additions are performed. This function won't work for dates before the Gregorian calendar switch.

NASL Unnamed Parameters:\n
  • An ISO time string
NASL Named Parameters:\n
  • years An integer with the number of years to add to the timestamp.
  • days An integer with the number of days to add to the timestamp.
  • seconds An integer with the number of seconds to add to the timestamp.
NASL Returns:\n The resulting ISO time string or NULL if the provided ISO
time string is not valid or the result would overflow (i.e. year > 9999).
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

◆ nasl_isotime_is_valid()

tree_cell* nasl_isotime_is_valid ( lex_ctxt lexic)

Check whether an ISO time string is valid.

NASL Function: isotime_is_valid\n
NASL Unnamed Parameters:\n
  • A string. Both, the standard 15 byte string and the better human readable up to 19 byte format are accepted here. If a plain data type is is provided only the 15 byte format is accepted.
NASL Returns:\n True is this is an ISO string; false if not.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

◆ nasl_isotime_now()

tree_cell* nasl_isotime_now ( lex_ctxt lexic)

Return the current time in ISO format.

NASL Function: isotime_now\n
NASL Unnamed Parameters:\n
  • None
NASL Returns:\n A string with the ISO time. If the current time is not
available an empty string is returned.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

◆ nasl_isotime_print()

tree_cell* nasl_isotime_print ( lex_ctxt lexic)

Convert an SIO time string into a better readable string.

NASL Function: isotime_print\n
NASL Unnamed Parameters:\n
  • An ISO time string.
NASL Returns:\n A string in the format "YYYY-MM-DD HH:MM:SS" or "[none]"
if the provided time string is not valid.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

◆ nasl_isotime_scan()

tree_cell* nasl_isotime_scan ( lex_ctxt lexic)

Convert a string into an ISO time string.

NASL Function: isotime_scan\n
NASL Unnamed Parameters:\n
  • A string
NASL Returns:\n A ISO time string on success or NULL on error.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

◆ string2isotime()

static int string2isotime ( my_isotime_t  atime,
const char *  string 
)
static