Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface TimeWriterSettings

Objects of this type are used to configure the behavior of time writers.

Using these objects it is possible to change the lexical and numerical output to fit many different scenarios. See the documentation for each property for more information.

Hierarchy

  • TimeWriterSettings

Index

Properties

Optional decimalPlaces

decimalPlaces: undefined | number

Numer of decimal places resulting from time conversions. Used in conjunction with [roundingMode] to write numeric values

Default: undefined.


const timeWriter = new TimeWriter();

timeWriter.write(Math.PI, "second");
// 3.1415926536 s

timeWriter.write(Math.PI, "second", { decimalPlaces: 4 });
// 3.1416 s

timeWriter.write(1, "year", { decimalPlaces: 4 });
// 1.0000 y

timeWriter.countdown(Time.from("2", "siderealMonth"), ["minute", "second", "millisecond"])
// ≈78686 min, 22 s, 848 ms

timeWriter.countdown(Time.from("2", "siderealMonth"), { decimalPlaces: 4 }, ["minute", "second", "millisecond"])
// ≈78686.0000 min, 22.0000 s, 848.0000 ms

Back to top

Optional decimalSeparator

decimalSeparator: undefined | string

String used as decimal separator, specially useful when translating to languages and regions where the "dot" notation is not standard.

If not set, time writers will first try to parse the decimal separator for the locale of the machine where the script is running and will ultimately fallback to ".".


timeWriter.write(3.5, "megasecond");
// 3.5 Ms

timeWriter.write(3.5, "megasecond", { decimalSeparator: "," });
// 3,5 Ms

timeWriter.countdown(Time.from(500, "day"), ["year", "month", "day", "hour"]);
// 1 y, 4 m, 13 d, 2.18 h

timeWriter.countdown(Time.from(500, "day"), { decimalSeparator: "point" }, ["year", "month", "day", "hour"]);
// 1 y, 4 m, 13 d, 2point18 h

Back to top

Optional defaultTimeUnit

defaultTimeUnit: TimeUnitSource

Specifies the default unit to convert to when writing time values.

Default is nanosecond.


let timeWriter = new TimeWriter();

timeWriter.write(100);
// 100 ns

timeWriter = new TimeWriter({ defaultTimeUnit: "microsecond" });

timeWriter.write(100);
// 100 µs

timeWriter.write(100, { defaultTimeUnit: "second" });
// 100 s

Back to top

Optional fractionDigits

fractionDigits: undefined | number

This property is no longer supported! When given, this will be used as the fixed number of decimal digits.

deprecated

Since v1.1.0 - In favor of significantDigits (will be removed in v2).

Optional hideTimeUnit

hideTimeUnit: undefined | false | true

When this property is set to true, it will prevent the time writer from writing any form of time unit, be it plural, symbol or verbose. It will also prevent the output of the time unit separator.

Default is false.


timeWriter.write(100);
// 100 ns

timeWriter.countdown(Time.from(5.123456789, "second"), TimeSegments.baseTen);
// 5 s, 123 ms, 456 µs, 789 ns

timeWriter.write(100, { hideTimeUnit: true });
// 100

timeWriter.countdown(Time.from(5.123456789, "second"), { hideTimeUnit: true }, TimeSegments.baseTen);
// 5, 123, 456, 789

Back to top

Optional hideZeroSegments

hideZeroSegments: undefined | false | true

Whether to hide countdown segments with a value of 0 (zero).

Default is true.


import { TimeSegments } from "timecount";

timeWriter.countdown(Time.from(5.123456789, "second"), TimeSegments.baseTen);
// 5 s, 123 ms, 456 µs, 789 ns

timeWriter.countdown(Time.from(5.123456789, "second"), { hideZeroSegments: false }, TimeSegments.baseTen);
// 0 Ys, 0 Zs, 0 Es, 0 Ps, 0 Ts, 0 Gs, 0 Ms, 0 Ks, 5 s, 123 ms, 456 µs, 789 ns, 0 ps, 0 fs, 0 as, 0 zs, 0 ys

Back to top

Optional numericNotation

numericNotation: NumericNotation

Numeric notation used to write time values. Accepted values are:

  • "decimal": standard Hindu–Arabic numeral system using base ten;
  • "roman": Ancient Rome numeral system, ignoring fractions;
  • "roman-fractions": Ancient Rome numeral system, using common fractions;
  • "scientific": Exponent notation; writes very small and very large numbers using powers of 10.

Default is "decimal".


timeWriter.write(12345.0125);
// 12345.0125 ns

timeWriter.write(12345.0125, { numericNotation: "decimal" });
// 12345.0125 ns

timeWriter.write(12345.0125, { numericNotation: "scientific" });
// 1.23450125e+4 ns

timeWriter.write(12345.0125, { numericNotation: "roman" });
// X̅MMCCCXLV ns

