Skip to content

Automatic Daylight Saving and Date Adjustments #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pages/alarms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const Alarms: NextPage = () => {
const [serverTime, setServerTime] = useState<DateTime>(
currDate.setZone(regionTZ)
)
const [selectedDate, setSelectedDate] = useState(currDate.setZone(regionTZ))
const [selectedDate, setSelectedDate] = useState(currDate.setZone(regionTZ).set({hour: 0, minute: 0, second: 0, millisecond: 0}))

const [gameEvents, setGameEvents] = useState<Array<GameEvent> | undefined>(
undefined
Expand Down Expand Up @@ -185,7 +185,7 @@ const Alarms: NextPage = () => {
if (regionTZ !== undefined) {
setMounted(true)
setServerTime(currDate.setZone(regionTZ))
setSelectedDate(currDate.setZone(regionTZ))
setSelectedDate(currDate.setZone(regionTZ).set({hour: 0, minute: 0, second: 0, millisecond: 0}))
}
}, [regionTZ])

Expand All @@ -200,14 +200,14 @@ const Alarms: NextPage = () => {
useEffect(() => {
const timer = setInterval(() => {
let now = DateTime.now()
if (currDate.endOf('day').diffNow().toMillis() < 0) setSelectedDate(now)
if (serverTime.endOf('day').diffNow().toMillis() < 0) setSelectedDate(now.setZone(regionTZ).set({hour: 0, minute: 0, second: 0, millisecond: 0}))
setCurrDate(now)
setServerTime(now.setZone(regionTZ))
}, 1000)
return () => {
clearInterval(timer) // Return a function to clear the timer so that it will stop being called on unmount
}
}, [regionTZ, view24HrTime, viewLocalizedTime, selectedDate])
}, [regionTZ, view24HrTime, viewLocalizedTime, selectedDate, serverTime.day])

// clear disabled alarm when alarm expires
useEffect(() => {
Expand Down Expand Up @@ -539,7 +539,7 @@ const Alarms: NextPage = () => {
</button>
<button
className="btn relative text-2xl text-stone-200"
onClick={(e) => setSelectedDate(serverTime)}
onClick={(e) => setSelectedDate(serverTime.setZone(regionTZ).set({hour: 0, minute: 0, second: 0, millisecond: 0}))}
>
<span>
{selectedDate.monthLong} {selectedDate.day}
Expand Down
24 changes: 19 additions & 5 deletions util/static.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { RegionKey } from './types/types'
import { DateTime } from 'luxon'

/* IANA Codes relative to approximate AWS server locations, as per http://ec2-reachability.amazonaws.com/
NA East: America/New_York (Herndon, VA, US)
NA West: America/Los_Angeles (San Francisco, CA, US)
EU Central: Europe/Berlin (Frankfurt, Germany)
EU West: Europe/Dublin (Dublin, Ireland)
South America: America/Sao_Paulo (São Paulo, Brazil)
*/

function DSTOffset(IANAString: string): string {
const offset = DateTime.now().setZone(IANAString).get('offset') / 60 || 0
return (`UTC${offset >= 0 ? "+" + offset : offset}`) // UTC+offset, UTC-offset
}

export const RegionTimeZoneMapping: { [K in RegionKey]: string } = {
'US West': 'UTC-7',
'US East': 'UTC-4',
'EU Central': 'UTC+1',
'EU West': 'UTC-0',
'South America': 'UTC-4',
'US West': DSTOffset('America/Los_Angeles'),
'US East': DSTOffset('America/New_York'),
'EU Central': DSTOffset('Europe/Berlin'),
'EU West': DSTOffset('Europe/Dublin'),
'South America': DSTOffset('America/Sao_Paulo'),
}