Skip to content

Commit

Permalink
Add support for $date-current, $date-start, and ISO-8601 time period …
Browse files Browse the repository at this point in the history
…values for

relative date/time values.
  • Loading branch information
michaelrsweet committed May 13, 2019
1 parent bd51657 commit c8d7d4b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
74 changes: 73 additions & 1 deletion cups/ipp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,80 @@ parse_value(_ipp_file_t *f, /* I - IPP data file */
utc_offset = 0; /* Timezone offset from UTC */
ipp_uchar_t date[11]; /* dateTime value */

if (sscanf(value, "%d-%d-%dT%d:%d:%d%d", &year, &month, &day, &hour, &minute, &second, &utc_offset) < 6)
if (*value == 'P')
{
/*
* Time period...
*/

time_t curtime; /* Current time in seconds */
int period = 0, /* Current period value */
saw_T = 0; /* Saw time separator */

curtime = time(NULL);

for (valueptr = value + 1; *valueptr; valueptr ++)
{
if (isdigit(*valueptr & 255))
{
period = strtol(valueptr, &valueptr, 10);

if (!valueptr || period < 0)
{
report_error(f, v, user_data, "Bad dateTime value \"%s\" on line %d of \"%s\".", value, f->linenum, f->filename);
return (0);
}
}

if (*valueptr == 'Y')
{
curtime += 365 * 86400 * period;
period = 0;
}
else if (*valueptr == 'M')
{
if (saw_T)
curtime += 60 * period;
else
curtime += 30 * 86400 * period;

period = 0;
}
else if (*valueptr == 'D')
{
curtime += 86400 * period;
period = 0;
}
else if (*valueptr == 'H')
{
curtime += 3600 * period;
period = 0;
}
else if (*valueptr == 'S')
{
curtime += period;
period = 0;
}
else if (*valueptr == 'T')
{
saw_T = 1;
period = 0;
}
else
{
report_error(f, v, user_data, "Bad dateTime value \"%s\" on line %d of \"%s\".", value, f->linenum, f->filename);
return (0);
}
}

return (ippSetDate(ipp, attr, element, ippTimeToDate(curtime)));
}
else if (sscanf(value, "%d-%d-%dT%d:%d:%d%d", &year, &month, &day, &hour, &minute, &second, &utc_offset) < 6)
{
/*
* Date/time value did not parse...
*/

report_error(f, v, user_data, "Bad dateTime value \"%s\" on line %d of \"%s\".", value, f->linenum, f->filename);
return (0);
}
Expand Down
4 changes: 4 additions & 0 deletions doc/help/man-ipptoolfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ <h3><a name="VARIABLES">Variables</a></h3>
<dd style="margin-left: 5.0em">Inserts a single "$" character.
<dt><b>$ENV[</b><i>name</i><b>]</b>
<dd style="margin-left: 5.0em">Inserts the value of the named environment variable, or an empty string if the environment variable is not defined.
<dt><b>$date-current</b>
<dd style="margin-left: 5.0em">Inserts the current date and time using the ISO-8601 format ("yyyy-mm-ddThh:mm:ssZ").
<dt><b>$date-start</b>
<dd style="margin-left: 5.0em">Inserts the starting date and time using the ISO-8601 format ("yyyy-mm-ddThh:mm:ssZ").
<dt><b>$filename</b>
<dd style="margin-left: 5.0em">Inserts the filename provided to
<b>ipptool</b>(8)
Expand Down
8 changes: 7 additions & 1 deletion man/ipptoolfile.5
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.\" Licensed under Apache License v2.0. See the file "LICENSE" for more
.\" information.
.\"
.TH ipptoolfile 5 "CUPS" "26 April 2019" "Apple Inc."
.TH ipptoolfile 5 "CUPS" "13 May 2019" "Apple Inc."
.SH NAME
ipptoolfile \- ipptool file format
.SH DESCRIPTION
Expand Down Expand Up @@ -594,6 +594,12 @@ Inserts a single "$" character.
\fB$ENV[\fIname\fB]\fR
Inserts the value of the named environment variable, or an empty string if the environment variable is not defined.
.TP 5
\fB$date-current\fR
Inserts the current date and time using the ISO-8601 format ("yyyy-mm-ddThh:mm:ssZ").
.TP 5
\fB$date-start\fR
Inserts the starting date and time using the ISO-8601 format ("yyyy-mm-ddThh:mm:ssZ").
.TP 5
\fB$filename\fR
Inserts the filename provided to
.BR ipptool (8)
Expand Down
7 changes: 7 additions & 0 deletions tools/ipptool.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ main(int argc, /* I - Number of command-line args */

_ippVarsInit(&vars, NULL, (_ipp_ferror_cb_t)error_cb, (_ipp_ftoken_cb_t)token_cb);

_ippVarsSet(&vars, "date-start", iso_date(ippTimeToDate(time(NULL))));

/*
* We need at least:
*
Expand Down Expand Up @@ -3992,6 +3994,8 @@ token_cb(_ipp_file_t *f, /* I - IPP file data */
data->transfer = data->def_transfer;
data->version = data->def_version;

_ippVarsSet(vars, "date-current", iso_date(ippTimeToDate(time(NULL))));

f->attrs = ippNew();
f->group_tag = IPP_TAG_ZERO;
}
Expand All @@ -4003,6 +4007,7 @@ token_cb(_ipp_file_t *f, /* I - IPP file data */

if (_ippFileReadToken(f, name, sizeof(name)) && _ippFileReadToken(f, temp, sizeof(temp)))
{
_ippVarsSet(vars, "date-current", iso_date(ippTimeToDate(time(NULL))));
_ippVarsExpand(vars, value, temp, sizeof(value));
_ippVarsSet(vars, name, value);
}
Expand All @@ -4022,6 +4027,7 @@ token_cb(_ipp_file_t *f, /* I - IPP file data */
{
if (!_ippVarsGet(vars, name))
{
_ippVarsSet(vars, "date-current", iso_date(ippTimeToDate(time(NULL))));
_ippVarsExpand(vars, value, temp, sizeof(value));
_ippVarsSet(vars, name, value);
}
Expand All @@ -4040,6 +4046,7 @@ token_cb(_ipp_file_t *f, /* I - IPP file data */

if (_ippFileReadToken(f, temp, sizeof(temp)))
{
_ippVarsSet(vars, "date-current", iso_date(ippTimeToDate(time(NULL))));
_ippVarsExpand(vars, data->file_id, temp, sizeof(data->file_id));
}
else
Expand Down

0 comments on commit c8d7d4b

Please sign in to comment.