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).
Use a timezone-aware date library instead of hand-rolled tz math
Prefer a dedicated timezone-aware date utility over manual Date string building and locale conversions for cross-timezone conversions.
Bad example
| 1 | function utcTimeToLocal(utcTime: string, tz: string): string | null { |
| 2 | if (!utcTime) return null; |
| 3 | const today = new Date().toISOString().slice(0, 10); |
| 4 | const utcDate = new Date(`${today}T${utcTime}Z`); |
| 5 | return utcDate.toLocaleTimeString('en-GB', { timeZone: tz, hour12: false }); |
| 6 | } |
Explanation (EN)
Splicing today's date onto a time string and round-tripping through toLocaleTimeString is fragile: it mishandles DST boundaries, date rollovers, and malformed input.
Objašnjenje (HR)
Lijepljenje danasnjeg datuma na string vremena i provlacenje kroz toLocaleTimeString je krhko: lose obradjuje granice ljetnog racunanja vremena, prijelaze datuma i neispravan unos.
Good example
| 1 | import { TZDate } from '@date-fns/tz'; |
| 2 | import { format } from 'date-fns'; |
| 3 |
|
| 4 | function utcTimeToLocal(utcInstant: Date, tz: string): string { |
| 5 | return format(new TZDate(utcInstant, tz), 'HH:mm:ss'); |
| 6 | } |
Explanation (EN)
A timezone-aware library handles DST, offsets, and edge cases correctly and expresses intent more clearly than manual string surgery.
Objašnjenje (HR)
Biblioteka svjesna vremenskih zona ispravno obradjuje ljetno racunanje vremena, pomake i rubne slucajeve te jasnije izrazava namjeru nego rucna manipulacija stringovima.
Exceptions / Tradeoffs (EN)
Trivial, fixed-offset formatting where no DST or locale concerns apply may not need a library.
Iznimke / Tradeoffi (HR)
Trivijalno formatiranje s fiksnim pomakom gdje nema briga oko ljetnog racunanja vremena ili lokalizacije mozda ne treba biblioteku.