Skip to content

Commit c45b781

Browse files
committed
fixed timezone default to nepal
1 parent f912bd7 commit c45b781

File tree

8 files changed

+87
-57
lines changed

8 files changed

+87
-57
lines changed

bikram-calendar/src/bikram.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
* TypeScript implementation of Bikram calendar conversion
43
* Copyright (c) 2024 onwards - khumnath cg, [email protected]
@@ -43,8 +42,9 @@ export class BikramDate {
4342
const { gYear, gMonth, gDay } = this.toGregorian(year, month, day);
4443
this.englishDate = new Date(gYear, gMonth - 1, gDay);
4544
} else {
46-
// Default to current date
47-
this.englishDate = new Date();
45+
const now = new Date();
46+
now.setHours(0, 0, 0, 0);
47+
this.englishDate = now;
4848
const { bsYear, bsMonth, bsDay } = this.fromGregorian(
4949
this.englishDate.getFullYear(),
5050
this.englishDate.getMonth() + 1,
@@ -58,16 +58,14 @@ export class BikramDate {
5858

5959
// Create a new BikramDate from a JavaScript Date
6060
static fromDate(date: Date): BikramDate {
61-
const bikramDate = new BikramDate();
62-
const { bsYear, bsMonth, bsDay } = bikramDate.fromGregorian(
63-
date.getFullYear(),
64-
date.getMonth() + 1,
65-
date.getDate()
61+
const d = new Date(date);
62+
d.setHours(0, 0, 0, 0);
63+
const { bsYear, bsMonth, bsDay } = new BikramDate().fromGregorian(
64+
d.getFullYear(),
65+
d.getMonth() + 1,
66+
d.getDate()
6667
);
67-
bikramDate.year = bsYear;
68-
bikramDate.month = bsMonth;
69-
bikramDate.day = bsDay;
70-
return bikramDate;
68+
return new BikramDate(bsYear, bsMonth, bsDay);
7169
}
7270

7371
// Convert to JavaScript Date
@@ -109,8 +107,8 @@ export class BikramDate {
109107
// Convert AD (Gregorian) date to BS (Bikram Sambat)
110108
fromGregorian(gYear: number, gMonth: number, gDay: number): { bsYear: number; bsMonth: number; bsDay: number } {
111109
// Reference date: 1 Baisakh 2000 BS = 14 April 1943 AD
112-
const refDate = new Date(1943, 3, 14); // Month is 0-indexed in JS Date
113-
const targetDate = new Date(gYear, gMonth - 1, gDay);
110+
const refDate = new Date(Date.UTC(1943, 3, 14)); // Month is 0-indexed in JS Date
111+
const targetDate = new Date(Date.UTC(gYear, gMonth - 1, gDay));
114112

115113
// Calculate difference in days
116114
const timeDiff = targetDate.getTime() - refDate.getTime();

bikram-calendar/src/converter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { BikramDate, BS_START_YEAR, BS_END_YEAR } from './bikram';
32

43
export type BikramDateObj = {
@@ -43,14 +42,15 @@ export const nepaliDaysEn = [
4342
];
4443

4544
// Get Today's Bikram Date
46-
export const getToday = (): BikramDateObj => {
47-
const bikramDate = new BikramDate();
45+
export const getToday = (date?: Date): BikramDateObj => {
46+
const today = date || new Date();
47+
const bikramDate = BikramDate.fromDate(today);
4848

4949
return {
5050
year: bikramDate.getYear(),
5151
month: bikramDate.getMonth(),
5252
day: bikramDate.getDate(),
53-
englishDate: bikramDate.toJsDate(),
53+
englishDate: today,
5454
};
5555
};
5656

docs/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<link href="https://fonts.googleapis.com/css2?family=Lohit+Devanagari&display=swap" rel="stylesheet">
1818
<!-- Print stylesheet -->
1919
<link rel="stylesheet" href="./print.css" media="print">
20-
<script type="module" crossorigin src="./assets/index-9O1kINYt.js"></script>
20+
<script type="module" crossorigin src="./assets/index-k7-yqMtV.js"></script>
2121
<link rel="stylesheet" crossorigin href="./assets/index-fmTXRpHI.css">
2222
</head>
2323

docs/assets/index-9O1kINYt.js renamed to docs/assets/index-k7-yqMtV.js

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<link href="https://fonts.googleapis.com/css2?family=Lohit+Devanagari&display=swap" rel="stylesheet">
1818
<!-- Print stylesheet -->
1919
<link rel="stylesheet" href="./print.css" media="print">
20-
<script type="module" crossorigin src="./assets/index-9O1kINYt.js"></script>
20+
<script type="module" crossorigin src="./assets/index-k7-yqMtV.js"></script>
2121
<link rel="stylesheet" crossorigin href="./assets/index-fmTXRpHI.css">
2222
</head>
2323

src/components/BikramCalendar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const BikramCalendar: React.FC = () => {
121121
days={calendarState.currentView.days}
122122
startWeekDay={calendarState.currentView.startWeekDay}
123123
englishStartDate={calendarState.currentView.englishStartDate}
124-
currentDate={calendarState.today.year === calendarState.currentView.year && calendarState.today.month === calendarState.currentView.month ? calendarState.today : undefined}
124+
currentDate={calendarState.today}
125125
selectedDate={calendarState.selectedDate || undefined}
126126
onDateSelect={calendarState.handleDateSelect}
127127
useNepaliLanguage={calendarState.useNepaliLanguage}

src/hooks/useCalendarState.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState, useEffect } from 'react';
2-
import { BikramDateObj, BikramMonth, getToday, getBikramMonth, getNepaliDigits, containsNepaliDigits, getEnglishDigits } from '../utils/bikramConverter';
2+
import { BikramDateObj, BikramMonth, getToday, getBikramMonth, getNepaliDigits, containsNepaliDigits, getEnglishDigits, getNepalTodayDate } from '../utils/bikramConverter';
33
import { EventModalData } from '../types/events';
44
import { CalendarEvent } from '../types/events';
55
import { loadEventsForYear } from '../utils/events';
@@ -8,7 +8,17 @@ import { calculateTithi } from '../utils/tithiCalculation';
88
import { getAllEventText, getAllEventDetails } from '../utils/events';
99

1010
export function useCalendarState() {
11-
const [today, setToday] = useState<BikramDateObj>(getToday());
11+
const [today, setToday] = useState<BikramDateObj>(() => getToday(getNepalTodayDate()));
12+
13+
// Keep today in sync with Nepal time
14+
useEffect(() => {
15+
const timer = setInterval(() => {
16+
setToday(getToday(getNepalTodayDate()));
17+
}, 60000); // Update every minute
18+
19+
return () => clearInterval(timer);
20+
}, []);
21+
1222
const [currentView, setCurrentView] = useState<BikramMonth>(() => getBikramMonth(today.year, today.month));
1323
const [selectedDate, setSelectedDate] = useState<BikramDateObj | null>(null);
1424
const [useNepaliLanguage, setUseNepaliLanguage] = useState<boolean>(true);
@@ -61,7 +71,7 @@ export function useCalendarState() {
6171
};
6272

6373
const handleTodayClick = () => {
64-
const todayDate = getToday();
74+
const todayDate = getToday(getNepalTodayDate());
6575
setCurrentView(getBikramMonth(todayDate.year, todayDate.month));
6676
setSelectedDate(todayDate);
6777
setYearInput(useNepaliLanguage ? getNepaliDigits(todayDate.year) : todayDate.year.toString());

src/utils/bikramConverter.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
// Re-export all needed functionality from the bikram-calendar package
32
import {
4-
getToday,
3+
getToday as originalGetToday,
54
getBikramMonth,
65
convertToBikram,
76
convertToEnglish,
@@ -16,8 +15,31 @@ import {
1615
type BikramMonth
1716
} from '../../bikram-calendar/src/converter';
1817

18+
export function getToday(date?: Date): BikramDateObj {
19+
const today = date || new Date();
20+
const bikramDate = convertToBikram(today);
21+
return {
22+
year: bikramDate.year,
23+
month: bikramDate.month,
24+
day: bikramDate.day,
25+
englishDate: today
26+
};
27+
}
28+
29+
export function getNepalTodayDate(): Date {
30+
const now = new Date();
31+
const utc = now.getTime() + (now.getTimezoneOffset() * 60000);
32+
const nepalOffsetMs = 5.75 * 60 * 60 * 1000;
33+
const nepalNow = new Date(utc + nepalOffsetMs);
34+
return new Date(
35+
nepalNow.getFullYear(),
36+
nepalNow.getMonth(),
37+
nepalNow.getDate(),
38+
0, 0, 0, 0
39+
);
40+
}
41+
1942
export {
20-
getToday,
2143
getBikramMonth,
2244
convertToBikram,
2345
convertToEnglish,

0 commit comments

Comments
 (0)