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,
port: process.env.PORT || "8080",
timezone: process.env.TIMEZONE || "Europe/Berlin",
gadgetbridge: {
db_path:
process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db",

View File

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

View File

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

View File

@@ -1,14 +1,24 @@
import { BaseService, type ServiceResult, type StepRow } from "@dpu/shared";
import { DateTime } from "luxon";
import { Config } from "../config.js";
import type { GadgetbridgeClient } from "./client.js";
export class GadgetbridgeService extends BaseService<GadgetbridgeClient> {
getStepsForTimespan(from: Date, to: Date): ServiceResult<StepRow[] | string> {
getStepsForTimespan(from: string, to: string): ServiceResult<StepRow[] | string> {
try {
const fromTs = Math.floor(from.getTime() / 1000);
// Add one day to make `to` inclusive
const toTs = Math.floor(to.getTime() / 1000) + 86400;
const tz = Config.timezone;
const fromDt = DateTime.fromISO(from, { zone: tz }).startOf("day");
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);
} catch (error) {
return this.getErrorResult(