fake-current-time 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -14
- package/dist/browser.js +6 -28
- package/dist/chunk-2QPID2IG.js +35 -0
- package/dist/react.d.ts +5 -0
- package/dist/react.js +265 -0
- package/package.json +17 -2
package/README.md
CHANGED
|
@@ -68,25 +68,15 @@ startTransition(() => {
|
|
|
68
68
|
### UI Component
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
|
-
import {
|
|
71
|
+
import { FakeTimeController } from "fake-current-time/react";
|
|
72
72
|
|
|
73
73
|
// Restrict access to this page in production (e.g., via routing or middleware)
|
|
74
|
-
export function
|
|
75
|
-
return
|
|
76
|
-
<div>
|
|
77
|
-
<p>Current: {new Date().toString()}</p>
|
|
78
|
-
{/* setOffset saves to cookie and reloads the page */}
|
|
79
|
-
<button onClick={() => setOffset({ days: 1 })}>+1 Day</button>
|
|
80
|
-
<button onClick={() => setOffset({ days: -1 })}>-1 Day</button>
|
|
81
|
-
<button onClick={() => setOffset({ months: 3, days: 7 })}>+3 Months +7 Days</button>
|
|
82
|
-
{/* clearOffset removes cookie and reloads the page */}
|
|
83
|
-
<button onClick={clearOffset}>Reset</button>
|
|
84
|
-
</div>
|
|
85
|
-
);
|
|
74
|
+
export function TimeControlPage() {
|
|
75
|
+
return <FakeTimeController />;
|
|
86
76
|
}
|
|
87
77
|
```
|
|
88
78
|
|
|
89
|
-
|
|
79
|
+

|
|
90
80
|
|
|
91
81
|
### Accessing the Original Date
|
|
92
82
|
|
package/dist/browser.js
CHANGED
|
@@ -1,32 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
} from "./chunk-XDOQVLFM.js";
|
|
9
|
-
|
|
10
|
-
// src/browser.ts
|
|
11
|
-
var initialized = false;
|
|
12
|
-
function setup() {
|
|
13
|
-
if (initialized) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
setupDateProxy(getOffset);
|
|
17
|
-
initialized = true;
|
|
18
|
-
}
|
|
19
|
-
function setOffset(offset) {
|
|
20
|
-
document.cookie = `${COOKIE_NAME}=${encodeOffset(offset)}; path=/`;
|
|
21
|
-
location.reload();
|
|
22
|
-
}
|
|
23
|
-
function clearOffset() {
|
|
24
|
-
document.cookie = `${COOKIE_NAME}=; path=/; max-age=0`;
|
|
25
|
-
location.reload();
|
|
26
|
-
}
|
|
27
|
-
function getOffset() {
|
|
28
|
-
return parseOffsetFromCookie(document.cookie);
|
|
29
|
-
}
|
|
2
|
+
clearOffset,
|
|
3
|
+
setOffset,
|
|
4
|
+
setup
|
|
5
|
+
} from "./chunk-2QPID2IG.js";
|
|
6
|
+
import "./chunk-Z7IHRBMK.js";
|
|
7
|
+
import "./chunk-XDOQVLFM.js";
|
|
30
8
|
export {
|
|
31
9
|
clearOffset,
|
|
32
10
|
setOffset,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COOKIE_NAME,
|
|
3
|
+
encodeOffset,
|
|
4
|
+
parseOffsetFromCookie
|
|
5
|
+
} from "./chunk-Z7IHRBMK.js";
|
|
6
|
+
import {
|
|
7
|
+
setupDateProxy
|
|
8
|
+
} from "./chunk-XDOQVLFM.js";
|
|
9
|
+
|
|
10
|
+
// src/browser.ts
|
|
11
|
+
var initialized = false;
|
|
12
|
+
function setup() {
|
|
13
|
+
if (initialized) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
setupDateProxy(getOffset);
|
|
17
|
+
initialized = true;
|
|
18
|
+
}
|
|
19
|
+
function setOffset(offset) {
|
|
20
|
+
document.cookie = `${COOKIE_NAME}=${encodeOffset(offset)}; path=/`;
|
|
21
|
+
location.reload();
|
|
22
|
+
}
|
|
23
|
+
function clearOffset() {
|
|
24
|
+
document.cookie = `${COOKIE_NAME}=; path=/; max-age=0`;
|
|
25
|
+
location.reload();
|
|
26
|
+
}
|
|
27
|
+
function getOffset() {
|
|
28
|
+
return parseOffsetFromCookie(document.cookie);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
setup,
|
|
33
|
+
setOffset,
|
|
34
|
+
clearOffset
|
|
35
|
+
};
|
package/dist/react.d.ts
ADDED
package/dist/react.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clearOffset,
|
|
3
|
+
setOffset
|
|
4
|
+
} from "./chunk-2QPID2IG.js";
|
|
5
|
+
import {
|
|
6
|
+
parseOffsetFromCookie
|
|
7
|
+
} from "./chunk-Z7IHRBMK.js";
|
|
8
|
+
import {
|
|
9
|
+
OriginalDate
|
|
10
|
+
} from "./chunk-XDOQVLFM.js";
|
|
11
|
+
|
|
12
|
+
// src/react.tsx
|
|
13
|
+
import { useEffect, useState } from "react";
|
|
14
|
+
|
|
15
|
+
// src/react.styles.ts
|
|
16
|
+
var timeDisplayContainer = {
|
|
17
|
+
marginBottom: "16px",
|
|
18
|
+
padding: "12px",
|
|
19
|
+
borderRadius: "4px",
|
|
20
|
+
border: "1px solid #e0e0e0"
|
|
21
|
+
};
|
|
22
|
+
var timeDisplayValue = {
|
|
23
|
+
fontSize: "20px",
|
|
24
|
+
fontFamily: "monospace"
|
|
25
|
+
};
|
|
26
|
+
var buttonBase = {
|
|
27
|
+
flex: 1,
|
|
28
|
+
padding: "10px 16px",
|
|
29
|
+
border: "none",
|
|
30
|
+
borderRadius: "4px",
|
|
31
|
+
fontSize: "14px",
|
|
32
|
+
fontWeight: "500",
|
|
33
|
+
cursor: "pointer"
|
|
34
|
+
};
|
|
35
|
+
var styles = {
|
|
36
|
+
container: {
|
|
37
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
38
|
+
fontSize: "14px",
|
|
39
|
+
padding: "16px",
|
|
40
|
+
border: "1px solid #ccc",
|
|
41
|
+
borderRadius: "8px",
|
|
42
|
+
backgroundColor: "#f9f9f9",
|
|
43
|
+
maxWidth: "400px"
|
|
44
|
+
},
|
|
45
|
+
header: {
|
|
46
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
47
|
+
margin: "0 0 12px 0",
|
|
48
|
+
fontSize: "16px",
|
|
49
|
+
fontWeight: "600",
|
|
50
|
+
color: "#333"
|
|
51
|
+
},
|
|
52
|
+
timeDisplay: {
|
|
53
|
+
current: {
|
|
54
|
+
container: { ...timeDisplayContainer, backgroundColor: "#fff" },
|
|
55
|
+
value: { ...timeDisplayValue, color: "#333" }
|
|
56
|
+
},
|
|
57
|
+
real: {
|
|
58
|
+
container: { ...timeDisplayContainer, backgroundColor: "#f5f5f5" },
|
|
59
|
+
value: { ...timeDisplayValue, color: "#666" }
|
|
60
|
+
},
|
|
61
|
+
label: {
|
|
62
|
+
fontSize: "12px",
|
|
63
|
+
color: "#666",
|
|
64
|
+
marginBottom: "4px"
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
offset: {
|
|
68
|
+
container: {
|
|
69
|
+
marginBottom: "16px",
|
|
70
|
+
padding: "8px 12px",
|
|
71
|
+
backgroundColor: "#e8f4fd",
|
|
72
|
+
borderRadius: "4px",
|
|
73
|
+
fontSize: "13px"
|
|
74
|
+
},
|
|
75
|
+
label: {
|
|
76
|
+
fontWeight: "600",
|
|
77
|
+
color: "#555"
|
|
78
|
+
},
|
|
79
|
+
value: {
|
|
80
|
+
marginLeft: "8px",
|
|
81
|
+
color: "#1a73e8"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
form: {
|
|
85
|
+
fieldGroup: {
|
|
86
|
+
display: "grid",
|
|
87
|
+
gridTemplateColumns: "repeat(3, 1fr)",
|
|
88
|
+
gap: "12px",
|
|
89
|
+
marginBottom: "16px"
|
|
90
|
+
},
|
|
91
|
+
field: {
|
|
92
|
+
display: "flex",
|
|
93
|
+
flexDirection: "column"
|
|
94
|
+
},
|
|
95
|
+
label: {
|
|
96
|
+
fontSize: "12px",
|
|
97
|
+
color: "#666",
|
|
98
|
+
marginBottom: "4px"
|
|
99
|
+
},
|
|
100
|
+
input: {
|
|
101
|
+
padding: "8px",
|
|
102
|
+
border: "1px solid #ddd",
|
|
103
|
+
borderRadius: "4px",
|
|
104
|
+
fontSize: "14px",
|
|
105
|
+
width: "100%",
|
|
106
|
+
boxSizing: "border-box"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
buttons: {
|
|
110
|
+
group: {
|
|
111
|
+
display: "flex",
|
|
112
|
+
gap: "8px"
|
|
113
|
+
},
|
|
114
|
+
apply: { ...buttonBase, backgroundColor: "#1a73e8", color: "#fff" },
|
|
115
|
+
clear: { ...buttonBase, backgroundColor: "#f1f3f4", color: "#333" }
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// src/react.tsx
|
|
120
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
121
|
+
var FIELDS = [
|
|
122
|
+
"years",
|
|
123
|
+
"months",
|
|
124
|
+
"days",
|
|
125
|
+
"hours",
|
|
126
|
+
"minutes",
|
|
127
|
+
"seconds"
|
|
128
|
+
];
|
|
129
|
+
var FIELD_LABELS = {
|
|
130
|
+
years: "Years",
|
|
131
|
+
months: "Months",
|
|
132
|
+
days: "Days",
|
|
133
|
+
hours: "Hours",
|
|
134
|
+
minutes: "Minutes",
|
|
135
|
+
seconds: "Seconds"
|
|
136
|
+
};
|
|
137
|
+
function getCurrentOffset() {
|
|
138
|
+
if (typeof document === "undefined") {
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
return parseOffsetFromCookie(document.cookie);
|
|
142
|
+
}
|
|
143
|
+
function formatCurrentOffset(offset) {
|
|
144
|
+
if (!offset) {
|
|
145
|
+
return "None";
|
|
146
|
+
}
|
|
147
|
+
const parts = [];
|
|
148
|
+
for (const field of FIELDS) {
|
|
149
|
+
const value = offset[field];
|
|
150
|
+
if (value !== void 0 && value !== 0) {
|
|
151
|
+
const sign = value > 0 ? "+" : "";
|
|
152
|
+
parts.push(`${sign}${value} ${field}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (offset.milliseconds !== void 0 && offset.milliseconds !== 0) {
|
|
156
|
+
const sign = offset.milliseconds > 0 ? "+" : "";
|
|
157
|
+
parts.push(`${sign}${offset.milliseconds} milliseconds`);
|
|
158
|
+
}
|
|
159
|
+
return parts.length > 0 ? parts.join(", ") : "None";
|
|
160
|
+
}
|
|
161
|
+
function FakeTimeController() {
|
|
162
|
+
const [values, setValues] = useState({
|
|
163
|
+
years: "",
|
|
164
|
+
months: "",
|
|
165
|
+
days: "",
|
|
166
|
+
hours: "",
|
|
167
|
+
minutes: "",
|
|
168
|
+
seconds: ""
|
|
169
|
+
});
|
|
170
|
+
const [currentTime, setCurrentTime] = useState(null);
|
|
171
|
+
const [realTime, setRealTime] = useState(null);
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
setCurrentTime(/* @__PURE__ */ new Date());
|
|
174
|
+
setRealTime(new OriginalDate());
|
|
175
|
+
const interval = setInterval(() => {
|
|
176
|
+
setCurrentTime(/* @__PURE__ */ new Date());
|
|
177
|
+
setRealTime(new OriginalDate());
|
|
178
|
+
}, 1e3);
|
|
179
|
+
return () => clearInterval(interval);
|
|
180
|
+
}, []);
|
|
181
|
+
const currentOffset = getCurrentOffset();
|
|
182
|
+
const handleFieldChange = (e) => {
|
|
183
|
+
const { name, value } = e.target;
|
|
184
|
+
setValues((prev) => ({ ...prev, [name]: value }));
|
|
185
|
+
};
|
|
186
|
+
const handleApply = () => {
|
|
187
|
+
const offset = {};
|
|
188
|
+
for (const field of FIELDS) {
|
|
189
|
+
const value = values[field];
|
|
190
|
+
if (value !== "") {
|
|
191
|
+
const num = Number.parseInt(value, 10);
|
|
192
|
+
if (!Number.isNaN(num) && num !== 0) {
|
|
193
|
+
offset[field] = num;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (Object.keys(offset).length > 0) {
|
|
198
|
+
setOffset(offset);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
return /* @__PURE__ */ jsxs("div", { style: styles.container, children: [
|
|
202
|
+
/* @__PURE__ */ jsx("h3", { style: styles.header, children: "Fake Time Controller" }),
|
|
203
|
+
/* @__PURE__ */ jsx(
|
|
204
|
+
TimeDisplay,
|
|
205
|
+
{
|
|
206
|
+
label: "Current Time (with offset applied)",
|
|
207
|
+
time: currentTime
|
|
208
|
+
}
|
|
209
|
+
),
|
|
210
|
+
/* @__PURE__ */ jsx(
|
|
211
|
+
TimeDisplay,
|
|
212
|
+
{
|
|
213
|
+
label: "Real Time (original)",
|
|
214
|
+
time: realTime,
|
|
215
|
+
variant: "real"
|
|
216
|
+
}
|
|
217
|
+
),
|
|
218
|
+
/* @__PURE__ */ jsxs("div", { style: styles.offset.container, children: [
|
|
219
|
+
/* @__PURE__ */ jsx("span", { style: styles.offset.label, children: "Current offset:" }),
|
|
220
|
+
/* @__PURE__ */ jsx("span", { style: styles.offset.value, children: formatCurrentOffset(currentOffset) })
|
|
221
|
+
] }),
|
|
222
|
+
/* @__PURE__ */ jsxs("form", { action: handleApply, children: [
|
|
223
|
+
/* @__PURE__ */ jsx("div", { style: styles.form.fieldGroup, children: FIELDS.map((field) => /* @__PURE__ */ jsxs("label", { style: styles.form.field, children: [
|
|
224
|
+
/* @__PURE__ */ jsx("span", { style: styles.form.label, children: FIELD_LABELS[field] }),
|
|
225
|
+
/* @__PURE__ */ jsx(
|
|
226
|
+
"input",
|
|
227
|
+
{
|
|
228
|
+
type: "number",
|
|
229
|
+
name: field,
|
|
230
|
+
value: values[field],
|
|
231
|
+
onChange: handleFieldChange,
|
|
232
|
+
placeholder: "0",
|
|
233
|
+
style: styles.form.input
|
|
234
|
+
}
|
|
235
|
+
)
|
|
236
|
+
] }, field)) }),
|
|
237
|
+
/* @__PURE__ */ jsxs("div", { style: styles.buttons.group, children: [
|
|
238
|
+
/* @__PURE__ */ jsx("button", { type: "submit", style: styles.buttons.apply, children: "Apply" }),
|
|
239
|
+
/* @__PURE__ */ jsx(
|
|
240
|
+
"button",
|
|
241
|
+
{
|
|
242
|
+
type: "button",
|
|
243
|
+
onClick: clearOffset,
|
|
244
|
+
style: styles.buttons.clear,
|
|
245
|
+
children: "Clear"
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
] })
|
|
249
|
+
] })
|
|
250
|
+
] });
|
|
251
|
+
}
|
|
252
|
+
function TimeDisplay({
|
|
253
|
+
label,
|
|
254
|
+
time,
|
|
255
|
+
variant = "current"
|
|
256
|
+
}) {
|
|
257
|
+
const variantStyles = styles.timeDisplay[variant];
|
|
258
|
+
return /* @__PURE__ */ jsxs("div", { style: variantStyles.container, children: [
|
|
259
|
+
/* @__PURE__ */ jsx("div", { style: styles.timeDisplay.label, children: label }),
|
|
260
|
+
/* @__PURE__ */ jsx("div", { style: variantStyles.value, children: time?.toLocaleString() ?? "-" })
|
|
261
|
+
] });
|
|
262
|
+
}
|
|
263
|
+
export {
|
|
264
|
+
FakeTimeController
|
|
265
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fake-current-time",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Manipulate current time in your application for development and staging environments",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"time",
|
|
@@ -31,6 +31,18 @@
|
|
|
31
31
|
"./browser": {
|
|
32
32
|
"types": "./dist/browser.d.ts",
|
|
33
33
|
"default": "./dist/browser.js"
|
|
34
|
+
},
|
|
35
|
+
"./react": {
|
|
36
|
+
"types": "./dist/react.d.ts",
|
|
37
|
+
"default": "./dist/react.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"react": {
|
|
45
|
+
"optional": true
|
|
34
46
|
}
|
|
35
47
|
},
|
|
36
48
|
"publishConfig": {
|
|
@@ -38,8 +50,11 @@
|
|
|
38
50
|
"provenance": true
|
|
39
51
|
},
|
|
40
52
|
"devDependencies": {
|
|
41
|
-
"@biomejs/biome": "2.3.
|
|
53
|
+
"@biomejs/biome": "2.3.8",
|
|
42
54
|
"@types/node": "^24.10.1",
|
|
55
|
+
"@types/react": "^19.2.10",
|
|
56
|
+
"react": "^19.2.4",
|
|
57
|
+
"react-dom": "^19.2.4",
|
|
43
58
|
"tsup": "^8.3.5",
|
|
44
59
|
"typescript": "^5.7.2",
|
|
45
60
|
"vitest": "^4.0.3"
|