Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/library/dayjs/plugins/utc.js
12923 views
1
import { MILLISECONDS_A_MINUTE, MIN } from "./constant.js";
2
3
const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g;
4
const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g;
5
6
function offsetFromString(value = "") {
7
const offset = value.match(REGEX_VALID_OFFSET_FORMAT);
8
9
if (!offset) {
10
return null;
11
}
12
13
const [indicator, hoursOffset, minutesOffset] = `${offset[0]}`.match(
14
REGEX_OFFSET_HOURS_MINUTES_FORMAT
15
) || ["-", 0, 0];
16
const totalOffsetInMinutes = +hoursOffset * 60 + +minutesOffset;
17
18
if (totalOffsetInMinutes === 0) {
19
return 0;
20
}
21
22
return indicator === "+" ? totalOffsetInMinutes : -totalOffsetInMinutes;
23
}
24
25
export default (option, Dayjs, dayjs) => {
26
const proto = Dayjs.prototype;
27
dayjs.utc = function (date) {
28
const cfg = { date, utc: true, args: arguments }; // eslint-disable-line prefer-rest-params
29
return new Dayjs(cfg); // eslint-disable-line no-use-before-define
30
};
31
32
proto.utc = function (keepLocalTime) {
33
const ins = dayjs(this.toDate(), { locale: this.$L, utc: true });
34
if (keepLocalTime) {
35
return ins.add(this.utcOffset(), MIN);
36
}
37
return ins;
38
};
39
40
proto.local = function () {
41
return dayjs(this.toDate(), { locale: this.$L, utc: false });
42
};
43
44
const oldParse = proto.parse;
45
proto.parse = function (cfg) {
46
if (cfg.utc) {
47
this.$u = true;
48
}
49
if (!this.$utils().u(cfg.$offset)) {
50
this.$offset = cfg.$offset;
51
}
52
oldParse.call(this, cfg);
53
};
54
55
const oldInit = proto.init;
56
proto.init = function () {
57
if (this.$u) {
58
const { $d } = this;
59
this.$y = $d.getUTCFullYear();
60
this.$M = $d.getUTCMonth();
61
this.$D = $d.getUTCDate();
62
this.$W = $d.getUTCDay();
63
this.$H = $d.getUTCHours();
64
this.$m = $d.getUTCMinutes();
65
this.$s = $d.getUTCSeconds();
66
this.$ms = $d.getUTCMilliseconds();
67
} else {
68
oldInit.call(this);
69
}
70
};
71
72
const oldUtcOffset = proto.utcOffset;
73
proto.utcOffset = function (input, keepLocalTime) {
74
const { u } = this.$utils();
75
if (u(input)) {
76
if (this.$u) {
77
return 0;
78
}
79
if (!u(this.$offset)) {
80
return this.$offset;
81
}
82
return oldUtcOffset.call(this);
83
}
84
if (typeof input === "string") {
85
input = offsetFromString(input);
86
if (input === null) {
87
return this;
88
}
89
}
90
const offset = Math.abs(input) <= 16 ? input * 60 : input;
91
let ins = this;
92
if (keepLocalTime) {
93
ins.$offset = offset;
94
ins.$u = input === 0;
95
return ins;
96
}
97
if (input !== 0) {
98
const localTimezoneOffset = this.$u
99
? this.toDate().getTimezoneOffset()
100
: -1 * this.utcOffset();
101
ins = this.local().add(offset + localTimezoneOffset, MIN);
102
ins.$offset = offset;
103
ins.$x.$localOffset = localTimezoneOffset;
104
} else {
105
ins = this.utc();
106
}
107
return ins;
108
};
109
110
const oldFormat = proto.format;
111
const UTC_FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ss[Z]";
112
proto.format = function (formatStr) {
113
const str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : "");
114
return oldFormat.call(this, str);
115
};
116
117
proto.valueOf = function () {
118
const addedOffset = !this.$utils().u(this.$offset)
119
? this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset())
120
: 0;
121
return this.$d.valueOf() - addedOffset * MILLISECONDS_A_MINUTE;
122
};
123
124
proto.isUTC = function () {
125
return !!this.$u;
126
};
127
128
proto.toISOString = function () {
129
return this.toDate().toISOString();
130
};
131
132
proto.toString = function () {
133
return this.toDate().toUTCString();
134
};
135
136
const oldToDate = proto.toDate;
137
proto.toDate = function (type) {
138
if (type === "s" && this.$offset) {
139
return dayjs(this.format("YYYY-MM-DD HH:mm:ss:SSS")).toDate();
140
}
141
return oldToDate.call(this);
142
};
143
const oldDiff = proto.diff;
144
proto.diff = function (input, units, float) {
145
if (input && this.$u === input.$u) {
146
return oldDiff.call(this, input, units, float);
147
}
148
const localThis = this.local();
149
const localInput = dayjs(input).local();
150
return oldDiff.call(localThis, localInput, units, float);
151
};
152
};
153
154