Skip to content

Commit e4970a6

Browse files
authored
Merge pull request #30 from twentyTwo/bugfix/timezone_issue
Bugfix/timezone issue
2 parents 5ea9ef1 + 5c1e9d3 commit e4970a6

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "simple-coding-time-tracker",
33
"displayName": "Simple Coding Time Tracker",
44
"description": "Track and visualize your coding time across projects",
5-
"version": "0.3.4",
5+
"version": "0.3.5",
66
"publisher": "noorashuvo",
77
"license": "MIT",
88
"icon": "icon-sctt.png",

src/database.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ export class Database {
2525
}
2626
}
2727

28+
private getLocalDateString(date: Date): string {
29+
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
30+
.toISOString()
31+
.split('T')[0];
32+
}
33+
2834
async addEntry(date: Date, project: string, timeSpent: number) {
29-
const dateString = date.toISOString().split('T')[0];
35+
const dateString = this.getLocalDateString(date);
3036
const entries = this.getEntries();
3137
const existingEntryIndex = entries.findIndex(entry => entry.date === dateString && entry.project === project);
3238

@@ -74,7 +80,7 @@ export class Database {
7480
}
7581

7682
async resetTodayTime(): Promise<void> {
77-
const today = new Date().toISOString().split('T')[0];
83+
const today = this.getLocalDateString(new Date());
7884
const entries = this.getEntries();
7985
const updatedEntries = entries.filter(entry => entry.date !== today);
8086
await this.context.globalState.update('timeEntries', updatedEntries);

src/timeTracker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,14 @@ export class TimeTracker implements vscode.Disposable {
196196
return 'Other';
197197
}
198198

199+
private getLocalDateString(date: Date): string {
200+
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
201+
.toISOString()
202+
.split('T')[0];
203+
}
204+
199205
getTodayTotal(): number {
200-
const today = new Date().toISOString().split('T')[0];
206+
const today = this.getLocalDateString(new Date());
201207
const entries = this.database.getEntries();
202208
const todayTotal = entries
203209
.filter((entry: TimeEntry) => entry.date === today)
@@ -217,7 +223,7 @@ export class TimeTracker implements vscode.Disposable {
217223
}
218224

219225
getCurrentProjectTime(): number {
220-
const today = new Date().toISOString().split('T')[0];
226+
const today = this.getLocalDateString(new Date());
221227
const currentProject = this.getCurrentProject();
222228
const entries = this.database.getEntries();
223229
const currentProjectTime = entries
@@ -266,8 +272,8 @@ export class TimeTracker implements vscode.Disposable {
266272

267273
private getTotalSince(startDate: Date): number {
268274
const entries = this.database.getEntries();
269-
const startDateString = startDate.toISOString().split('T')[0];
270-
const now = new Date().toISOString().split('T')[0];
275+
const startDateString = this.getLocalDateString(startDate);
276+
const now = this.getLocalDateString(new Date());
271277

272278
const filteredEntries = entries.filter(entry =>
273279
entry.date >= startDateString && entry.date <= now

0 commit comments

Comments
 (0)