Skip to content

Commit

Permalink
Fix timezone offset
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiancolosimo committed Nov 6, 2023
1 parent f087552 commit f1bacfe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions docker-compose.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ services:
- HISTORY_INTERVAL=300000 #5 minuti
- GET_LAB_STATE_ENDPOINT=
- GET_LAB_HISTORY_ENDPOINT=
- TIMEZONE_OFFSET=+01:00
restart: unless-stopped
18 changes: 12 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@ import {
import { get_db } from "./src/db.ts";
import { get_env } from "./src/env.ts";
import { ResponseLab } from "./src/interface.ts";







const env = get_env();
const db = get_db();
function generataRisposta(id: number, nome?: string): string {
const date = new Date().toLocaleString("it", { timeZone: "Europe/Rome" });
const formattedDate = date;
if (id == 1 && nome != undefined)
return (
"Il laboratorio è stato aperto alle " + formattedDate + " da " + nome
"Il laboratorio è stato aperto " + formattedDate + " da " + nome
);
else if (id === 1)
return (
"Il laboratorio è stato aperto alle " + formattedDate + " con la chiave"
"Il laboratorio è stato aperto " + formattedDate + " con la chiave"
);
else return "Il laboratorio è stato chiuso alle " + formattedDate;
else return "Il laboratorio è stato chiuso " + formattedDate;
}

const getCurrentLabState = async () => {
Expand All @@ -43,9 +50,8 @@ const getCurrentLabState = async () => {
const jsonFetchApetureOnline = await rawFetchApetureOnline.json();
let nome = undefined;
const dataAttuale = new Date() as any;
dataAttuale.setHours(dataAttuale.getHours() + 2);
const dataApetura = new Date(jsonFetchApetureOnline[0].time) as any;
if (dataAttuale - dataApetura < env.HISTORY_INTERVAL) {
const dataApetura = new Date(jsonFetchApetureOnline[0].time+env.TIMEZONE_OFFSET) as any;
if ((dataAttuale - dataApetura) < env.HISTORY_INTERVAL) {
nome = jsonFetchApetureOnline[0].user;
}
//console.log(nome);
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ and set the env vars:

- GET_LAB_HISTORY_ENDPOINT

- TIMEZONE_OFFSET default +01:00 (Italy)
and for start it run the command docker-compose up -d
5 changes: 4 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export function get_env() {
const GET_LAB_STATE_ENDPOINT = Deno.env.get("GET_LAB_STATE_ENDPOINT");
const GET_LAB_HISTORY_ENDPOINT = Deno.env.get("GET_LAB_HISTORY_ENDPOINT");
const HISTORY_INTERVAL = Deno.env.get("HISTORY_INTERVAL");
const TIMEZONE_OFFSET = Deno.env.get("TIMEZONE_OFFSET");
console.log(
"keys",
TOKEN_TELEGRAM,
POLLING_INTERVAL,
GET_LAB_STATE_ENDPOINT,
GET_LAB_HISTORY_ENDPOINT
GET_LAB_HISTORY_ENDPOINT,
TIMEZONE_OFFSET
);
if (!TOKEN_TELEGRAM) throw new Error("Bot token is not provided");
if (!POLLING_INTERVAL) throw new Error("Polling interval is not provided");
Expand All @@ -25,5 +27,6 @@ export function get_env() {
GET_LAB_STATE_ENDPOINT,
GET_LAB_HISTORY_ENDPOINT,
HISTORY_INTERVAL:parseInt(HISTORY_INTERVAL),
TIMEZONE_OFFSET
};
}

0 comments on commit f1bacfe

Please sign in to comment.