InitializePokerusTimer:
	ld hl, wRTCBaseDay
	ld de, wPokerusTimerDay
	ld bc, wStartDateEnd - wPokerusTimerDay
	rst CopyBytes
	ret

CheckRTCTimers:
	ld hl, RTCTimers
	jr .handleLoop
.loop
	push af
	ld a, [hli]
	ld e, a
	ld a, [hli]
	ld d, a
	ld a, [hli]
	ld c, a
	ld a, [hli]
	ld b, a
	pop af
	call CheckTimerDependentEvent
	CheckEngine ENGINE_RTC_TIMERS_ENABLED
	ret z
.handleLoop
	ld a, [hli]
	and a
	jr nz, .loop
	ret

rtctimer: MACRO
	db \1 ; time unit basepoint
	dw \2 ; timer pointer
	dw \3 ; function pointer
	rept 7 - \1
		shift
		db \3 ; time to compare by
	endr
ENDM

RTCTimers:
	rtctimer RTC_TIMER_SECONDS, wTimeMachineTimer, EnableTimeMachine, 0, 0, 1, 0, 0, 0
RTCTimersExcludingTimeMachineTimer:
	rtctimer RTC_TIMER_DAYS, wDailyResetTimer, ResetDailyFlags, 0, 0, 1
	rtctimer RTC_TIMER_DAYS, wPokerusTimerDay, ApplyPokerusTick, 0, 0, 1
	db 0

CheckTimerDependentEvent:
; bc = function
; de = timer
; hl = time comparison
	push de
	push bc
	push af
	push hl
	ld h, d
	ld l, e
	ld b, a
	call CalcTimeSince
	pop hl
	pop af
	ld c, a
	ld a, 7
	sub c
	ld c, a
	call CompareTimeSinceWithTimeRequiredToPass
	pop de ; function
	pop bc ; timer
	ret c

	push hl ; end address
	push bc
	call _de_
	pop hl ; timer
	call CopyCurrentTimeToTimer
	pop hl ; end address
	ret

CompareTimeSinceWithTimeRequiredToPass:
; Return no carry if enough time has passed
	ld de, wYearsSince
	call StringCmp
	push af
	ld b, 0
	add hl, bc
	pop af
	ret

UpdateTimerStatusesIfResetTime:
	call CheckRTCStatus
	add a
	ret nc

DoTimeMachine:
	ld a, $ff
	ld [wDailyFlags], a
	ResetEngine ENGINE_RTC_TIMERS_ENABLED
	ld hl, wTimeMachineTimer

CopyCurrentTimeToTimer:
	push hl
	call UpdateTime
	pop hl
	ld a, [wCurDay]
	ld [hli], a
	ld a, [hHours]
	ld [hli], a
	ld a, [hMinutes]
	ld [hli], a
	ld a, [hSeconds]
	ld [hli], a
	ld a, [wCurYear]
	ld [hli], a
	ld a, [wCurMonth]
	ld [hl], a
	ret

ResetDailyFlags:
	xor a
	ld [wDailyFlags], a
	ret

ApplyPokerusTick:
	ld a, [wDaysSince]
	ld b, a
	jpba _ApplyPokerusTick

EnableTimeMachine:
	CheckEngine ENGINE_RTC_TIMERS_ENABLED
	ret nz
	ld hl, RTCTimersExcludingTimeMachineTimer
	jr .handleLoop
.loop
	push af
	push hl
	ld a, [hli]
	ld h, [hl]
	ld l, a
	call CopyCurrentTimeToTimer
	pop hl
	pop af
	ld bc, 4
	add hl, bc
	ld c, a
	ld a, 7
	sub c
	add l
	ld l, a
	jr nc, .handleLoop
	inc h
.handleLoop
	ld a, [hli]
	and a
	jr nz, .loop
	SetEngine ENGINE_RTC_TIMERS_ENABLED
	ret

CalcTimeSince:
; calc time since using the index in b as a basepoint
	push hl
	push bc
	xor a
	ld hl, wYearsSince
	ld [hli], a
	ld [hli], a
	ld [hli], a
	ld [hli], a
	ld [hli], a
	ld [hl], a
	call UpdateTime
	pop bc
	pop hl
	
	and a
	inc hl
	inc hl
	inc hl
	ld d, h
	ld e, l
	dec b
	jr z, .FromSecs
	dec b
	jr z, .FromMins
	dec hl
	dec b
	jr z, .FromHours
	dec hl
	dec b
	jr z, .FromDays
	push hl
	ld c, e
	ld a, [wCurMonth]
	ld e, a
	ld h, d
	ld a, [wCurYear]
	ld d, a
	ld a, h
	pop hl
	dec b
	jr z, .FromMonths
	ld h, a
	ld l, c
	inc hl
	dec b
	jr z, .FromYears
	ret

.FromSecs:
	ld a, [hSeconds]
	sub [hl]
	jr nc, .skip1
	add 60
.skip1
	ld [wSecondsSince], a ; seconds since
.FromMins:
	dec hl
	ld a, [hMinutes]
	sbc [hl]
	jr nc, .skip2
	add 60
.skip2
	ld [wMinutesSince], a ; minutes since
.FromHours:
	dec hl
	ld a, [hHours]
	sbc [hl]
	jr nc, .skip3
	add 24
.skip3
	ld [wHoursSince], a ; hours since
.FromDays:
	dec hl
	ld a, [wCurMonth]
	ld e, a
	ld a, [wCurYear]
	ld d, a
	ld a, [wCurDay]
	sbc [hl]
	jr nc, .skip4
	push hl
	push af
	push de
	inc hl
	inc hl
	inc hl
	inc hl
	ld a, [hli]
	ld d, a
	ld e, [hl]
	call GetMaxDateTableEntryBasedOnMonthAndYear
	pop de
	pop af
	add [hl]
	pop hl
.skip4
	ld [wDaysSince], a
.FromMonths:
	inc hl
	inc hl
	inc hl
	inc hl
	inc hl
	ld a, e
	sbc [hl]
	jr nc, .skip5
	add 12
.skip5
	ld [wMonthsSince], a
	dec hl
.FromYears:
	ld a, d
	sbc [hl]
	ld [wYearsSince], a
	ret
