Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TimeWriter

Time writers are objects used to synthesize time values into strings, i.e. write times using different lexical and numerical configurations. Write is used to express a single time value, using a single time unit; countdown is used to classify time values by segmenting them into parts with different time units.

These configurations are loaded in the following order:

  • Defaults — hardcoded for English (US);
  • Options from the current Locale;
  • Options from the instance settings;
  • Options given as a parameter to a method.

Examples

Writing time using different units

First, import and initialize the appropriate objects:

  • EcmaScript 6:

    import { Time, TimeWriter } from "timecount";
    
    var time = new Time(1000);
    const timeWriter = new TimeWriter();
    
  • Older EcmaScripts:

    var timecount = require("timecount");
    
    var time = new timecount.Time(1000);
    var timeWriter = new timecount.TimeWriter();
    

Next, use the hypothetical log function to receive time outputs:


1. Writing multiple time values

import { Time, TimeWriter } from "timecount";

const timeWriter = new TimeWriter();

// Writes the value of 90 minutes using hours:
console.log(timerWriter.write(90, "minute", "hour"));
// Result: 1.5 h

// Writes the value of 90 minutes:
console.log(timerWriter.write(90, "minute"));
// Result: 90 min

// Writes the value of a trillion nanoseconds in minutes:
console.log(timerWriter.write(new Time(1000000000000), "minute");
// Result: 16.666666666666668 min

// By default, timecount will use nanoseconds for input and output:
console.log(timerWriter.write(1000000000000));
// Result: 1000000000000 ns

2. Customizing the time output

There are three places where the output of a time writer (TimeWriterSettings) can be configured, each one overriding the previous:

  1. Locale, which is used for translations;
  2. The object itself (TimeWriter.settings);
  3. Parameter options of the write method.
const timecount = require("timecount");

const tw = new TimeWriter({ verbose: true });

tw.write(10, "second");
// Result: 10 seconds

tw.write(10, "second", { spaceTimeUnit: false, verbose: false });
// Result: 10s

tw.write(10, "day");
// Result: 10 days

Locale.set("es");

tw.write(10, "day");
// Result: 10 días

tw.settings.verbose = false;

tw.write(10, "second");
// Result: 10 s

Hierarchy

  • TimeWriter

Index

Constructors

Properties

Methods

Constructors

constructor

Properties

settings

Configurations pertaining to this instance, overriding those of the locale. May be overridden via parameter of write or countdown.

Methods

countdown

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    This "overload" will always use TIME_SEGMENTS_COMMON as source for its time unit conversions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific countdown. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Rest ...args: Array<TimeUnitSource | TimeUnitSource[]>

      Accepts any number of time units or array of time units (if repetitions are given, they will be singled out).

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Rest ...args: Array<string | TimeUnit | string[]>

      Accepts any number of time units or array of time units (if repetitions are given, they will be singled out).

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific countdown. It does not change the object in any way whatsoever.

    • Rest ...args: Array<string | TimeUnit | string[]>

      Accepts any number of time units or array of time units (if repetitions are given, they will be singled out).

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific countdown. It does not change the object in any way whatsoever.

    • Rest ...args: Array<TimeUnitSource | TimeUnitSource[]>

      Accepts any number of time units or array of time units (if repetitions are given, they will be singled out).

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

  • Writes a time length segmented into multiple units using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will produce explanations of time lengths much easier to understand than arbitrary fractions.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional options: TimeWriterSettings | TimeUnitSource

      When given, these configurations will override the those of the Locale and the instance, for this specific countdown. It does not change the object in any way whatsoever.

    • Rest ...args: Array<string | TimeUnit | string[]>

      Accepts any number of time units or array of time units (if repetitions are given, they will be singled out).

    Returns string

    A string containing a synthetic representation of the given time through the use of multiple units of time.

write

  • Writes a time value using a Time object and, optionally, configurations that may override those of the Locale and the instance, for the duration of the method.

    This will use the default time unit (which falls back to "nanoseconds") as both input (when time is numeric) and output time unit.

    Parameters

    • time: TimeValueSource | Time

      A numeric representation of a time value -or- an encapsulated nanosecond-based time.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration of the method.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional toTimeUnit: TimeUnitSource

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration of the method.

    Parameters

    • time: Time

      An encapsulated nanosecond-based time.

    • Optional toTimeUnit: string | TimeUnit

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds". Accepts plurals and case variations.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional timeUnit: TimeUnitSource

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional timeUnit: string | TimeUnit

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional fromTimeUnit: TimeUnitSource

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional toTimeUnit: TimeUnitSource

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional fromTimeUnit: string | TimeUnit

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds". Accepts plurals and case variations.

    • Optional toTimeUnit: TimeUnitSource

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional fromTimeUnit: TimeUnitSource

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds".

    • Optional toTimeUnit: string | TimeUnit

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds". Accepts plurals and case variations.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

  • Writes a time value using a Time object and, optionally, a time unit to transform it and configurations that may override those of the Locale and the instance, for the duration the method.

    Parameters

    • timeValue: TimeValueSource

      A numeric representation of a time value.

    • Optional fromTimeUnit: string | TimeUnit

      Time unit from which the given time value will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds". Accepts plurals and case variations.

    • Optional toTimeUnit: string | TimeUnit

      Time unit to which the given time will be converted. If this parameter is not passed, it fallbacks to the default time unit and ultimately to "nanoseconds". Accepts plurals and case variations.

    • Optional options: TimeWriterSettings

      When given, these configurations will override the those of the Locale and the instance, for this specific write. It does not change the object in any way whatsoever.

    Returns string

    A string containing a synthetic representation of the given time.

Generated using TypeDoc