fix timezone issues

This commit is contained in:
Darius
2026-03-06 01:30:05 +01:00
parent 6e61cff231
commit 197fda9ad9
4 changed files with 25 additions and 16 deletions

View File

@@ -6,6 +6,8 @@ export const Config = {
api_key: process.env.API_KEY, api_key: process.env.API_KEY,
port: process.env.PORT || "8080", port: process.env.PORT || "8080",
timezone: process.env.TIMEZONE || "Europe/Berlin",
gadgetbridge: { gadgetbridge: {
db_path: db_path:
process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db", process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db",

View File

@@ -25,29 +25,32 @@ export class GadgetbridgeClient {
return db; return db;
} }
getStepsPerDay(fromTimestamp: number, toTimestamp: number): StepRow[] { getStepsPerDay(fromTimestamp: number, toTimestamp: number, utcOffset: string): StepRow[] {
const stmt = this.db.prepare(` const stmt = this.db.prepare(`
SELECT date, SUM(steps) AS steps SELECT date, SUM(steps) AS steps
FROM ( FROM (
SELECT SELECT
DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date, DATE(TIMESTAMP, 'unixepoch', ?) AS date,
STEPS AS steps STEPS AS steps
FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE
WHERE TIMESTAMP >= ? AND TIMESTAMP < ? WHERE TIMESTAMP >= ? AND TIMESTAMP < ?
UNION ALL UNION ALL
SELECT date, steps SELECT date, steps
FROM old.steps FROM old.steps
WHERE date >= DATE(?, 'unixepoch', 'localtime') WHERE date >= DATE(?, 'unixepoch', ?)
AND date < DATE(?, 'unixepoch', 'localtime') AND date < DATE(?, 'unixepoch', ?)
) )
GROUP BY date GROUP BY date
ORDER BY date ORDER BY date
`); `);
return stmt.all( return stmt.all(
utcOffset,
fromTimestamp, fromTimestamp,
toTimestamp, toTimestamp,
fromTimestamp, fromTimestamp,
utcOffset,
toTimestamp, toTimestamp,
utcOffset,
) as StepRow[]; ) as StepRow[];
} }
} }

View File

@@ -34,13 +34,7 @@ export async function publicGadgetbridgeRoutes(
async (request, reply) => { async (request, reply) => {
const { from, to } = request.query as any; const { from, to } = request.query as any;
const fromDate = new Date(`${from}T00:00:00`); const service_result = gadgetbridgeService.getStepsForTimespan(from, to);
const toDate = new Date(`${to}T00:00:00`);
const service_result = gadgetbridgeService.getStepsForTimespan(
fromDate,
toDate,
);
if (!service_result.successful) { if (!service_result.successful) {
reply.code(418); reply.code(418);

View File

@@ -1,14 +1,24 @@
import { BaseService, type ServiceResult, type StepRow } from "@dpu/shared"; import { BaseService, type ServiceResult, type StepRow } from "@dpu/shared";
import { DateTime } from "luxon";
import { Config } from "../config.js";
import type { GadgetbridgeClient } from "./client.js"; import type { GadgetbridgeClient } from "./client.js";
export class GadgetbridgeService extends BaseService<GadgetbridgeClient> { export class GadgetbridgeService extends BaseService<GadgetbridgeClient> {
getStepsForTimespan(from: Date, to: Date): ServiceResult<StepRow[] | string> { getStepsForTimespan(from: string, to: string): ServiceResult<StepRow[] | string> {
try { try {
const fromTs = Math.floor(from.getTime() / 1000); const tz = Config.timezone;
// Add one day to make `to` inclusive const fromDt = DateTime.fromISO(from, { zone: tz }).startOf("day");
const toTs = Math.floor(to.getTime() / 1000) + 86400; const toDt = DateTime.fromISO(to, { zone: tz }).startOf("day").plus({ days: 1 });
const rows = this.getClient().getStepsPerDay(fromTs, toTs); const fromTs = Math.floor(fromDt.toSeconds());
const toTs = Math.floor(toDt.toSeconds());
const offsetMinutes = fromDt.offset;
const sign = offsetMinutes >= 0 ? "+" : "-";
const abs = Math.abs(offsetMinutes);
const utcOffset = `${sign}${String(Math.floor(abs / 60)).padStart(2, "0")}:${String(abs % 60).padStart(2, "0")}`;
const rows = this.getClient().getStepsPerDay(fromTs, toTs, utcOffset);
return this.getSuccessfulResult(rows); return this.getSuccessfulResult(rows);
} catch (error) { } catch (error) {
return this.getErrorResult( return this.getErrorResult(