74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
const hoursperday = 24
|
|
const minutesperhour = 60
|
|
const secondsperminute = 60
|
|
const secondms = 1000
|
|
const minutems = secondms * secondsperminute
|
|
const hourms = minutems * minutesperhour
|
|
const dayms = hourms * hoursperday
|
|
|
|
const solaryear = 365.242
|
|
|
|
const yeardays = 366
|
|
const leapchange = -6
|
|
const leapyeardays = yeardays + leapchange
|
|
|
|
const monthdays = 30
|
|
|
|
const leapfreq = 8
|
|
|
|
const exleapcyclelength = 800;
|
|
const extraleapyear = 500;
|
|
|
|
const exleapcycledays = (exleapcyclelength * yeardays) + leapchange + ((exleapcyclelength / leapfreq) * leapchange)
|
|
|
|
const leapcycledays = (leapfreq * yeardays) + leapchange;
|
|
|
|
const unityzero = Date.parse("2000-03-08T00:00:00.000Z");
|
|
|
|
function unixtounity(unixtime) {
|
|
const unitytime = unixtime - unityzero;
|
|
const days = unitytime / dayms;
|
|
|
|
const exleapcycles = Math.floor(days / exleapcycledays);
|
|
|
|
const exleapcycleprogress = days - exleapcycles * exleapcycledays;
|
|
|
|
const leapcycles = Math.floor(exleapcycleprogress / leapcycledays);
|
|
|
|
const leapcycleprogress = exleapcycleprogress - leapcycles * leapcycledays;
|
|
|
|
let years;
|
|
|
|
let yearprogress;
|
|
|
|
if (leapcycleprogress > leapyeardays) {
|
|
|
|
years = Math.floor((leapcycleprogress - leapyeardays) / yeardays) + 1;
|
|
|
|
yearprogress = leapcycleprogress - ((years * yeardays) + leapchange);
|
|
|
|
} else {
|
|
|
|
years = 0;
|
|
|
|
yearprogress = leapcycleprogress;
|
|
|
|
}
|
|
|
|
const months = Math.floor(yearprogress / monthdays);
|
|
|
|
const monthprogress = yearprogress - months * monthdays;
|
|
|
|
const date = Math.floor(monthprogress);
|
|
|
|
return `${12000 + (exleapcycles * exleapcyclelength) + (leapcycles * leapfreq) + years}-${months + 1}-${date + 1}`
|
|
|
|
}
|
|
|
|
const oldtimes = new Date();
|
|
oldtimes.setYear(-600)
|
|
|
|
console.log(unixtounity(Date.now()))
|
|
console.log(unixtounity(Date.parse("3500-03-03")))
|
|
console.log(unixtounity(Date.parse("0010-12-30")))
|
|
console.log(unixtounity(oldtimes.getTime())) |