Skip to content

Instantly share code, notes, and snippets.

@wirtaw
Created November 19, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wirtaw/694762c85a16541b74aa45a06c0f4681 to your computer and use it in GitHub Desktop.
Save wirtaw/694762c85a16541b74aa45a06c0f4681 to your computer and use it in GitHub Desktop.
Time zones
'use strict';
const { faker } = require('@faker-js/faker');
const { DateTime, Settings } = require('luxon');
Settings.defaultZone = "utc";
console.info(`defaultZone zoneName ${DateTime.local().zoneName}`);
const dt = DateTime.local();
class Client {
constructor(zone = '') {
this.id = faker.string.uuid();
this.firstName = faker.person.firstName();
this.timezone = zone || faker.location.timeZone();
}
getZone() {
return this.timezone;
}
getId() {
return this.id;
}
getFirstName() {
return this.firstName;
}
getNow() {
return DateTime.now().setZone(this.timezone).toISO();
}
}
class Store {
constructor(zone = '') {
this.products = [];
this.timezone = zone || faker.location.timeZone();
}
getZone() {
return this.timezone;
}
getProducts() {
for (const product of this.products) {
const { id, title, createdAt, updatedAt } = product;
console.info(`${id}
${title}
created ${DateTime.fromMillis(createdAt).toISO()}
updatedAt ${DateTime.fromMillis(updatedAt).toISO()}
`);
}
}
loadProducts(len = 10) {
this.products = Array.from({ length: len }, (v, i) => ({
id: faker.string.uuid(),
title: faker.word.words({ count: 5, min: 1, max: 5 }),
createdAt: DateTime.fromJSDate(faker.date.past()).setZone(this.timezone).toMillis(),
updatedAt: DateTime.fromJSDate(faker.date.past()).setZone(this.timezone).toMillis(),
}));
}
}
const start = async () => {
try {
console.group('CLIENTS');
const clientA = new Client();
const clientB = new Client();
console.info(`clientA ${clientA.getId()}
zone: ${clientA.getZone()}
now ${clientA.getNow()}`);
console.info(`clientB ${clientB.getId()}
zone: ${clientB.getZone()}
now: ${clientB.getNow()}`);
console.groupEnd('CLIENTS');
console.group('PRODUCTS');
const store = new Store('utc');
store.loadProducts(20);
store.getProducts();
console.groupEnd('PRODUCTS');
console.group('SERVER TIME');
// console.dir(dt, { depth: 3 });
console.info(`iso ${dt.toISO()}`);
console.info(`offset ${dt.offset}`);
console.info(`timestamp ${dt.toMillis()}`);
console.info(`zone ${dt.zoneName}`);
console.groupEnd('SERVER TIME');
Settings.defaultZone = "system";
console.info(`defaultZone zoneName ${DateTime.local().zoneName}`);
} catch (exc) {
console.error(' Error ', exc.message);
}
};
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment