UNPKG

@fluent-org/logger

Version:
84 lines 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * TS/JS representation of the [Fluentd EventTime](https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format) type */ class EventTime { /** * Creates a new EventTime object * @param epoch The epoch (seconds since midnight, Jan 1st, 1970) * @param nano The nano part (epoch + nano = timestamp) */ constructor(epoch, nano) { this._epoch = epoch; this._nano = nano; } /** * The epoch of this EventTime (seconds since midnight, Jan 1st, 1970) */ get epoch() { return this._epoch; } /** * The nano part of this EventTime (epoch + nano = timestamp nanos) */ get nano() { return this._nano; } /** * Packs the `EventTime` into a buffer * @internal * @param eventTime The `EventTime` object to pack * @returns The serialized `EventTime` */ static pack(eventTime) { const b = Buffer.allocUnsafe(8); b.writeUInt32BE(eventTime.epoch, 0); b.writeUInt32BE(eventTime.nano, 4); return b; } /** * Unpacks an `EventTime` from a buffer * @internal * @param buffer The buffer to read the `EventTime` from * @returns The deserialized `EventTime`. */ static unpack(buffer) { const e = buffer.readUInt32BE(0); const n = buffer.readUInt32BE(4); return new EventTime(e, n); } /** * Returns the current timestamp as an `EventTime` * * Similar to `Date.now()` * @returns The EventTime representation of the current timestamp */ static now() { const now = Date.now(); return EventTime.fromTimestamp(now); } /** * Converts a `Date` to an `EventTime`. * * @param date The `Date` object to convert * @returns The equivalent `EventTime`. */ static fromDate(date) { const t = date.getTime(); return EventTime.fromTimestamp(t); } /** * Creates a new `EventTime` from a numeric timestamp * * @param t The numeric timestamp to convert to an EventTime * @returns The EventTime representation of the timestamp */ static fromTimestamp(t) { const epoch = Math.floor(t / 1000); const nano = (t % 1000) * 1000000; return new EventTime(epoch, nano); } } exports.default = EventTime; //# sourceMappingURL=event_time.js.map