timeWriter.write(12345.0125, { numericNotation: "roman-fractions" });
// X̅MMCCCXLVƧ ns

timeWriter.countdown(new Time(9.065e16), { numericNotation: "scientific" }, ["year", "day", "hour", "minute", "second"]);
// 2e+0 y, 3.18e+2 d, 1.6e+1 h, 5.4e+1 min, 5.6e+1 s

timeWriter.countdown(new Time(9.065e16), { numericNotation: "roman-fractions" }, ["year", "day", "hour", "minute", "second"]);
// II y, CCCXVIII d, XVI h, LIV min, LVS:·: s

Back to top

Optional numericWriter

numericWriter: NumericWriter

This can be set to a function that will replace all other forms of numeric writing, ignoring the numeric notation and other mathematical properties.

This function may receive two parameters: a number and an optional big decimal.

Default is undefined.


function myWriter(value: number) {
    switch (value) {
        case 3: return "three";
        case 4: return "four";
        case 5: return "five";
    }

    return value.toString();
}

timeWriter.countdown(Time.from(11045, "second"));
// 3 h, 4 min, 5 s

timeWriter.countdown(Time.from(11045, "second"), { numericWriter: myWriter });
// three h, four min, five s

timeWriter.countdown(Time.from(11045, "second"), { numericWriter: myWriter, verbose: true });
// three hours, four minutes, five seconds

Back to top

Optional roundingMode

roundingMode: RoundingMode

Numeric rounding mode used by the writer.

Default is RoundingMode.RoundHalfUp or 4.


const timeWriter = new TimeWriter();

timeWriter.write(Math.PI, "second");
// 3.1415926536 s

timeWriter.write(Math.PI, "second", { decimalPlaces: 4 });
// 3.1416 s

timeWriter.write(Math.PI, "second", { decimalPlaces: 4, roundindMode: RoundingMode.RoundDown });
// 3.1415 s

timeWriter.write(Math.PI, "second", { decimalPlaces: 4, roundindMode: 2 });
// 3.1416 s

Back to top

Optional segmentSeparator

segmentSeparator: undefined | string

A string used to separate countdown segments.

Default is ", " (comma and space).


import { TimeSegments } from "timecount";

timeWriter.countdown(Time.from(2.10066102, "second"), "second", "millisecond", "microsecond");
// 2 s, 100 ms, 661.20 µs

timeWriter.countdown(Time.from(2.10066102, "second"), { segmentSeparator: "; " }, "second", "millisecond", "microsecond");
// 2 s; 100 ms; 661.20 µs

timeWriter.countdown(Time.from(2.10066102, "second"), TimeSegments.baseTen);
// 2 s, 100 ms, 661 µs, 20 ns

timeWriter.countdown(Time.from(2.10066102, "second"), { segmentSeparator: " + " }, TimeSegments.baseTen);
// 2 s + 100 ms + 661 µs + 20 ns

timeWriter.countdown(Time.from(2.10066102, "second"), { hideZeroSegments: false, segmentSeparator: " + " }, TimeSegments.baseTen);
// 0 Ys + 0 Zs + 0 Es + 0 Ps + 0 Ts + 0 Gs + 0 Ms + 0 Ks + 2 s + 100 ms + 661 µs + 20 ns + 0 ps + 0 fs + 0 as + 0 zs + 0 ys

Back to top

Optional significantDigits

significantDigits: undefined | number

Number of significant digits of the decimal part of numeric values, intended to minimize the quantity of written numbers for very large conversions.

It will enforce a precision of the specified magnitude, but only for the decimal part of the number, unlike decimal.js precision. It doesn't apply to leading zeros.

Default: 10.


import { Decimal } from "decimal.js";

const pi = Decimal.acos(-1);
const timeWriter = new TimeWriter({ significantDigits: 99 });

timeWriter.write(pi, "second");
// 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 s

timeWriter.write(pi, "second", { significantDigits: 4 });
// 3.1416 s

timeWriter.write(pi, "second", { decimalPlaces: 10, significantDigits: 4 });
// 3.1416000000 s

Back to top

Optional spaceTimeUnit

spaceTimeUnit: undefined | false | true

This property is no longer supported! Whether to separate the numeric value from the time unit.

deprecated

Since v1.1.0 - In favor of timeUnitSeparator (will be removed in v2).

Optional symbolApproximately

symbolApproximately: undefined | string

When writing time values or units which based on approximations, this symbol will be added before the number to demonstrate the approximation, when verbose is disabled. Otherwise, its term is used instead.

Default: "≈" (almost equal to symbol).


timeWriter.write(Time.from(1, "year"), "tropicalYear");
// ≈1.000000697152733 tropical years

timeWriter.write(Time.from(1, "year"), "tropicalYear", { symbolApproximately: "~" });
// ~1.000000697152733 tropical years

