Skip to content

Commit 1b1a18f

Browse files
committed
fix(command): Added logging functionality to DateCommand class, including debug and info logs for various date formatting and conversion steps.
1 parent a230b75 commit 1b1a18f

1 file changed

Lines changed: 44 additions & 12 deletions

File tree

src/service/command/date.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
import { Context } from "grammy";
22
import { SafeExecution } from "../../decorators/SafeExecution";
3+
import { Logger } from "../../config/logger";
4+
const logger = new Logger({
5+
file: "date-command.log",
6+
level: "info",
7+
timestampFormat: "locale",
8+
jsonFormat: true,
9+
});
310

411
export class DateCommand {
512
@SafeExecution()
613
static async date(ctx: Context) {
14+
logger.info("Date command invoked.", "Date", {
15+
chatId: ctx.chat?.id!,
16+
userId: ctx.from?.id!,
17+
});
18+
719
const now = new Date();
820

921
// Format Gregorian Date
1022
const gregorianDate = this.formatGregorianDate(now);
23+
logger.info("Gregorian date formatted.", "Date", { gregorianDate });
1124

1225
// Convert to Persian Date
1326
const persianDate = this.convertToPersianDate(now);
27+
logger.info("Persian date converted.", "Date", { persianDate });
1428

1529
// Reply with both date formats
1630
await ctx.reply(
@@ -21,6 +35,8 @@ Persian Date: **${persianDate}**`,
2135
}
2236

2337
static formatGregorianDate(date: Date): string {
38+
logger.debug("Formatting Gregorian date.", "Date", { date });
39+
2440
const days = [
2541
"Sunday",
2642
"Monday",
@@ -56,12 +72,17 @@ Persian Date: **${persianDate}**`,
5672
}
5773

5874
static convertToPersianDate(date: Date): string {
59-
// Correct conversion using the Jalaali algorithm
75+
// Gy: Gregorian year
76+
// Gm: Gregorian month (1-12)
77+
// Gd: Gregorian day of the month (1-31)
78+
79+
logger.debug("Converting to Persian date.", "Date", { date });
6080
const persianDate = this.toJalaali(
6181
date.getFullYear(),
6282
date.getMonth() + 1,
6383
date.getDate()
6484
);
85+
logger.debug("Persian date calculated.", "Date", { persianDate });
6586

6687
const persianDays = [
6788
"شنبه",
@@ -88,17 +109,20 @@ Persian Date: **${persianDate}**`,
88109
"اسفند",
89110
];
90111

91-
const dayName = persianDays[date.getDay()];
112+
const dayName = persianDays[date.getDay()+1];
92113
const hours = date.getHours().toString().padStart(2, "0");
93114
const minutes = date.getMinutes().toString().padStart(2, "0");
94-
95-
return `${dayName} ${persianDate.jd} ${persianMonths[persianDate.jm - 1]} ${
96-
persianDate.jy
97-
} ساعت: ${hours}:${minutes}`;
115+
116+
return `${dayName} ${persianDate.jd} ${persianMonths[persianDate.jm - 1]} ${ persianDate.jy } ساعت: ${hours}:${minutes}`;
98117
}
99118

100119
static toJalaali(gy: number, gm: number, gd: number) {
101-
// Convert Gregorian to Jalaali using an algorithm
120+
logger.debug("Converting Gregorian date to Jalaali.", "Date", {
121+
gy,
122+
gm,
123+
gd,
124+
});
125+
102126
const g_d_m = [
103127
0,
104128
31,
@@ -136,12 +160,20 @@ Persian Date: **${persianDate}**`,
136160
days = (days - 1) % 365;
137161
}
138162

139-
const jm =
140-
days < 186
141-
? 1 + Math.floor(days / 31)
142-
: 7 + Math.floor((days - 186) / 30);
143-
const jd = 1 + (days < 186 ? days % 31 : (days - 186) % 30);
163+
// Month Calculation
164+
const jm = days < 186 ? 1 + Math.floor(days / 31) : 7 + Math.floor((days - 186) / 30);
165+
166+
// Day Calculation
167+
const jd = (days < 186) ? (days % 31) : ((days - 186) % 30);
168+
169+
console.log("jm", jm);
170+
console.log("jd", jd);
144171

172+
logger.debug("Jalaali date converted.", "Date", {
173+
jy,
174+
jm,
175+
jd,
176+
});
145177
return { jy, jm, jd };
146178
}
147179
}

0 commit comments

Comments
 (0)