All files / src/utils Time.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                              9x             7x                 18x                   3x   3x      
/**
 * @class
 *
 * Clock helpers used by the framework layer.
 *
 * @remarks
 * One place that reads the clock, so timestamps agree across a request: the
 * logger stamps every line, the middleware measures duration, and a service
 * records when a row was written. Some runtimes freeze `Date.now()` between I/O operations, so a duration
 * measured across an `await` is wall-clock time around the I/O rather than CPU
 * time.
 *
 * @example
 * ```ts
 * const start = Time.now()
 * await handler()
 * log.info('done', { durationMs: Time.since(start), at: Time.nowIso() })
 * ```
 *
 * @author Bayu Dwiyan Satria
 * @version 1.0.0
 * @since 1.0.0
 */
export class Time {
  /**
   * Reads the current time in epoch milliseconds.
   *
   * @returns Milliseconds since the epoch.
   */
  public static now(): number {
    return Date.now()
  }
 
  /**
   * Reads the current time as an ISO 8601 string.
   *
   * @returns The timestamp, e.g. `2026-07-26T09:12:44.031Z`.
   */
  public static nowIso(): string {
    return new Date().toISOString()
  }
 
  /**
   * Measures how long has passed since a mark taken with {@link Time.now}.
   *
   * @param start The earlier mark, in epoch milliseconds.
   * @returns Elapsed milliseconds, never negative.
   */
  public static since(start: number): number {
    const elapsed = Time.now() - start
 
    return elapsed > 0 ? elapsed : 0
  }
}