From ad3f9553c140136e688b83eafab424ffa97ed78e Mon Sep 17 00:00:00 2001 From: Elihuso Quigley Date: Thu, 19 Dec 2024 10:48:50 +0800 Subject: [PATCH 1/2] [O] Reduce the amount of reading at night --- src/logic/sunset.ts | 51 +++++++++++++++++++++++++++++++++++++++++++ src/views/Profile.vue | 25 ++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/logic/sunset.ts diff --git a/src/logic/sunset.ts b/src/logic/sunset.ts new file mode 100644 index 000000000..3949a5682 --- /dev/null +++ b/src/logic/sunset.ts @@ -0,0 +1,51 @@ +export interface Time { + hour: number; + minute: number; + second: number; +} + +function degToRad(deg: number): number { + return deg * Math.PI / 180; +} + +function radToDeg(rad: number): number { + return rad * 180.0 / Math.PI; +} + +function isLeap(year: number): boolean { + return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)); +} + +function numberOfDay(year: number, month: number, day: number): number { + const days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + let n = day; + if (month > 2) n += isLeap(year) ? 1 : 0; + while ((--month) > 0) n += days[month]; + return n; +} + +function sunset(year: number, month: number, day: number, latitude: number): number { + const dec = -degToRad(23.44) * Math.cos(2 * Math.PI / (365 + (isLeap(year) ? 1 : 0)) * (numberOfDay(year, month, day) + 10)); + return -Math.tan(degToRad(latitude)) * Math.tan(dec); +} + +function radToTime(rad: number): Time { + if (rad > Math.PI) rad -= 2 * Math.PI; + const sec = Math.floor(43200 + (rad * 43200 / Math.PI)); + const hour = Math.floor(sec / 3600); + const minute = Math.floor((sec - 3600 * hour) / 60); + const second = Math.floor((sec - 3600 * hour) % 60); + return { + hour: hour, + minute: minute, + second: second, + } +} + +export function sunsetTime(year: number, month: number, day: number, latitude: number): Time { + return radToTime(Math.acos(sunset(year, month, day, latitude))); +} + +export function sunriseTime(year: number, month: number, day: number, latitude: number): Time { + return radToTime(-Math.acos(sunset(year, month, day, latitude))); +} \ No newline at end of file diff --git a/src/views/Profile.vue b/src/views/Profile.vue index e1bc21efc..065c6119b 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -20,6 +20,7 @@ import {balloons, dataHost, Lang, limit, peopleUrl, replaceUrlVars, setLang, t} import {parsePeopleJson, Person} from "@/logic/data"; import {handleEasterEgg} from '@/logic/easterEgg' import {fetchWithLang, randint, scheduledTask, trim} from "@/logic/helper"; +import {sunriseTime, sunsetTime} from "@/logic/sunset"; import ProfileComments from "@/views/ProfileComments.vue"; import Swal from 'sweetalert2'; import urljoin from "url-join"; @@ -90,7 +91,29 @@ export default class Profile extends Vue { checkViewLimit(): boolean | void { if (this.screenshotMode) return - const config = limit + const config = (() => { + const now = new Date(); + const sunset = sunsetTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); + const sunrise = sunriseTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); + + if (now.getTime() > (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunset.hour, sunset.minute, sunset.second).getTime())) { + return { + warningLimit: Math.floor(limit.warningLimit / 2), + errorLimit: Math.floor(limit.errorLimit / 2), + cooldown: limit.cooldown + } + } + else if (now.getTime() < (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunrise.hour, sunrise.minute, sunrise.second).getTime())) { + return { + warningLimit: Math.floor(limit.warningLimit / 2), + errorLimit: Math.floor(limit.errorLimit / 2), + cooldown: limit.cooldown + } + } + return limit + })() + + console.log(config) const now = new Date() const [_time, _entries] = [localStorage.getItem("view_limit_time"), localStorage.getItem("view_limit_entries")] From b30037c6ed6fd8fe1049c01292d5e4be102742f7 Mon Sep 17 00:00:00 2001 From: Elihuso Quigley Date: Thu, 19 Dec 2024 15:46:57 +0800 Subject: [PATCH 2/2] [O] works in midnight --- src/views/Profile.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/views/Profile.vue b/src/views/Profile.vue index 065c6119b..9470bc7ad 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -96,14 +96,14 @@ export default class Profile extends Vue { const sunset = sunsetTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); const sunrise = sunriseTime(now.getFullYear(), now.getMonth() + 1, now.getDate(), 45.0); - if (now.getTime() > (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunset.hour, sunset.minute, sunset.second).getTime())) { + if (now.getTime() > (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunset.hour + 2, sunset.minute, sunset.second).getTime())) { return { warningLimit: Math.floor(limit.warningLimit / 2), errorLimit: Math.floor(limit.errorLimit / 2), cooldown: limit.cooldown } } - else if (now.getTime() < (new Date(now.getFullYear(), now.getMonth(), now.getDate(), sunrise.hour, sunrise.minute, sunrise.second).getTime())) { + else if (now.getTime() < (new Date(now.getFullYear(), now.getMonth(), now.getDate(), Math.floor(sunrise.hour * 0.5 + 2), sunrise.minute, sunrise.second).getTime())) { return { warningLimit: Math.floor(limit.warningLimit / 2), errorLimit: Math.floor(limit.errorLimit / 2),