timeWriter.write(Time.from(1, "year"), "tropicalYear", { symbolApproximately: "~", verbose: true });
// approximately 1.000000697152733 tropical years

Back to top

Optional symbolInfinite

symbolInfinite: undefined | string

When writing time values that represent an Infinity, this symbol will be used to express it, when verbose is disabled. Otherwise, its term is used instead.

Default: "∞".


timeWriter.write(Time.from(Infinity, "year"));
// ∞ y

timeWriter.write(Time.from(Infinity, "year"), { symbolInfinite: "<INF>" });
// <INF> y

timeWriter.write(Time.from(Infinity, "year").approximate(), { symbolInfinite: "<INF>" });
// ≈<INF> y

timeWriter.write(Time.from(Infinity, "year"), { symbolInfinite: "<INF>", verbose: true });
// infinite years

Back to top

Optional symbolNaN

symbolNaN: undefined | string

When writing time values that represent a NaN (not a number), this symbol will be used to express it, when verbose is disabled. Otherwise, its term is used instead.

Default: "NaN".


timeWriter.write(Time.from(NaN, "cosmicYear"));
// ≈NaN cosmic years

timeWriter.write(Time.from(NaN, "cosmicYear"), { symbolNaN: "ɳɑɲ" });
// ≈ɳɑɲ cosmic years

timeWriter.write(NaN, "second", { symbolNaN: "ɳɑɲ" });
// ɳɑɲ s

timeWriter.write(Time.from(NaN, "cosmicYear"), { symbolNaN: "ɳɑɲ", verbose: true });
// invalid number of cosmic years

Back to top

Optional termApproximately

termApproximately: undefined | string

When writing time values or units which based on approximations, this string will be added before the number to demonstrate the approximation, when verbose is enabled. Otherwise, its symbol is used instead.

Default for English (US): "approximately".


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

timeWriter.write(Time.from(1, "year"), "draconicMonth");
// approximately 13.422003056 draconic months

timeWriter.write(Time.from(1, "year"), "draconicMonth", { termApproximately: "more or less" });
// more or less 13.422003056 draconic months

timeWriter.write(Time.from(1, "year"), "draconicMonth", { termApproximately: "more or less", verbose: false });
// ≈13.422003056 draconic months

Back to top

Optional termInfinite

termInfinite: undefined | string

When writing time values that represent an Infinity, this string will be used to express it, when verbose is enabled. Otherwise, its symbol is used instead.

Default for English (US): "infinite".


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

timeWriter.write(Infinity, "month");
// infinite months

timeWriter.write(Infinity, "month", { termInfinite: "infinite number of" });
// infinite number of months

timeWriter.write(Infinity, "month", { termInfinite: "infinite number of", verbose: false });
// ∞ m

Back to top

Optional termNaN

termNaN: undefined | string

When writing time values that represent a NaN (not a number), this string will be used to express it, when verbose is enabled. Otherwise, its symbol is used instead.


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

timeWriter.write(NaN, "day");
// invalid number of days

timeWriter.write(NaN, "day", { termNaN: "not a number of" });
// not a number of days

timeWriter.write(NaN, "day", { termNaN: "not a number of", verbose: false });
// NaN d

Back to top

Optional terms

terms: undefined | { approximately?: undefined | string; infinite?: undefined | string; nan?: undefined | string }

This property is no longer supported! This was used to define a few special numeric terms.

deprecated

Since v1.1.0 - In favor of verbose (will be removed in v2).

Optional thousandsSeparator

thousandsSeparator: undefined | string

A string used as the thousands separator when using decimal numeric notations.

Default is undefined.


Optional timeUnitSeparator

timeUnitSeparator: undefined | string

A string that separates the numeric value from the time unit.

Default is " " (space).


Optional verbose

verbose: undefined | false | true

Whether to write more sentences to describe a time value instead of using symbols. Setting this to true enforce descriptive text for time units

All time units without a symbol are already written in verbose mode.

Default is false.


import { TimeSegments } from "timecount";

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

timeWriter.countdown(Time.from(1.4e+3, "month"));
// 116 years, 7 months, 2 days, 6 hours, 52 minutes, 48 seconds

timeWriter.countdown(Time.from(1.4e+3, "month"), { verbose: false });
// 116 y, 7 m, 2 d, 6 h, 52 m, 48 s

timeWriter.write(new Time(Infinity, true), "biennium");
// approximately infinite biennia

timeWriter.write(new Time(Infinity, true), "biennium", { verbose: false });
// ≈∞ biennia

Back to top

Optional verboseTimeUnit

verboseTimeUnit: undefined | false | true

This property is no longer supported! Whether or not to write down the full unit readable name, instead of its symbol.

deprecated

Since v1.1.0 - In favor of verbose (will be removed in v2).

Generated using TypeDoc