Time: a plug for TIME.HMS

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Time: a plug for TIME.HMS

Richard Ristow
Function TIME.HMS is a *good* way of specifying
time intervals, including constants. It's a good
deal more readable than 'magic numbers' (e.g.,
"twelve hours" as "TIME.HMS(12)" rather than
"43200"). And it's astonishingly, and
gratifyingly, flexible. From the SPSS 15 Command Syntax Reference(*):

>You can supply one, two, or three arguments.
>Trailing arguments can be omitted and default to
>0. The value of the first nonzero argument can
>spill over into the next higher argument. For example, the command
>
>COMPUTE Period2=TIME.HMS(hr,min).
>
>produces an interval of 5400 seconds for Period2
>when hr is 0 and min is 90. The value can be
>displayed as 01:30 with a TIME5 print format.
>
>You can have a non-integer value for the last
>argument. For example, the command
>
>COMPUTE Period3=TIME.HMS(hr).
>
>produces an interval of 5400 seconds for Period3
>when hr equals 1.5 and is displayed as 01:30 with a TIME5 format.
>When you supply a nonzero argument to a
>function, each of the lower-level units must be
>within the range of –60 to +60.

And don't forget TIME.DAYS, which also accepts
fractional values; see TIME4 on the first record in the demo

Demo, SPSS 14 draft output (WRR:not saved separately):
INPUT PROGRAM.
.  STRING  TYPE (A8).
.  NUMERIC TIME1 TIME2 TIME3 TIME4 (TIME8).
.  COMPUTE    TYPE = 'Hours'.
.     COMPUTE TIME1 = TIME.HMS(1,30).
.     COMPUTE TIME2 = TIME.HMS(1.5).
.     COMPUTE TIME3 = TIME.HMS(0,90).
.     COMPUTE TIME4 = TIME.DAYS(1/16).
.  END CASE.
.  COMPUTE TYPE = 'Minutes'.
.     COMPUTE TIME1 = TIME.HMS(0,1,30).
.     COMPUTE TIME2 = TIME.HMS(0,1.5).
.     COMPUTE TIME3 = TIME.HMS(0,0,90).
.     COMPUTE TIME4 = TIME.HMS(1/40).
.  END CASE.
END FILE.
END INPUT PROGRAM.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |17-OCT-2007 14:44:52       |
|-----------------------------|---------------------------|
TYPE        TIME1    TIME2    TIME3    TIME4

Hours     1:30:00  1:30:00  1:30:00  1:30:00
Minutes   0:01:30  0:01:30  0:01:30  0:01:30

Number of cases read:  2    Number of cases listed:  2
...................................................
(*) The SPSS 15 Command Syntax Reference gives a
far more restricted specification for TIME.HMS:

>TIME.HMS. TIME.HMS(hours,minutes,seconds).
>Numeric . Returns a time interval corresponding
>to the indicated number of hours, minutes, and
>seconds. Hours must resolve to an integer, and
>minutes must resolve to an integer less than 60.
>Seconds can contain decimals but must resolve to
>a number less than 60. All arguments must
>resolve to either all positive or all negative
>values. To display the result as a time, assign
>a time format to the result variable.

I don't have a working copy of 15 or 16 at the
moment, but I'll bet the behavior is still as
previously documented, though.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD