This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
6,204 additions
and
3,912 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
import moment, { Moment } from 'moment'; | ||
import parseMilliseconds from 'parse-ms'; | ||
|
||
export const formatIsoDateTime = (isoDateString: string) => moment.parseZone(isoDateString).format('ll @ HH:mm:ss'); | ||
const LOCALE_FORMAT = new Intl.DateTimeFormat( | ||
[...window.navigator.languages], | ||
{ | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: false | ||
} | ||
); | ||
|
||
export const formatLocalDateTime = (moment: Moment) => moment.format('YYYY-MM-DDTHH:mm'); | ||
export const formatDateTime = (dateTime: string) => { | ||
return LOCALE_FORMAT.format(new Date(dateTime.substr(0, 19))); | ||
} | ||
|
||
export const formatLocalDateTime = (date: Date) => { | ||
return new Date(date.getTime() - date.getTimezoneOffset() * 60000) | ||
.toISOString() | ||
.slice(0, -1) | ||
.substr(0, 19); | ||
} | ||
|
||
export const formatDuration = (duration: number) => { | ||
const { days, hours, minutes, seconds } = parseMilliseconds(duration * 1000); | ||
var formatted = ''; | ||
if (days) { | ||
formatted += pluralize(days, 'day'); | ||
} | ||
if (formatted || hours) { | ||
formatted += pluralize(hours, 'hour'); | ||
} | ||
if (formatted || minutes) { | ||
formatted += pluralize(minutes, 'minute'); | ||
} | ||
if (formatted || seconds) { | ||
formatted += pluralize(seconds, 'second'); | ||
} | ||
return formatted; | ||
} | ||
|
||
const pluralize = (count: number, noun: string, suffix: string = 's') => ` ${count} ${noun}${count !== 1 ? suffix : ''} `; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
#include <NTPStatus.h> | ||
|
||
NTPStatus::NTPStatus(AsyncWebServer * server, SecurityManager * securityManager) { | ||
server->on(NTP_STATUS_SERVICE_PATH, | ||
HTTP_GET, | ||
securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); | ||
NTPStatus::NTPStatus(AsyncWebServer* server, SecurityManager* securityManager) { | ||
server->on(NTP_STATUS_SERVICE_PATH, | ||
HTTP_GET, | ||
securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), | ||
AuthenticationPredicates::IS_AUTHENTICATED)); | ||
} | ||
|
||
String toISOString(tm * time, bool incOffset) { | ||
char time_string[25]; | ||
strftime(time_string, 25, incOffset ? "%FT%T%z" : "%FT%TZ", time); | ||
return String(time_string); | ||
/* | ||
* 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, format, time); | ||
return String(time_string); | ||
} | ||
|
||
void NTPStatus::ntpStatus(AsyncWebServerRequest * request) { | ||
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE); | ||
JsonObject root = response->getRoot(); | ||
String toUTCTimeString(tm* time) { | ||
return formatTime(time, "%FT%TZ"); | ||
} | ||
|
||
String toLocalTimeString(tm* time) { | ||
return formatTime(time, "%FT%T"); | ||
} | ||
|
||
void NTPStatus::ntpStatus(AsyncWebServerRequest* request) { | ||
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_NTP_STATUS_SIZE); | ||
JsonObject root = response->getRoot(); | ||
|
||
// grab the current instant in unix seconds | ||
time_t now = time(nullptr); | ||
// grab the current instant in unix seconds | ||
time_t now = time(nullptr); | ||
|
||
// only provide enabled/disabled status for now | ||
root["status"] = sntp_enabled() ? 1 : 0; | ||
// only provide enabled/disabled status for now | ||
root["status"] = sntp_enabled() ? 1 : 0; | ||
|
||
// the current time in UTC | ||
root["time_utc"] = toISOString(gmtime(&now), false); | ||
// the current time in UTC | ||
root["utc_time"] = toUTCTimeString(gmtime(&now)); | ||
|
||
// local time as ISO String with TZ | ||
root["time_local"] = toISOString(localtime(&now), true); | ||
// local time with offset | ||
root["local_time"] = toLocalTimeString(localtime(&now)); | ||
|
||
// the sntp server name | ||
root["server"] = sntp_getservername(0); | ||
// the sntp server name | ||
root["server"] = sntp_getservername(0); | ||
|
||
// device uptime in seconds | ||
root["uptime"] = uuid::get_uptime() / 1000; | ||
// device uptime in seconds | ||
root["uptime"] = millis() / 1000; | ||
|
||
response->setLength(); | ||
request->send(response); | ||
response->setLength(); | ||
request->send(response); | ||
} |