use local time instead of UTC, migrate.js, closes #131

Run `node migrate.js localtime data/*.json` to convert
existing `time` entries from UTC to your local timezone.
This commit is contained in:
Ralf Vogler 2023-04-28 00:11:29 +02:00
parent bc8a89f365
commit bb51fd8065
2 changed files with 37 additions and 3 deletions

34
migrate.js Normal file
View file

@ -0,0 +1,34 @@
import { existsSync } from 'fs';
import { Low } from 'lowdb';
import { JSONFile } from 'lowdb/node';
import { datetime } from './util.js';
const datetime_UTCtoLocalTimezone = async file => {
if (!existsSync(file))
return console.error('File does not exist:', file);
const db = new Low(new JSONFile(file));
await db.read();
db.data ||= {};
console.log('Migrating', file);
for (const user in db.data) {
for (const game in db.data[user]) {
const time1 = db.data[user][game].time;
const time1s = time1.endsWith('Z') ? time1 : time1 + ' UTC';
const time2 = datetime(new Date(time1s));
console.log([game, time1, time2]);
db.data[user][game].time = time2;
}
}
// console.log(db.data);
await db.write(); // write out json db
};
const args = process.argv.slice(2);
if (args[0] == 'localtime') {
const files = args.slice(1);
console.log('Will convert UTC datetime to local timezone for', files);
files.forEach(datetime_UTCtoLocalTimezone);
} else {
console.log('Usage: node migrate.js <cmd> <args>');
console.log(' node migrate.js localtime data/*.json');
}

View file

@ -19,9 +19,9 @@ export const jsonDb = async file => {
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); export const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// date and time as UTC (no timezone offset) in nicely readable and sortable format, e.g., 2022-10-06 12:05:27.313 // date and time as UTC (no timezone offset) in nicely readable and sortable format, e.g., 2022-10-06 12:05:27.313
export const datetime = (d = new Date()) => d.toISOString().replace('T', ' ').replace('Z', ''); export const datetimeUTC = (d = new Date()) => d.toISOString().replace('T', ' ').replace('Z', '');
// same as datetime() but for local timezone, e.g., UTC + 2h for the above in DE // same as datetimeUTC() but for local timezone, e.g., UTC + 2h for the above in DE
export const datetimeLocal = (d = new Date()) => datetime(new Date(d.getTime() - new Date().getTimezoneOffset() * 60000)); export const datetime = (d = new Date()) => datetimeUTC(new Date(d.getTime() - d.getTimezoneOffset() * 60000));
export const filenamify = s => s.replaceAll(':', '.').replace(/[^a-z0-9 _\-.]/gi, '_'); // alternative: https://www.npmjs.com/package/filenamify - On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods are reserved. export const filenamify = s => s.replaceAll(':', '.').replace(/[^a-z0-9 _\-.]/gi, '_'); // alternative: https://www.npmjs.com/package/filenamify - On Unix-like systems, / is reserved. On Windows, <>:"/\|?* along with trailing periods are reserved.
export const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C export const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C