From aa6d6be329cfbc669dd0ccc774d283f24297a5e8 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 17 Jan 2021 22:33:21 +0300 Subject: [PATCH 1/3] experiment with alternative payload for NTP --- interface/.env.development | 4 ++-- lib/framework/NTPSettingsService.cpp | 4 ++-- lib/framework/NTPStatus.cpp | 29 +++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/interface/.env.development b/interface/.env.development index b12cfd04..949cce68 100644 --- a/interface/.env.development +++ b/interface/.env.development @@ -1,4 +1,4 @@ # Change the IP address to that of your ESP device to enable local development of the UI. # Remember to also enable CORS in platformio.ini before uploading the code to the device. -REACT_APP_HTTP_ROOT=http://192.168.0.88 -REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.88 +REACT_APP_HTTP_ROOT=http://192.168.0.24 +REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.24 diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index 420db7f4..04b26238 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -73,9 +73,9 @@ void NTPSettingsService::configureNTP() { void NTPSettingsService::configureTime(AsyncWebServerRequest* request, JsonVariant& json) { if (!sntp_enabled() && json.is()) { - String timeUtc = json["time_utc"]; struct tm tm = {0}; - char* s = strptime(timeUtc.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm); + String timeLocal = json["time_local"]; + char* s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm); if (s != nullptr) { time_t time = mktime(&tm); struct timeval now = {.tv_sec = time}; diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index 2275d2f7..e4c50661 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -7,12 +7,29 @@ NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) { AuthenticationPredicates::IS_AUTHENTICATED)); } -String toISOString(tm* time, bool incOffset) { +/* + * Formats the time using the format provided. + * + * Uses a 25 byte buffer, large enough to fit an ISO time string with offset. + */ +String formatTime(tm* time, const char* format) { char time_string[25]; - strftime(time_string, 25, incOffset ? "%FT%T%z" : "%FT%TZ", time); + strftime(time_string, 25, format, time); return String(time_string); } +String toUTCTimeString(tm* time) { + return formatTime(time, "%FT%TZ"); +} + +String toLocalTimeString(tm* time) { + return formatTime(time, "%FT%T"); +} + +String offsetString(tm* time) { + return formatTime(time, "%z"); +} + void NTPStatus::ntpStatus(AsyncWebServerRequest* request) { AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE); JsonObject root = response->getRoot(); @@ -24,10 +41,12 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest* request) { root["status"] = sntp_enabled() ? 1 : 0; // the current time in UTC - root["time_utc"] = toISOString(gmtime(&now), false); + root["time_utc"] = toUTCTimeString(gmtime(&now)); - // local time as ISO String with TZ - root["time_local"] = toISOString(localtime(&now), true); + // local time with offset separate + struct tm* ltm = localtime(&now); + root["time_local"] = toLocalTimeString(ltm); + root["time_offset"] = offsetString(ltm); // the sntp server name root["server"] = sntp_getservername(0); From 0c06c38f07ca46fd7cd606ab419c746c39747347 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 17 Jan 2021 19:24:47 -0200 Subject: [PATCH 2/3] experiment with datefns --- interface/package-lock.json | 15 ++++++++----- interface/package.json | 3 ++- interface/src/ntp/NTPStatusForm.tsx | 18 ++++++---------- interface/src/ntp/TimeFormat.ts | 33 ++++++++++++++++++++++++++--- interface/src/ntp/types.ts | 2 +- lib/framework/NTPStatus.cpp | 2 +- 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/interface/package-lock.json b/interface/package-lock.json index 19b41e6b..883ce1f9 100644 --- a/interface/package-lock.json +++ b/interface/package-lock.json @@ -4835,6 +4835,11 @@ "whatwg-url": "^8.0.0" } }, + "date-fns": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.16.1.tgz", + "integrity": "sha512-sAJVKx/FqrLYHAQeN7VpJrPhagZc9R4ImZIWYRFZaaohR3KzmuK88touwsSwSVT8Qcbd4zoDsnGfX4GFB4imyQ==" + }, "debug": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", @@ -10309,11 +10314,6 @@ "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" }, - "moment": { - "version": "2.29.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", - "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" - }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", @@ -10984,6 +10984,11 @@ "lines-and-columns": "^1.1.6" } }, + "parse-ms": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/parse-ms/-/parse-ms-2.1.0.tgz", + "integrity": "sha512-kHt7kzLoS9VBZfUsiKjv43mr91ea+U05EyKkEtqp7vNbHxmaVuEqN7XxeEVnGrMtYOAxGrDElSi96K7EgO1zCA==" + }, "parse5": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/parse5/-/parse5-5.1.1.tgz", diff --git a/interface/package.json b/interface/package.json index 2cf702ad..40c10efc 100644 --- a/interface/package.json +++ b/interface/package.json @@ -13,11 +13,12 @@ "@types/react-router": "^5.1.8", "@types/react-router-dom": "^5.1.6", "compression-webpack-plugin": "^4.0.0", + "date-fns": "^2.16.1", "jwt-decode": "^3.1.2", "lodash": "^4.17.20", "mime-types": "^2.1.28", - "moment": "^2.29.1", "notistack": "^1.0.3", + "parse-ms": "^2.1.0", "react": "^17.0.1", "react-dom": "^17.0.1", "react-dropzone": "^11.2.4", diff --git a/interface/src/ntp/NTPStatusForm.tsx b/interface/src/ntp/NTPStatusForm.tsx index 6b277dd1..e75990ed 100644 --- a/interface/src/ntp/NTPStatusForm.tsx +++ b/interface/src/ntp/NTPStatusForm.tsx @@ -1,5 +1,4 @@ import React, { Component, Fragment } from 'react'; -import moment from 'moment'; import { WithTheme, withTheme } from '@material-ui/core/styles'; import { Avatar, Divider, List, ListItem, ListItemAvatar, ListItemText, Button } from '@material-ui/core'; @@ -14,7 +13,7 @@ import RefreshIcon from '@material-ui/icons/Refresh'; import { RestFormProps, FormButton, HighlightAvatar } from '../components'; import { isNtpActive, ntpStatusHighlight, ntpStatus } from './NTPStatus'; -import { formatIsoDateTime, formatLocalDateTime } from './TimeFormat'; +import { formatDuration, formatDateTime, formatLocalDateTimeNow, formatLocalDateTime } from './TimeFormat'; import { NTPStatus, Time } from './types'; import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication'; import { TIME_ENDPOINT } from '../api'; @@ -43,7 +42,7 @@ class NTPStatusForm extends Component { } openSetTime = () => { - this.setState({ localTime: formatLocalDateTime(moment()), settingTime: true, }); + this.setState({ localTime: formatLocalDateTimeNow(), settingTime: true, }); } closeSetTime = () => { @@ -51,13 +50,8 @@ class NTPStatusForm extends Component { } createAdjustedTime = (): Time => { - const currentLocalTime = moment(this.props.data.time_local); - const newLocalTime = moment(this.state.localTime); - newLocalTime.subtract(currentLocalTime.utcOffset()) - newLocalTime.milliseconds(0); - newLocalTime.utc(); return { - time_utc: newLocalTime.format() + time_local: formatLocalDateTime(this.state.localTime) } } @@ -153,7 +147,7 @@ class NTPStatusForm extends Component { - + @@ -162,7 +156,7 @@ class NTPStatusForm extends Component { - + @@ -171,7 +165,7 @@ class NTPStatusForm extends Component { - + diff --git a/interface/src/ntp/TimeFormat.ts b/interface/src/ntp/TimeFormat.ts index 7e0bb826..df991b29 100644 --- a/interface/src/ntp/TimeFormat.ts +++ b/interface/src/ntp/TimeFormat.ts @@ -1,5 +1,32 @@ -import moment, { Moment } from 'moment'; +import { parseISO, format } from 'date-fns'; +import parseMilliseconds from 'parse-ms'; -export const formatIsoDateTime = (isoDateString: string) => moment.parseZone(isoDateString).format('ll @ HH:mm:ss'); +export const formatDateTime = (dateTime: string) => { + return format(parseISO(dateTime.substr(0, 19)), 'PP @ HH:mm:ss'); +} -export const formatLocalDateTime = (moment: Moment) => moment.format('YYYY-MM-DDTHH:mm'); +export const formatLocalDateTimeNow = () => { + return format(new Date(), 'yyyy-MM-ddTHH:mm'); +} + +export const formatLocalDateTime = (dateTime: string) => { + return format(parseISO(dateTime), 'yyyy-MM-ddTHH:mm:ss'); +} + +export const formatDuration = (duration: number) => { + const { days, hours, minutes, seconds } = parseMilliseconds(duration * 1000); + var formatted = ''; + if (days) { + formatted += days + ' days '; + } + if (formatted || hours) { + formatted += hours + ' hours '; + } + if (formatted || minutes) { + formatted += minutes + ' minutes '; + } + if (formatted || seconds) { + formatted += seconds + ' seconds'; + } + return formatted; +} diff --git a/interface/src/ntp/types.ts b/interface/src/ntp/types.ts index a266d121..5498e216 100644 --- a/interface/src/ntp/types.ts +++ b/interface/src/ntp/types.ts @@ -19,5 +19,5 @@ export interface NTPSettings { } export interface Time { - time_utc: string; + time_local: string; } diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index e4c50661..227c7504 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -43,7 +43,7 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest* request) { // the current time in UTC root["time_utc"] = toUTCTimeString(gmtime(&now)); - // local time with offset separate + // local time with offset struct tm* ltm = localtime(&now); root["time_local"] = toLocalTimeString(ltm); root["time_offset"] = offsetString(ltm); From 354a161ca3a49729d4326a47a529d48602b390d8 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 17 Jan 2021 19:26:41 -0200 Subject: [PATCH 3/3] experiment with datefns --- interface/src/ntp/TimeFormat.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/ntp/TimeFormat.ts b/interface/src/ntp/TimeFormat.ts index df991b29..e1668103 100644 --- a/interface/src/ntp/TimeFormat.ts +++ b/interface/src/ntp/TimeFormat.ts @@ -6,11 +6,11 @@ export const formatDateTime = (dateTime: string) => { } export const formatLocalDateTimeNow = () => { - return format(new Date(), 'yyyy-MM-ddTHH:mm'); + return format(new Date(), "yyyy-MM-dd'T'HH:mm"); } export const formatLocalDateTime = (dateTime: string) => { - return format(parseISO(dateTime), 'yyyy-MM-ddTHH:mm:ss'); + return format(parseISO(dateTime), "yyyy-MM-dd'T'HH:mm:ss"); } export const formatDuration = (duration: number) => {