REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 21-Jun-2009 Edit History  

REBOL 3 Datatypes: Time!

Contents

Concept

The REBOL language supports the standard expression of time in hours, minutes, seconds, and subseconds. Both positive and negative times are permitted.

The time! datatype uses relative rather than absolute time. For example, 10:30 is 10 hours and 30 minutes rather than the time of 10:30 A.M. or P.M.

Format

Times are expressed as a set of integers separated by colons (:).. Hours and minutes are required, but seconds are optional. Within each field, leading zeros are ignored:

10:30
0:00
18:59
23:59:50
8:6:20
8:6:2

The minutes and seconds fields can contain values greater than 60. Values greater than 60 are automatically converted. For instance 0:120:00 is the same as 2:00.

probe 00:120:00
2:00

Subseconds are specified using a decimal in the seconds field. Use either a period or a comma as the decimal point. The hours and minutes fields become optional when the decimal is present. Subseconds are encoded to the nano-second, or one billionth (- US, one milliardth - GB) of a second:

probe 32:59:29.5
32:59:29.5
probe 1:10,25
0:01:10.25
probe 0:0.000000001
0:00:00.000000001
probe 0:325.2
0:05:25.2

Times can be followed by AM or PM, but no space is permitted. PM adds 12 hours to the time:

probe 10:20PM
22:20
probe 3:32:20AM
3:32:20

Times are output in a standard hours, minutes, seconds, and subseconds format, regardless of how they are entered:

probe 0:87363.21
24:16:03.21

Access

Time values have three refinements that can be used to return specific information about the value:

Refinement Description
'/hour Gets the value's hour.
'/minute Gets the value's minute.
'/second Gets the value's second.

Here's how to use a time value's refinements:

lapsed-time: 91:32:12.14
probe lapsed-time/hour
91
probe lapsed-time/minute
32
probe lapsed-time/second
12.14

Times with time zones can only be used with the date!.

Creation

Times can be converted using the to-time function:

probe to-time "10:30"
10:30
probe to-time [10 30]
10:30
probe to-time [0 10 30]
0:10:30
probe to-time [10 30 20.5]
10:30:20.5

In the previous examples, the values are not evaluated. To evaluate values as mathematical expressions, use the reduce function:

probe to-time reduce [10 30 + 5]
10:35

In various math operations involving time values, the time values, integers, or decimals are converted as shown below:

probe 10:30 + 1
10:30:01
probe 10:00 - 10
9:59:50
probe 0:00 - 10
-0:00:10
probe 5:10 * 3
15:30
probe 0:0:0.000000001 * 1'500'600
0:00:00.0015006
probe 8:40:20 / 4
2:10:05
probe 8:40:20 / 2:20:05
3
probe 8:40:20 // 4:20
0:00:20

Related

Use time? to determine whether a value is a time! datatype:

probe time? 10:30
true
probe time? 10.30
false

Use the now function with the /time refinement to return the current local date and time:

print now/time
14:42:15

Use the wait function to wait for a duration, port, or both.

If a value is a time! datatype, wait delays for that period of time. If a value is a date!/time!, wait waits until the indicated date and time. If the value is an integer! or decimal!, the function waits the indicated number of seconds. If the value is a port!, the function will wait for an event from that port. If a block is specified, it will wait for any of the times or ports to occur. It returns the port that caused the wait to complete or returns none! if the timeout occurred. For example,

probe now/time
14:42:16
wait 0:00:10
probe now/time
14:42:26


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin