Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Normalize stored timestamps to a single timezone (prefer UTC)
Persist timestamps in one consistent timezone (ideally UTC) and let clients localize, instead of mixing local-zone offsets in the datastore.
Bad example
| 1 | // Stores a local-zone offset that varies with DST and source region |
| 2 | public formatDateWithTimezone(date: string): string { |
| 3 | const parsed = parse(date, 'yyyy-MM-dd HH:mm:ss', new Date()); |
| 4 | const zoned = fromZonedTime(parsed, 'Europe/Oslo'); |
| 5 | return formatTz(zoned, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", { timeZone: 'Europe/Oslo' }); |
| 6 | } |
Explanation (EN)
Writing local-zone offsets into the store means sorting and range queries can break once data from a different timezone (or a DST change) arrives, and every consumer must guess the zone.
Objašnjenje (HR)
Spremanje vremenskih oznaka s lokalnim vremenskim odmakom znaci da sortiranje i upiti po rasponu mogu zakazati cim stignu podaci iz druge vremenske zone (ili kod promjene ljetnog vremena), a svaki potrosac mora pogadjati zonu.
Good example
| 1 | // Store UTC; clients convert to local time with a FE date lib |
| 2 | public toUtcIso(date: string): string { |
| 3 | const parsed = parse(date, 'yyyy-MM-dd HH:mm:ss', new Date(), { timeZone: SOURCE_TZ }); |
| 4 | return fromZonedTime(parsed, SOURCE_TZ).toISOString(); // e.g. 2024-05-27T09:20:11.000Z |
| 5 | } |
Explanation (EN)
Storing everything in UTC gives one canonical instant: the store sorts correctly across regions and clients localize trivially with dayjs/Intl. If you must use a single local zone, document it and apply it uniformly.
Objašnjenje (HR)
Spremanje svega u UTC daje jednu kanonsku tocku u vremenu: pohrana ispravno sortira preko regija, a klijenti lako lokaliziraju pomocu dayjs/Intl. Ako ipak koristite jednu lokalnu zonu, dokumentirajte je i primijenite dosljedno.
Notes (EN)
When all data is guaranteed to be in one local zone today, a single local zone is acceptable short-term, but UTC removes the ambiguity entirely.
Bilješke (HR)
Kad su svi podaci danas zajamceno u jednoj lokalnoj zoni, jedna lokalna zona je kratkorocno prihvatljiva, ali UTC u potpunosti uklanja dvosmislenost.