Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9dcdba0

Browse files
committed
experiment with luxon
1 parent 4917b38 commit 9dcdba0

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

interface/package-lock.json

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@material-ui/core": "^4.11.2",
77
"@material-ui/icons": "^4.11.2",
88
"@types/lodash": "^4.14.165",
9+
"@types/luxon": "^1.25.1",
910
"@types/node": "^12.12.32",
1011
"@types/react": "^17.0.0",
1112
"@types/react-dom": "^17.0.0",
@@ -15,9 +16,10 @@
1516
"compression-webpack-plugin": "^4.0.0",
1617
"jwt-decode": "^3.1.2",
1718
"lodash": "^4.17.20",
19+
"luxon": "^1.25.0",
1820
"mime-types": "^2.1.28",
19-
"moment": "^2.29.1",
2021
"notistack": "^1.0.3",
22+
"parse-ms": "^2.1.0",
2123
"react": "^17.0.1",
2224
"react-dom": "^17.0.1",
2325
"react-dropzone": "^11.2.4",

interface/src/ntp/NTPStatusForm.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component, Fragment } from 'react';
2-
import moment from 'moment';
2+
import { DateTime } from 'luxon';
33

44
import { WithTheme, withTheme } from '@material-ui/core/styles';
55
import { Avatar, Divider, List, ListItem, ListItemAvatar, ListItemText, Button } from '@material-ui/core';
@@ -14,7 +14,7 @@ import RefreshIcon from '@material-ui/icons/Refresh';
1414

1515
import { RestFormProps, FormButton, HighlightAvatar } from '../components';
1616
import { isNtpActive, ntpStatusHighlight, ntpStatus } from './NTPStatus';
17-
import { formatIsoDateTime, formatLocalDateTime } from './TimeFormat';
17+
import { formatDuration, formatIsoDateTime, formatLocalDateTime, fromISOWithOffset, toISOWithOffset } from './TimeFormat';
1818
import { NTPStatus, Time } from './types';
1919
import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
2020
import { TIME_ENDPOINT } from '../api';
@@ -43,21 +43,18 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
4343
}
4444

4545
openSetTime = () => {
46-
this.setState({ localTime: formatLocalDateTime(moment()), settingTime: true, });
46+
this.setState({ localTime: formatLocalDateTime(DateTime.local()), settingTime: true, });
4747
}
4848

4949
closeSetTime = () => {
5050
this.setState({ settingTime: false });
5151
}
5252

5353
createAdjustedTime = (): Time => {
54-
const currentLocalTime = moment(this.props.data.time_local);
55-
const newLocalTime = moment(this.state.localTime);
56-
newLocalTime.subtract(currentLocalTime.utcOffset())
57-
newLocalTime.milliseconds(0);
58-
newLocalTime.utc();
54+
const currentLocalTime = fromISOWithOffset(this.props.data.time_local);
55+
const newLocalTime = fromISOWithOffset(this.state.localTime).minus(currentLocalTime.offset).toUTC();
5956
return {
60-
time_utc: newLocalTime.format()
57+
time_utc: toISOWithOffset(newLocalTime)
6158
}
6259
}
6360

@@ -171,7 +168,7 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
171168
<AvTimerIcon />
172169
</Avatar>
173170
</ListItemAvatar>
174-
<ListItemText primary="Uptime" secondary={moment.duration(data.uptime, 'seconds').humanize()} />
171+
<ListItemText primary="Uptime" secondary={formatDuration(data.uptime)} />
175172
</ListItem>
176173
<Divider variant="inset" component="li" />
177174
</List>

interface/src/ntp/TimeFormat.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
import moment, { Moment } from 'moment';
1+
import { DateTime } from 'luxon';
2+
import parseMilliseconds from 'parse-ms';
23

3-
export const formatIsoDateTime = (isoDateString: string) => moment.parseZone(isoDateString).format('ll @ HH:mm:ss');
4+
export const fromISOWithOffset = (isoDateString: string) => DateTime.fromISO(isoDateString, { setZone: true });
5+
export const toISOWithOffset = (dateTime: DateTime) => dateTime.toFormat("yyyy-LL-dd'T'HH:mm:ssZZZ");
6+
export const formatIsoDateTime = (isoDateString: string) => fromISOWithOffset(isoDateString).toFormat('DD @ HH:mm:ss ZZ');
7+
export const formatLocalDateTime = (dateTime: DateTime) => dateTime.toFormat("yyyy-LL-dd'T'HH:mm");
48

5-
export const formatLocalDateTime = (moment: Moment) => moment.format('YYYY-MM-DDTHH:mm');
9+
export const formatDuration = (duration: number) => {
10+
const { days, hours, minutes, seconds } = parseMilliseconds(duration * 1000);
11+
var formatted = '';
12+
if (days) {
13+
formatted += days + ' days ';
14+
}
15+
if (formatted || hours) {
16+
formatted += hours + ' hours ';
17+
}
18+
if (formatted || minutes) {
19+
formatted += minutes + ' minutes ';
20+
}
21+
if (formatted || seconds) {
22+
formatted += seconds + ' seconds';
23+
}
24+
return formatted;
25+
}

0 commit comments

Comments
 (0)