/
event_time.ts
91 lines (83 loc) · 2.33 KB
/
event_time.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* TS/JS representation of the [Fluentd EventTime](https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format) type
*/
class EventTime {
/**
* The epoch of this EventTime (seconds since midnight, Jan 1st, 1970)
*/
public get epoch(): number {
return this._epoch;
}
/**
* The nano part of this EventTime (epoch + nano = timestamp nanos)
*/
public get nano(): number {
return this._nano;
}
private _epoch: number;
private _nano: number;
/**
* 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: number, nano: number) {
this._epoch = epoch;
this._nano = nano;
}
/**
* Packs the `EventTime` into a buffer
* @internal
* @param eventTime The `EventTime` object to pack
* @returns The serialized `EventTime`
*/
static pack(eventTime: EventTime): Buffer {
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: Buffer): EventTime {
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(): EventTime {
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: Date): EventTime {
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: number): EventTime {
const epoch = Math.floor(t / 1000);
const nano = (t % 1000) * 1000000;
return new EventTime(epoch, nano);
}
}
export default EventTime;