UNPKG

3.55 kBJavaScriptView Raw
1import SystemZone from "./zones/systemZone.js";
2import IANAZone from "./zones/IANAZone.js";
3import Locale from "./impl/locale.js";
4
5import { normalizeZone } from "./impl/zoneUtil.js";
6
7let now = () => Date.now(),
8 defaultZone = "system",
9 defaultLocale = null,
10 defaultNumberingSystem = null,
11 defaultOutputCalendar = null,
12 throwOnInvalid;
13
14/**
15 * Settings contains static getters and setters that control Luxon's overall behavior. Luxon is a simple library with few options, but the ones it does have live here.
16 */
17export default class Settings {
18 /**
19 * Get the callback for returning the current timestamp.
20 * @type {function}
21 */
22 static get now() {
23 return now;
24 }
25
26 /**
27 * Set the callback for returning the current timestamp.
28 * The function should return a number, which will be interpreted as an Epoch millisecond count
29 * @type {function}
30 * @example Settings.now = () => Date.now() + 3000 // pretend it is 3 seconds in the future
31 * @example Settings.now = () => 0 // always pretend it's Jan 1, 1970 at midnight in UTC time
32 */
33 static set now(n) {
34 now = n;
35 }
36
37 /**
38 * Set the default time zone to create DateTimes in. Does not affect existing instances.
39 * Use the value "system" to reset this value to the system's time zone.
40 * @type {string}
41 */
42 static set defaultZone(zone) {
43 defaultZone = zone;
44 }
45
46 /**
47 * Get the default time zone object currently used to create DateTimes. Does not affect existing instances.
48 * The default value is the system's time zone (the one set on the machine that runs this code).
49 * @type {Zone}
50 */
51 static get defaultZone() {
52 return normalizeZone(defaultZone, SystemZone.instance);
53 }
54
55 /**
56 * Get the default locale to create DateTimes with. Does not affect existing instances.
57 * @type {string}
58 */
59 static get defaultLocale() {
60 return defaultLocale;
61 }
62
63 /**
64 * Set the default locale to create DateTimes with. Does not affect existing instances.
65 * @type {string}
66 */
67 static set defaultLocale(locale) {
68 defaultLocale = locale;
69 }
70
71 /**
72 * Get the default numbering system to create DateTimes with. Does not affect existing instances.
73 * @type {string}
74 */
75 static get defaultNumberingSystem() {
76 return defaultNumberingSystem;
77 }
78
79 /**
80 * Set the default numbering system to create DateTimes with. Does not affect existing instances.
81 * @type {string}
82 */
83 static set defaultNumberingSystem(numberingSystem) {
84 defaultNumberingSystem = numberingSystem;
85 }
86
87 /**
88 * Get the default output calendar to create DateTimes with. Does not affect existing instances.
89 * @type {string}
90 */
91 static get defaultOutputCalendar() {
92 return defaultOutputCalendar;
93 }
94
95 /**
96 * Set the default output calendar to create DateTimes with. Does not affect existing instances.
97 * @type {string}
98 */
99 static set defaultOutputCalendar(outputCalendar) {
100 defaultOutputCalendar = outputCalendar;
101 }
102
103 /**
104 * Get whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals
105 * @type {boolean}
106 */
107 static get throwOnInvalid() {
108 return throwOnInvalid;
109 }
110
111 /**
112 * Set whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals
113 * @type {boolean}
114 */
115 static set throwOnInvalid(t) {
116 throwOnInvalid = t;
117 }
118
119 /**
120 * Reset Luxon's global caches. Should only be necessary in testing scenarios.
121 * @return {void}
122 */
123 static resetCaches() {
124 Locale.resetCache();
125 IANAZone.resetCache();
126 }
127}