Make two functions to convert ISO 8601 <=> YYYY-MM-DD hh:mm:ss

This commit is contained in:
Louis Lam 2022-09-28 00:48:15 +08:00
parent b1465c0282
commit 204339fbed
5 changed files with 37 additions and 35 deletions

View file

@ -1,9 +1,7 @@
const dayjs = require("dayjs");
const { BeanModel } = require("redbean-node/dist/bean-model"); const { BeanModel } = require("redbean-node/dist/bean-model");
const { parseTimeObject, parseTimeFromTimeObject } = require("../../src/util"); const { parseTimeObject, parseTimeFromTimeObject, isoToUTCDateTime, utcToISODateTime } = require("../../src/util");
const { isArray } = require("chart.js/helpers"); const { isArray } = require("chart.js/helpers");
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server"); const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
const { R } = require("redbean-node");
class Maintenance extends BeanModel { class Maintenance extends BeanModel {
@ -15,20 +13,11 @@ class Maintenance extends BeanModel {
*/ */
async toPublicJSON(timezone = null) { async toPublicJSON(timezone = null) {
let dateTimeRange = [];
if (this.start_datetime) {
dateTimeRange.push(dayjs.utc(this.start_datetime).toISOString());
if (this.end_datetime) {
dateTimeRange.push(dayjs.utc(this.end_datetime).toISOString());
}
}
let dateRange = []; let dateRange = [];
if (this.start_date) { if (this.start_date) {
dateRange.push(dayjs.utc(this.start_date).toISOString()); dateRange.push(utcToISODateTime(this.start_date));
if (this.end_date) { if (this.end_date) {
dateRange.push(dayjs.utc(this.end_date).toISOString()); dateRange.push(utcToISODateTime(this.end_date));
} }
} }
@ -55,7 +44,6 @@ class Maintenance extends BeanModel {
strategy: this.strategy, strategy: this.strategy,
intervalDay: this.interval_day, intervalDay: this.interval_day,
active: !!this.active, active: !!this.active,
dateTimeRange: dateTimeRange,
dateRange: dateRange, dateRange: dateRange,
timeRange: timeRange, timeRange: timeRange,
weekdays: (this.weekdays) ? JSON.parse(this.weekdays) : [], weekdays: (this.weekdays) ? JSON.parse(this.weekdays) : [],
@ -104,18 +92,10 @@ class Maintenance extends BeanModel {
bean.active = obj.active; bean.active = obj.active;
if (obj.dateRange[0]) { if (obj.dateRange[0]) {
bean.start_date = R.isoDate(dayjs(obj.dateRange[0]).utc()); bean.start_date = isoToUTCDateTime(obj.dateRange[0]);
if (obj.dateRange[1]) { if (obj.dateRange[1]) {
bean.end_date = R.isoDate(dayjs(obj.dateRange[1]).utc()); bean.end_date = isoToUTCDateTime(obj.dateRange[1]);
}
}
if (obj.dateTimeRange[0]) {
bean.start_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[0]).utc());
if (obj.dateTimeRange[1]) {
bean.end_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[1]).utc());
} }
} }

View file

@ -36,8 +36,8 @@ class MaintenanceTimeslot extends BeanModel {
} else if (maintenance.strategy === "single") { } else if (maintenance.strategy === "single") {
let bean = R.dispense("maintenance_timeslot"); let bean = R.dispense("maintenance_timeslot");
bean.maintenance_id = maintenance.id; bean.maintenance_id = maintenance.id;
bean.start_date = maintenance.start_datetime; bean.start_date = maintenance.start_date;
bean.end_date = maintenance.end_datetime; bean.end_date = maintenance.end_date;
bean.generated_next = true; bean.generated_next = true;
await R.store(bean); await R.store(bean);
} else { } else {

View file

@ -103,7 +103,7 @@
<div class="my-3"> <div class="my-3">
<label class="form-label">{{ $t("DateTime Range") }}</label> <label class="form-label">{{ $t("DateTime Range") }}</label>
<Datepicker <Datepicker
v-model="maintenance.dateTimeRange" v-model="maintenance.dateRange"
:dark="$root.isDark" :dark="$root.isDark"
range textInput range textInput
:monthChangeOnScroll="false" :monthChangeOnScroll="false"
@ -369,8 +369,7 @@ export default {
strategy: "single", strategy: "single",
active: 1, active: 1,
intervalDay: 1, intervalDay: 1,
dateTimeRange: [ this.minDate ], dateRange: [ this.minDate ],
dateRange: [],
timeRange: [{ timeRange: [{
hours: 2, hours: 2,
minutes: 0, minutes: 0,

View file

@ -7,9 +7,8 @@
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0;
const _dayjs = require("dayjs"); const dayjs = require("dayjs");
const dayjs = _dayjs;
exports.isDev = process.env.NODE_ENV === "development"; exports.isDev = process.env.NODE_ENV === "development";
exports.appName = "Uptime Kuma"; exports.appName = "Uptime Kuma";
exports.DOWN = 0; exports.DOWN = 0;
@ -351,3 +350,14 @@ function parseTimeFromTimeObject(obj) {
return result; return result;
} }
exports.parseTimeFromTimeObject = parseTimeFromTimeObject; exports.parseTimeFromTimeObject = parseTimeFromTimeObject;
function isoToUTCDateTime(input) {
return dayjs(input).utc().format("YYYY-MM-DD HH:mm:ss");
}
exports.isoToUTCDateTime = isoToUTCDateTime;
/**
* @param input
*/
function utcToISODateTime(input) {
return dayjs.utc(input).toISOString();
}
exports.utcToISODateTime = utcToISODateTime;

View file

@ -6,8 +6,10 @@
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
import * as _dayjs from "dayjs"; import * as dayjs from "dayjs";
const dayjs = _dayjs; import * as timezone from "dayjs/plugin/timezone";
import * as utc from "dayjs/plugin/utc";
import {R} from "redbean-node";
export const isDev = process.env.NODE_ENV === "development"; export const isDev = process.env.NODE_ENV === "development";
export const appName = "Uptime Kuma"; export const appName = "Uptime Kuma";
@ -392,3 +394,14 @@ export function parseTimeFromTimeObject(obj : any) {
return result; return result;
} }
export function isoToUTCDateTime(input : string) {
return dayjs(input).utc().format("YYYY-MM-DD HH:mm:ss");
}
/**
* @param input
*/
export function utcToISODateTime(input : string) {
return dayjs.utc(input).toISOString();
}