time-helper.ts 720 B

12345678910111213141516171819
  1. // box-stats-api/src/feature/time-helper.ts
  2. const TZ_OFFSET_SEC = 8 * 3600;
  3. export function getGmt8HourStartUtcSec(epochSec: number): number {
  4. const shifted = epochSec + TZ_OFFSET_SEC;
  5. const hourStartShifted = Math.floor(shifted / 3600) * 3600;
  6. return hourStartShifted - TZ_OFFSET_SEC; // still UTC epoch seconds
  7. }
  8. export function getGmt8CurrentHourStartUtcSec(nowSec: number): number {
  9. return getGmt8HourStartUtcSec(nowSec);
  10. }
  11. export function computeHourlyWindowUtcSec(nowSec: number, lookbackHours = 2) {
  12. const currentHourStartUtc = getGmt8CurrentHourStartUtcSec(nowSec);
  13. const startSec = currentHourStartUtc - lookbackHours * 3600;
  14. const endSec = nowSec; // realtime
  15. return { startSec, endSec };
  16. }