Aller au contenu principal

Time

A collection of functions to manipulate time (i.e timestamp)

Index

Bound

readonlymaxValue

maxValue: Time

Maximum value for this type

@example
const anyNumber: number
anyNumber < Number.maxValue // true

readonlyminValue

minValue: Time

Minimum value for this type

@example
const anyNumber: number
anyNumber > Number.minValue // true

Codec

codecDecode

  • codecDecode(this: void, input: unknown, context: Context<Time>): Result<Time, CodecError>
  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

codecEncode

  • codecEncode(this: void, input: Time): unknown
  • Returns the encoded input

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

codecSchema

  • codecSchema(this: void): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Comparator

!=

  • !=(this: void, left: Time, right: Time): boolean
  • "Not equal to" operator

    @example
    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this: void, left: Time, right: Time): boolean
  • "Less than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this: void, left: Time, right: Time): boolean
  • "Less than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this: void, left: Time, right: Time): boolean
  • "Equal to" operator

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this: void, left: Time, right: Time): boolean
  • "Greater than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this: void, left: Time, right: Time): boolean
  • "Greater than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

clamp

  • clamp(this: void, value: Time, minValue: Time, maxValue: Time): Time
  • Clamp value between minValue and maxValue

    @example
    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this: void, left: Time, right: Time): number
  • Return a number that represents comparison

    @example
    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

equals

  • equals(this: void, left: Time, right: Time): boolean
  • Alias to '=='

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this: void, left: Time, right: Time): Time
  • "maximum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this: void, left: Time, right: Time): Time
  • "minimum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Constructor

asInstance

  • asInstance(anyValue: unknown): Option<Time>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

of

  • of(milliseconds: number): Time
  • Create a new Time value

    @example
    const time = Time.of(0);

Type

inspect

inspect: Option<(anyValue: Time, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

When defined, returns a custom string representation

@example

@param

hasInstance

  • hasInstance(anyValue: unknown): anyValue is Time
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

typeName

Re-exports typeName

__call__

  • Converts a number or a TimeObject to a Time

    @example
    Time.from({ year: 2020, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });// new Date('2020-01-01T00:00:00.000Z').getTime()

add

  • add(time: Time, duration: TimeDuration): Time
  • Adds duration to time

    @example
    const now = Time.of(0);
    const duration = TimeDuration.of(10);
    Time.add(now, duration);// now + 10ms

delay

  • delay(duration: TimeDuration): Task<Time, never>
  • Return a new Task that resolves the current time in milliseconds after waiting duration.

    @example
    const wait2s = Time.delay(TimeDuration.seconds(2));
    const logTime = Task.andThen(wait2s, (time) => Console.debug(time));
    Task.run(logTime);// wait 2 seconds then console.debug(Date.now())

diff

  • diff(left: Time, right: Time): TimeDuration
  • Return the difference between 2 time values

    @example
    const begin = Time.of(0);
    const end = Time.of(10);
    Time.diff(end, begin);// TimeDuration.of(10)

format

  • format(time: Time): string
  • Return an ISO 8601 string representation

    @example
    const time = Time.of(0);
    Time.format(time);// '1970-01-01T00:00:00.000Z'

from

  • Converts a number or a TimeObject to a Time

    @example
    Time.from({ year: 2020, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });// new Date('2020-01-01T00:00:00.000Z').getTime()

now

  • now(): Task<Time, never>
  • A task that resolves the current time in milliseconds.

    @example
    const program = () => Task.andThen(Time.now(), (currentTime) => {
    // use currentTime
    });

parse

  • parse(expression: string): Option<Time>
  • Parse an ISO 8601 string. If invalid, returns Option.None

    @example
    Time.parse('1970-01-01T00:00:00.000Z');// 0

unwrap

  • unwrap(value: Time): number
  • Convert a tagged value to the underlying type

wrap

  • wrap(value: number): Time
  • Convert an underlying type to a tagged type