1 | "use strict"
|
2 |
|
3 | class HighResDate extends Date {
|
4 | constructor(...args) {
|
5 | super(...args)
|
6 | this.fraction = 0
|
7 |
|
8 | if (args.length === 1) {
|
9 | this.parseFraction(args[0])
|
10 | }
|
11 | }
|
12 | getStringFraction() {
|
13 | return this.fraction > 0 ? this.fraction.toString() : ""
|
14 | }
|
15 | static equals(value1, value2) {
|
16 | if (value1 instanceof Date && value2 instanceof Date) {
|
17 | if(value1.getTime() === value2.getTime()) {
|
18 | return (value1.fraction || 0) === (value2.fraction || 0)
|
19 | }
|
20 | }
|
21 | return false
|
22 | }
|
23 |
|
24 | parseFraction(date) {
|
25 | if (date instanceof HighResDate) {
|
26 | this.fraction = date.fraction
|
27 | } else if (typeof date === "string") {
|
28 | let fraction = date.match(/\.\d{4,}/)
|
29 | if (fraction) {
|
30 | this.fraction = parseInt(fraction[0].substr(4)) || 0
|
31 | }
|
32 | }
|
33 | }
|
34 |
|
35 | toJSON() {
|
36 | return super.toJSON().replace(/\.(\d+)/, `.$1${this.getStringFraction()}`)
|
37 | }
|
38 |
|
39 | toSQL() {
|
40 | return this.toJSON().replace(/T/, " ").replace(/Z$/, "")
|
41 | }
|
42 | }
|
43 |
|
44 | module.exports = HighResDate
|