Skip to content

Commit

Permalink
[PR] Merge pull request #62 from LS-KR/main
Browse files Browse the repository at this point in the history
[O] Reduce the amount of reading at night
  • Loading branch information
LS-KR authored Dec 20, 2024
2 parents 5fe74f8 + b30037c commit f9122be
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/logic/sunset.ts
Original file line number Diff line number Diff line change
@@ -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)));
}
25 changes: 24 additions & 1 deletion src/views/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 + 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(), 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),
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")]
Expand Down

0 comments on commit f9122be

Please sign in to comment.