Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PGTime

A utility class to simplify handling timestamps generated from Postgres. Provides helpers for time comparisons, and formatting.

Hierarchy

  • PGTime

Index

Constructors

constructor

  • new PGTime(timestamp: string): PGTime
  • Parses a string timestamp to create a PGTime instance. The timestamp should be in 24-hour format, with

    • One or two digits in the hour
    • Optionally two digits in the minute
    • Optionally two digits in the second
    • Optionally one to three digits in the millisecond

    e.g., the string "13" will be treated the same as "13:00:00.000"

    Constructor will throw a TypeError if provided with a bad timestamp

    Parameters

    • timestamp: string

    Returns PGTime

Properties

hour

hour: number

The hour part of the timestamp

millisecond

millisecond: number

The millisecond part of the timestamp

minute

minute: number

The minute part of the timestamp

Private regex

regex: RegExp = /^(?<hour>[01]?[0-9]|2[0-3])(:(?<minute>[0-5][0-9])(:(?<second>[0-5][0-9])(\.(?<millisecond>[0-9][0-9]?[0-9]?)?)?)?)?$/

A static RegExp that matches a wide range of 24-hour HH:MM:SS.mmm formatted timestamps

second

second: number

The second part of the timestamp

Static strictRegex

strictRegex: RegExp = /^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9](\.[0-9]{3})?)?$/

A regex that strictly matches against the postgres format, for validation

Accessors

displayHour

  • get displayHour(): string

displayTime

  • get displayTime(): string

hourHeading

  • get hourHeading(): string

inputString

  • get inputString(): string

meridian

msSinceMidnight

  • get msSinceMidnight(): number
  • Converts the time into the number of milliseconds that have elapsed since midnight before. Used for comparisons.

    Returns number

Methods

isAfter

  • isAfter(compTime: PGTime): boolean

isBefore

  • isBefore(compTime: PGTime): boolean

isSameAs

  • isSameAs(compTime: PGTime): boolean

toString

  • toString(): string

Static fromDisplay

  • fromDisplay(ampmTimestamp: string): PGTime
  • Attempts to parse an AM/PM style timestamp into a PGTime instance. This may not cover every possible format, but should hit most of our use cases.

    Parameters

    • ampmTimestamp: string

    Returns PGTime

Static toDisplay

  • toDisplay(timestamp: string): string
  • Static method to convert a given timestamp to Display format (i.e. 12 hour AM/PM time). It's just a wrapper around

    new PGTime(<timestamp>).displayTime

    but it returns null for falsy values (e.g. null, undefined, ''), where the constructor would throw an error

    Parameters

    • timestamp: string

    Returns string

Generated using TypeDoc