Time! datatype

1. Abstract

Time! values represent an amount of time, specified in hours, minutes, and seconds. They may be interpreted as an offset from midnight, in the context of dates, or as absolute times of day.

Time! is a member of the following typesets: immediate!, scalar!

2. Creation

Time! values can be created using literal syntax, or at runtime by using a make constructor or to conversion.

3. Literal syntax

BNF:

<time-literal> ::= <Red-time> | +<Red-time> | -<Red-time>
<Red-time> ::= <hours>:<minutes> | <hours>:<minutes>:<seconds> | <hours>:<minutes>:<seconds>.<decimals> |
               <minutes>:<seconds>.<decimals>

4. Runtime Creation

make time! [<hour> <minute> <second>]
make time! <integer-literal> | <float-literal>

to time! [<hour> <minute> <second>]
to time! <integer-literal> | <float-literal>

<hour>    : <integer-literal>
<minute>  : <integer-literal>
<second>  : <integer-literal> | <float-literal>
>> make time! 60
== 0:01:00

>> make time! 95'000
== 26:23:20

>> make time! [3 2 1]
== 3:02:01

>> make time! 300.45
== 0:05:00.45
>> to time! 60
== 0:01:00

>> to time! 95'000
== 26:23:20

>> to time! [3 2 1]
== 3:02:01

5. Path accessors

Path accessors provide a convenient way for getting and setting all time! value fields.

5.1. /hour

Description

Gets or sets the hour field of a time. Hour is returned as an integer! value.

Examples

>> t: make time! [3 2 1]
== 3:02:01

>> t/hour
== 3

>> t/hour: 6
== 6

>> t
== 6:02:01

5.2. /minute

Description

Gets or sets the minute field of a time. Minute is returned as an integer! value.

Examples

>> t: make time! [3 2 1]
== 3:02:01

>> t/minute
== 2

>> t/minute: 7
== 7

>> t
== 3:07:01

5.3. /second

Description

Gets or sets the second field of a time. Second is returned as an float! value.

Examples

>> t: make time! [3 2 1]
== 3:02:01

>> t/second
== 1.0

>> t/second: 58
== 58

>> t
== 3:02:58

Time fields are also accessible by index using path notation.

>> t
== 3:02:01

>> t/1
== 3

>> t/2
== 2

>> t/3
== 1.0

5.4. Accessing time fields using Pick

It is possible to access time fields without using a path, which can be more convenient in some cases. pick can be used for that on times.

Syntax

pick <time> <field>

<time>  : a time! value
<field> : an integer! value

Examples

>> t
== 3:02:01

>> pick t 1
== 3

>> pick t 2
== 2

>> pick t 3
== 1.0

6. Conversions

to integer! <time>

<time>  : a time! value
>> t
== 3:02:01

>> to integer! t
== 10921
to float! <time>

<time>  : a time! value
>> t
== 3:02:01

>> to float! t
== 10921.0

Integer!, float!, and percent! values can be converted to time.

>> i: to integer! t
== 10921

>> to time! i
== 3:02:01
>> f: to float! t
== 10921.0

>> to time! f
== 3:02:01
>> p: to percent! t
== 1092100%

>> to time! p
== 3:02:01

7. Comparisons

All comparators can be applied on time!: =, ==, <>, >, <, >=, <=, =?. In addition, min, and max are also supported.

8. Arithmetic

Supported math operations on times include:

  • adding or subtracting values from any time field.

  • adding or subtracting an integer value with a time value.

  • adding or subtracting a time value with a time value.

  • multiplying or dividing values from any time field.

  • multiplying or dividing an integer value with a time value.

Examples

>> t: 2:30:45
== 2:30:45

>> t/hour: t/hour + 5
== 7

>> t
== 7:30:45

>> t/minute: t/minute - 20
== 10

>> t
== 7:10:45
>> 1:40:45 + 50
== 1:41:35

>> 1:40:45 - 100
== 1:39:05
>> 3:02:01 + 1:45:30
== 4:47:31

>> 3:02:01 - 1:00:00
== 2:02:01
t: 2:30:45

>> t/second: t/second * 5
== 225.0

>> t
== 2:33:45

>> t/2 / 3
== 11

>> t/2 // 3
== 0

>> t/3 * 3
== 135.0

9. Testing values

Use time? to check if a value is of the time! datatype.

>> time? t
== true

Use type? to return the datatype of a given value.

>> type? t
== time!
>>

10. Pre-defined words

10.1. Actions

absolute, add, change, divide, even?, multiply, negate, odd?, pick, remainder, round, subtract

10.2. Functions

first, mod, modulo, second, third, time?, to-time

10.3. Natives

loop, negative?, now, positive?, remove-each, repeat, sign?, wait, zero?

10.4. Operators

%, *, +, -, /, //

results matching ""

    No results matching ""