4.6.6 Method for say-time-of-day

We can provide a way to ask an instance of <time-of-day> to describe the time in a conventional format, such as 8:30. For the application that we are planning, there is no need to view the seconds. We want the method to print the description in a window on the screen. We define a method named say-time-of-day:

define method say-time-of-day (time :: <time-of-day>) => ()	// 1
  let(hours, minutes) = decode-total-seconds(time);	// 2
  format-out	// 3
    ("%d:%s%d", hours, if (minutes < 10) "0" else "" end, minutes);	// 4
end method say-time-of-day;	// 5

On line 1, we provide an empty value declaration, which means that this method returns no values.

On line 2, we use let to initialize two local variables to the first and second values returned by decode-total-seconds. Remember that decode-total-seconds returns three values (the third value is the seconds). For the application that we are planning, the say-time-of-day method does not need to show the seconds, so we do not use the third value. It is not necessary to receive the third value of decode-total-seconds; here we do not provide a local variable to receive the third value, so that value is simply ignored.

On line 4, we use if to print a leading 0 for the minutes when there are fewer than 10 minutes, such as 2:05.

Comparison to C: In C, if does not return a value. In Dylan, if returns the value of the body that is selected, if any is.

Note on format-out: We have purposely used a limited subset of the format-out function's features to allow our examples to run on as many Dylan implementations as possible. The printing of times could be done much more elegantly if we used the full power of the format-out function.

We can call say-time-of-day:

? say-time-of-day(*your-time-of-day*);
8:30
? say-time-of-day(*my-time-of-day*);
0:02

The listener displays the output (printed by format-out), but displays no values, because say-time-of-day does not return any values.