Skip to content

Commit b209617

Browse files
committed
replace moment for luxon
1 parent 0ece16e commit b209617

13 files changed

+258
-127
lines changed

Signum.React/Scripts/ConfigureReactWidgets.ts

Lines changed: 178 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
import * as moment from "moment"
1+
import { DateTime } from 'luxon';
22

33
import * as ReactWidgets from 'react-widgets';
44

55
export function configure() {
66

7-
if (typeof moment !== 'function') throw new TypeError('You must provide a valid moment object');
8-
9-
const localField = (m: moment.Moment) => m.locale || m.lang,
10-
hasLocaleData = !!moment.localeData;
11-
12-
if (!hasLocaleData) throw new TypeError('The Moment localizer depends on the `localeData` api, please provide a moment object v2.2.0 or higher');
13-
14-
function getMoment(culture: string, value: any, format: string | undefined) {
15-
return culture ? localField(moment(value, format))(culture) : moment(value, format);
16-
}
177

188
function endOfDecade(date: Date) {
19-
return moment(date).add(10, 'year').add(-1, 'millisecond').toDate();
9+
return DateTime.fromJSDate(date).plus({ years: 10 }).minus({ millisecond: 1 }).toJSDate();
2010
}
2111

2212
function endOfCentury(date: Date) {
23-
return moment(date).add(100, 'year').add(-1, 'millisecond').toDate();
13+
return DateTime.fromJSDate(date).plus({ years: 100 }).minus({ millisecond: 1 }).toJSDate();
2414
}
2515

2616
const localizer = {
2717
formats: {
28-
date: 'L',
29-
time: 'LT',
30-
'default': 'lll',
31-
header: 'MMMM YYYY',
32-
footer: 'LL',
33-
weekday: 'dd',
34-
dayOfMonth: 'DD',
18+
date: 'D',
19+
time: 't',
20+
'default': 'FF',
21+
header: 'MMMM yyyy',
22+
footer: 'DDD',
23+
weekday: 'EEE',
24+
dayOfMonth: 'dd',
3525
month: 'MMM',
36-
year: 'YYYY',
26+
year: 'yyyy',
3727

3828
decade: function decade(date: Date, culture: string, localizer: any) {
3929
return localizer.format(date, 'YYYY', culture) + ' - ' + localizer.format(endOfDecade(date), 'YYYY', culture);
@@ -45,16 +35,24 @@ export function configure() {
4535
},
4636

4737
firstOfWeek: function firstOfWeek(culture: string) {
48-
return (moment.localeData(culture) as any).firstDayOfWeek();
38+
var day = fistDay[culture?.tryAfter("-") ?? "ES"];
39+
40+
switch (day) {
41+
case "sun": return 0;
42+
case "mon": return 1;
43+
case "fri": return 5;
44+
case "sat": return 6;
45+
default: throw new Error("Unexpected " + day);
46+
}
4947
},
5048

5149
parse: function parse(value: string, format: string, culture: string) {
5250
if (value == undefined || value == "")
5351
return undefined;
5452

55-
var moment = getMoment(culture, value, format);
56-
if (moment.isValid())
57-
return moment.toDate();
53+
var t = DateTime.fromFormat(value, format ?? "F", { locale: culture })
54+
if (t.isValid)
55+
return t.toJSDate();
5856

5957
return undefined;
6058
},
@@ -63,33 +61,165 @@ export function configure() {
6361
if (value == undefined)
6462
return "";
6563

66-
return getMoment(culture, value, undefined).format(_format);
64+
return DateTime.fromJSDate(value, { locale: culture }).toFormat(_format);
6765
}
6866
};
6967
(ReactWidgets as any).setDateLocalizer(localizer);
7068

7169
}
7270

73-
declare module "moment" {
74-
75-
interface Moment {
76-
fromUserInterface(this: moment.Moment): moment.Moment;
77-
toUserInterface(this: moment.Moment): moment.Moment;
78-
}
71+
//https://github.com/unicode-cldr/cldr-core/blob/master/supplemental/weekData.json#L61
72+
const fistDay: { [isoCode: string]: "mon" | "sat" | "sun" | "fri" } = {
73+
"001": "mon",
74+
"AD": "mon",
75+
"AE": "sat",
76+
"AF": "sat",
77+
"AG": "sun",
78+
"AI": "mon",
79+
"AL": "mon",
80+
"AM": "mon",
81+
"AN": "mon",
82+
"AR": "mon",
83+
"AS": "sun",
84+
"AT": "mon",
85+
"AU": "sun",
86+
"AX": "mon",
87+
"AZ": "mon",
88+
"BA": "mon",
89+
"BD": "sun",
90+
"BE": "mon",
91+
"BG": "mon",
92+
"BH": "sat",
93+
"BM": "mon",
94+
"BN": "mon",
95+
"BR": "sun",
96+
"BS": "sun",
97+
"BT": "sun",
98+
"BW": "sun",
99+
"BY": "mon",
100+
"BZ": "sun",
101+
"CA": "sun",
102+
"CH": "mon",
103+
"CL": "mon",
104+
"CM": "mon",
105+
"CN": "sun",
106+
"CO": "sun",
107+
"CR": "mon",
108+
"CY": "mon",
109+
"CZ": "mon",
110+
"DE": "mon",
111+
"DJ": "sat",
112+
"DK": "mon",
113+
"DM": "sun",
114+
"DO": "sun",
115+
"DZ": "sat",
116+
"EC": "mon",
117+
"EE": "mon",
118+
"EG": "sat",
119+
"ES": "mon",
120+
"ET": "sun",
121+
"FI": "mon",
122+
"FJ": "mon",
123+
"FO": "mon",
124+
"FR": "mon",
125+
"GB": "mon",
126+
"GB-alt-variant": "sun",
127+
"GE": "mon",
128+
"GF": "mon",
129+
"GP": "mon",
130+
"GR": "mon",
131+
"GT": "sun",
132+
"GU": "sun",
133+
"HK": "sun",
134+
"HN": "sun",
135+
"HR": "mon",
136+
"HU": "mon",
137+
"ID": "sun",
138+
"IE": "mon",
139+
"IL": "sun",
140+
"IN": "sun",
141+
"IQ": "sat",
142+
"IR": "sat",
143+
"IS": "mon",
144+
"IT": "mon",
145+
"JM": "sun",
146+
"JO": "sat",
147+
"JP": "sun",
148+
"KE": "sun",
149+
"KG": "mon",
150+
"KH": "sun",
151+
"KR": "sun",
152+
"KW": "sat",
153+
"KZ": "mon",
154+
"LA": "sun",
155+
"LB": "mon",
156+
"LI": "mon",
157+
"LK": "mon",
158+
"LT": "mon",
159+
"LU": "mon",
160+
"LV": "mon",
161+
"LY": "sat",
162+
"MC": "mon",
163+
"MD": "mon",
164+
"ME": "mon",
165+
"MH": "sun",
166+
"MK": "mon",
167+
"MM": "sun",
168+
"MN": "mon",
169+
"MO": "sun",
170+
"MQ": "mon",
171+
"MT": "sun",
172+
"MV": "fri",
173+
"MX": "sun",
174+
"MY": "mon",
175+
"MZ": "sun",
176+
"NI": "sun",
177+
"NL": "mon",
178+
"NO": "mon",
179+
"NP": "sun",
180+
"NZ": "mon",
181+
"OM": "sat",
182+
"PA": "sun",
183+
"PE": "sun",
184+
"PH": "sun",
185+
"PK": "sun",
186+
"PL": "mon",
187+
"PR": "sun",
188+
"PT": "sun",
189+
"PY": "sun",
190+
"QA": "sat",
191+
"RE": "mon",
192+
"RO": "mon",
193+
"RS": "mon",
194+
"RU": "mon",
195+
"SA": "sun",
196+
"SD": "sat",
197+
"SE": "mon",
198+
"SG": "sun",
199+
"SI": "mon",
200+
"SK": "mon",
201+
"SM": "mon",
202+
"SV": "sun",
203+
"SY": "sat",
204+
"TH": "sun",
205+
"TJ": "mon",
206+
"TM": "mon",
207+
"TR": "mon",
208+
"TT": "sun",
209+
"TW": "sun",
210+
"UA": "mon",
211+
"UM": "sun",
212+
"US": "sun",
213+
"UY": "mon",
214+
"UZ": "mon",
215+
"VA": "mon",
216+
"VE": "sun",
217+
"VI": "sun",
218+
"VN": "mon",
219+
"WS": "sun",
220+
"XK": "mon",
221+
"YE": "sun",
222+
"ZA": "sun",
223+
"ZW": "sun"
224+
};
79225

80-
function smartNow(this: moment.Moment): moment.Moment;
81-
}
82-
83-
export function asumeGlobalUtcMode(m: typeof moment, utcMode: boolean) {
84-
if (utcMode) {
85-
m.fn.fromUserInterface = function (this: moment.Moment) { return this.utc(); };
86-
m.fn.toUserInterface = function (this: moment.Moment) { return this.local(); };
87-
m.smartNow = function () { return moment.utc(); };
88-
}
89-
90-
else {
91-
m.fn.fromUserInterface = function (this: moment.Moment) { return this; };
92-
m.fn.toUserInterface = function (this: moment.Moment) { return this; };
93-
m.smartNow = function () { return moment(); };
94-
}
95-
}

Signum.React/Scripts/Exceptions/Exception.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import * as moment from 'moment'
2+
import { DateTime } from 'luxon'
33
import { BigStringEmbedded, ExceptionEntity } from '../Signum.Entities.Basics'
44
import { ValueLine, EntityLine, TypeContext } from '../Lines'
55
import { Tab, Tabs } from 'react-bootstrap';
@@ -14,7 +14,7 @@ export default function Exception(p: { ctx: TypeContext<ExceptionEntity> }) {
1414
<div className="row">
1515
<div className="col-sm-6">
1616
<ValueLine ctx={sc.subCtx(f => f.environment)} />
17-
<ValueLine ctx={sc.subCtx(f => f.creationDate)} unitText={moment(sc.value.creationDate!).toUserInterface().fromNow()} />
17+
<ValueLine ctx={sc.subCtx(f => f.creationDate)} unitText={DateTime.fromISO(sc.value.creationDate!).toRelative() ?? undefined} />
1818
<EntityLine ctx={sc.subCtx(f => f.user)} />
1919
<ValueLine ctx={sc.subCtx(f => f.version)} />
2020
<ValueLine ctx={sc.subCtx(f => f.threadId)} />

Signum.React/Scripts/Finder.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import * as moment from "moment"
2+
import { DateTime } from 'luxon'
33
import numbro from "numbro"
44
import * as AppContext from "./AppContext"
55
import * as Navigator from "./Navigator"
@@ -21,7 +21,7 @@ import { TypeEntity, QueryEntity } from './Signum.Entities.Basics';
2121

2222
import {
2323
Type, IType, EntityKind, QueryKey, getQueryNiceName, getQueryKey, isQueryDefined, TypeReference,
24-
getTypeInfo, tryGetTypeInfos, getEnumInfo, toMomentFormat, toNumbroFormat, PseudoType, EntityData,
24+
getTypeInfo, tryGetTypeInfos, getEnumInfo, toLuxonFormat, toNumbroFormat, PseudoType, EntityData,
2525
TypeInfo, PropertyRoute, QueryTokenString, getTypeInfos, tryGetTypeInfo, onReloadTypesActions
2626
} from './Reflection';
2727

@@ -1602,8 +1602,8 @@ export const formatRules: FormatRule[] = [
16021602
name: "Date",
16031603
isApplicable: col => col.token!.filterType == "DateTime",
16041604
formatter: col => {
1605-
const momentFormat = toMomentFormat(col.token!.format);
1606-
return new CellFormatter((cell: string | undefined) => cell == undefined || cell == "" ? "" : <bdi className="date">{moment(cell).format(momentFormat)}</bdi>) //To avoid flippig hour and date (L LT) in RTL cultures
1605+
const luxonFormat = toLuxonFormat(col.token!.format);
1606+
return new CellFormatter((cell: string | undefined) => cell == undefined || cell == "" ? "" : <bdi className="date">{DateTime.fromISO(cell).toFormat(luxonFormat)}</bdi>) //To avoid flippig hour and date (L LT) in RTL cultures
16071607
}
16081608
},
16091609
{
@@ -1618,7 +1618,7 @@ export const formatRules: FormatRule[] = [
16181618
ctx.systemTime && ctx.systemTime.mode == "Between" && ctx.systemTime.startDate! < cell ? "date-created" :
16191619
undefined;
16201620

1621-
return <bdi className={classes("date", className)}>{moment(cell).format("YYYY-MM-DDTHH:mm:ss")}</bdi>; //To avoid flippig hour and date (L LT) in RTL cultures
1621+
return <bdi className={classes("date", className)}>{DateTime.fromISO(cell).toFormat("yyyy-MM-dd'T'HH:mm:ss")}</bdi>; //To avoid flippig hour and date (L LT) in RTL cultures
16221622
});
16231623
}
16241624
},
@@ -1634,7 +1634,7 @@ export const formatRules: FormatRule[] = [
16341634
ctx.systemTime && ctx.systemTime.mode == "Between" && cell < ctx.systemTime.endDate! ? "date-removed" :
16351635
undefined;
16361636

1637-
return <bdi className={classes("date", className)}>{moment(cell).format("YYYY-MM-DDTHH:mm:ss")}</bdi>; //To avoid flippig hour and date (L LT) in RTL cultures
1637+
return <bdi className={classes("date", className)}>{DateTime.fromISO(cell).toFormat("yyyy-MM-dd'T'HH:mm:ss")}</bdi>; //To avoid flippig hour and date (L LT) in RTL cultures
16381638
});
16391639
}
16401640
},

Signum.React/Scripts/Frames/VersionChangedAlert.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import * as moment from 'moment'
2+
import { DateTime } from 'luxon'
33
import { classes } from '../Globals'
44
import { VersionFilter } from '../Services'
55
import { ConnectionMessage } from '../Signum.Entities';
@@ -54,13 +54,13 @@ export function VersionInfo() {
5454

5555
function VersionInfoTooltip(p: {}) {
5656

57-
var bt = moment(VersionFilter.initialBuildTime!);
57+
var bt = DateTime.fromISO(VersionFilter.initialBuildTime!);
5858

5959
return (
6060
<div style={{ whiteSpace: "nowrap" }}>
61-
{bt.format("llll")}
61+
{bt.toFormat("FFFF")}
6262
<br />
63-
({bt.fromNow()})
63+
({bt.toRelative()})
6464
</div>
6565
);
6666
}

0 commit comments

Comments
 (0)