View Raw SPL
/****************************************************************************
* *
* WRAPTIME.SPL Copyright 2008 (C) DSP Development Corporation *
* *
* Author: Randy Race *
* *
* Synopsis: Converts AM/PM time to 24 hour time *
* *
* Revisions: 27 Aug 2008 RRR Creation *
* *
****************************************************************************/
#if @HELP_WRAPTIME
WRAPTIME
Purpose: Converts 12 hour AM/PM time to 24 hour time
Syntax: WRAPTIME(t, ampm, wrap)
t - A series of time values
ampm - Optional string, the starting time offset. "AM"
indicates morning time, "PM" indicates afternoon
time. Defaults to "AM".
wrap - Optional integer, add day values to sequential time
changes such that 23:59:59 to 0:0:0 represents the
next day.
0: do not wrap days
1: wrap days (default)
Returns: A series of time values.
Example:
W1: {todstr("12:59"),todstr("1:00"),todstr("1:01")};setvunits("Time")
W2: wraptime(w1)
W2 contains the time values:
12:59, 13:00, 13:01
Example:
W3: {todstr("10:59"), todstr("11:00"), todstr("11:01")};setvunits("Time")
W4: wraptime(w3, "pm")
W4 contains the time values:
22:59, 23:00, 23:01
Remarks:
As shown in the example, AM/PM time values will show an
apparent discontinuity when the time changes at noon or
midnight. WRAPTIME converts the values to 24 hour time and
thus removes these discontinuities.
If wrap is 1 (the default),the time values are considered
sequential such that a change in time from 2359.59 to 0.0
indicates a change in day.
See Also:
DTXY
JULSTR
STRJUL
STRTOD
TODSTR
XYDT
#endif
/* convert AM/PM time to 24 hour time */
wraptime(t, ampm, wrap)
{
local dt;
if (argc < 3)
{
if (argc < 2)
{
if (argc < 1) error("wraptime - input series required");
ampm = "am";
}
wrap = 1;
}
ampm = tolower(ampm);
/* find time discontinuities */
dt = extract(t - delay(t, 1), 2, length(t));
dt[length(dt)] = dt[length(dt) - 1];
/* time offsets */
dt = partsum(dt < 0) * 43200;
/* unwrap by adding offsets */
t = extract(t + delay(dt, 1), 1, length(t));
/* AM/PM offset */
if (ampm == "pm") t += 43200.0;
if (wrap != 1)
{
/* factor out day changes */
t %= 86400;
}
return(t);
}