Skip to content

Commit

Permalink
ADDED: date_time_stamp/2: allow leaving components unbound from the r…
Browse files Browse the repository at this point in the history
…ight.
  • Loading branch information
JanWielemaker committed Sep 30, 2024
1 parent 48ffa31 commit f0b87bf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
19 changes: 13 additions & 6 deletions man/builtin.doc
Original file line number Diff line number Diff line change
Expand Up @@ -10395,12 +10395,19 @@ to extract the local time, \const{'UTC'} to extract a UTC time or an
integer describing the seconds west of Greenwich.

\predicate{date_time_stamp}{2}{+DateTime, -TimeStamp}
Compute the timestamp from a {date}/9 term. Values for month, day,
hour, minute or second need not be normalized. This flexibility
allows for easy computation of the time at any given number of
these units from a given timestamp. Normalization can be achieved
following this call with stamp_date_time/3. This example computes
the date 200 days after 2006-07-14:
Compute the timestamp from a \funcref{date}{9} term. Values for month, day,
hour, minute or second may be left unbound from the right, i.e.,
seconds may be unbound, or seconds and minutes, etc. It is not
allowed to specify the seconds and leave one of the more significant
fields unbound. Unbound values unified to their lowest possible
value.

Values for month, day, hour, minute or second need not be
\jargon{normalized}. This flexibility allows for easy computation of
the time at any given number of these units from a given timestamp.
Normalization can be achieved following this call with
stamp_date_time/3. This example computes the date 200 days after
2006-07-14:

\begin{code}
?- date_time_stamp(date(2006,7,214,0,0,0,0,-,-), Stamp),
Expand Down
49 changes: 33 additions & 16 deletions src/os/pl-tai.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,6 @@ get_voff_arg(int i, term_t t, term_t a, int *val)
}


static int
get_float_arg(int i, term_t t, term_t a, double *val)
{ GET_LD

_PL_get_arg(i, t, a);

return PL_get_float_ex(a, val);
}


static int
get_dst_arg(int i, term_t t, term_t a, int *val)
{ GET_LD
Expand All @@ -296,6 +286,29 @@ get_dst_arg(int i, term_t t, term_t a, int *val)
return PL_get_bool_ex(a, val); /* generate an error */
}

static int
get_v_float_arg(int i, term_t t, term_t a, double *val, bool *v, double def)
{ _PL_get_arg(i, t, a);
if ( PL_is_variable(a) && *v )
{ *val = def;
return PL_unify_float(a, def);
} else
{ *v = false;
return PL_get_float_ex(a, val);
}
}

static int
get_v_int_arg(int i, term_t t, term_t a, int *val, bool *v, int def)
{ _PL_get_arg(i, t, a);
if ( PL_is_variable(a) && *v )
{ *val = def;
return PL_unify_integer(a, def);
} else
{ *v = false;
return PL_get_integer_ex(a, val);
}
}

static int
get_ftm(term_t t, ftm *ftm)
Expand All @@ -306,12 +319,16 @@ get_ftm(term_t t, ftm *ftm)
memset(ftm, 0, sizeof(*ftm));

if ( (date9=PL_is_functor(t, FUNCTOR_date9)) )
{ if ( get_int_arg (1, t, tmp, &ftm->tm.tm_year) &&
get_int_arg (2, t, tmp, &ftm->tm.tm_mon) &&
get_int_arg (3, t, tmp, &ftm->tm.tm_mday) &&
get_int_arg (4, t, tmp, &ftm->tm.tm_hour) &&
get_int_arg (5, t, tmp, &ftm->tm.tm_min) &&
get_float_arg(6, t, tmp, &ftm->sec) &&
{ bool v = true;

if ( get_int_arg (1, t, tmp, &ftm->tm.tm_year) &&

get_v_float_arg(6, t, tmp, &ftm->sec, &v, 0.0) &&
get_v_int_arg (5, t, tmp, &ftm->tm.tm_min, &v, 0) &&
get_v_int_arg (4, t, tmp, &ftm->tm.tm_hour, &v, 0) &&
get_v_int_arg (3, t, tmp, &ftm->tm.tm_mday, &v, 1) &&
get_v_int_arg (2, t, tmp, &ftm->tm.tm_mon, &v, 1) &&

get_voff_arg (7, t, tmp, &ftm->utcoff) &&
get_tz_arg (8, t, tmp, &ftm->tzname) &&
get_dst_arg (9, t, tmp, &ftm->isdst) )
Expand Down

0 comments on commit f0b87bf

Please sign in to comment.