From c6a850f7d426ea3179cbfe3fd6f5bcac5c370d9e Mon Sep 17 00:00:00 2001 From: Krafpy Date: Wed, 4 Sep 2024 01:10:05 +0200 Subject: [PATCH] Minor filename case change for consistency --- dist/main/time/kronometertime.js | 123 +++++++++++++++++++++++++ dist/main/time/time.js | 2 +- src/main/time/kronometertime.ts | 149 +++++++++++++++++++++++++++++++ src/main/time/time.ts | 2 +- 4 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 dist/main/time/kronometertime.js create mode 100644 src/main/time/kronometertime.ts diff --git a/dist/main/time/kronometertime.js b/dist/main/time/kronometertime.js new file mode 100644 index 0000000..eb4e27c --- /dev/null +++ b/dist/main/time/kronometertime.js @@ -0,0 +1,123 @@ +export class KronometerTime { + constructor(date, config) { + this.config = config; + this._exactDate = 0; + this.utDisplayMode = "offset"; + if (typeof date == "number") { + this.dateSeconds = date; + } + else { + this.displayYDHMS = date; + } + } + stringYDHMS(precision, display) { + let year, day, hour, minute, second; + if (display == "ut") { + const ydhms = this.displayYDHMS; + year = ydhms.year; + day = ydhms.day; + hour = ydhms.hour; + minute = ydhms.minute; + second = ydhms.second; + } + else { + const t = this._exactDate; + year = Math.floor(t / 31536000); + day = Math.floor((t % 31536000) / 86400); + hour = Math.floor((t % 86400) / 3600); + minute = Math.floor((t % 3600) / 60); + second = (t % 60); + } + let hmsStr = ""; + switch (precision) { + case "hms": hmsStr = `:${(second >= 10 ? "" : "0")}${second.toFixed(0)}${hmsStr}`; + case "hm": hmsStr = `:${(minute >= 10 ? "" : "0")}${minute}${hmsStr}`; + } + hmsStr = `${(hour >= 10 ? "" : "0")}${hour}${hmsStr}`; + if (precision == "h") { + hmsStr += "h"; + } + if (display == "ut") { + return `Year ${year} - Day ${day} - ${hmsStr}`; + } + else { + return `T+ ${year}y - ${day}d - ${hmsStr}`; + } + } + toUT(from) { + if (typeof from == "number") + return new KronometerTime(from + this._exactDate, this.config); + else + return new KronometerTime(from.dateSeconds + this._exactDate, this.config); + } + get dateSeconds() { + return this._exactDate; + } + set dateSeconds(date) { + this._exactDate = date; + } + get displayYDHMS() { + const daysInShortYear = Math.floor(this.config.orbitalPeriod / this.config.solarDayLength); + const daysInLongYear = Math.ceil(this.config.orbitalPeriod / this.config.solarDayLength); + const shortYear = this.config.solarDayLength * daysInShortYear; + let chanceOfLeapDay = (this.config.orbitalPeriod / this.config.solarDayLength) % 1; + if (daysInShortYear === daysInLongYear) { + chanceOfLeapDay = 0; + } + let left = this._exactDate; + let leap = 0; + let year = 0; + let day = 0; + let hours = 0; + let minutes = 0; + let seconds = 0; + while (left >= shortYear) { + left -= shortYear; + leap += chanceOfLeapDay; + year++; + while (Math.floor(leap) >= 1) { + leap = Math.max(leap - 1, 0); + if (left >= this.config.solarDayLength) { + left -= this.config.solarDayLength; + } + else { + year--; + day += daysInShortYear; + } + } + } + day += Math.floor(left / this.config.solarDayLength); + left -= Math.floor(left / this.config.solarDayLength) * this.config.solarDayLength; + if (left >= this.config.solarDayLength) { + day++; + left -= this.config.solarDayLength; + } + hours = Math.floor(left / 3600); + left -= hours * 3600; + minutes = Math.floor(left / 60); + left -= minutes * 60; + seconds = Math.floor(left); + return { + year: year + 1, + day: day + 1, + hour: hours, + minute: minutes, + second: seconds + }; + } + set displayYDHMS(dateYDHMS) { + let { year, day, hour, minute, second } = dateYDHMS; + const daysInShortYear = Math.floor(this.config.orbitalPeriod / this.config.solarDayLength); + let chanceOfLeapDay = (this.config.orbitalPeriod / this.config.solarDayLength) % 1; + let exactDate = 0; + let days = (day - 1) + Math.floor((year - 1) * chanceOfLeapDay) + ((year - 1) * daysInShortYear); + exactDate += days * this.config.solarDayLength; + exactDate += hour * 3600; + exactDate += minute * 60; + exactDate += second; + this._exactDate = exactDate; + } + get defaultDate() { + return this.config.initialDate; + } +} diff --git a/dist/main/time/time.js b/dist/main/time/time.js index ee60152..47f8ee1 100644 --- a/dist/main/time/time.js +++ b/dist/main/time/time.js @@ -1,6 +1,6 @@ import { RealKSPTime } from "./realtime.js"; import { BaseKSPTime } from "./basetime.js"; -import { KronometerTime } from "./KronometerTime.js"; +import { KronometerTime } from "./kronometertime.js"; export function KSPTime(date, config, dateMode) { switch (config.type) { case "base": diff --git a/src/main/time/kronometertime.ts b/src/main/time/kronometertime.ts new file mode 100644 index 0000000..c24d6bb --- /dev/null +++ b/src/main/time/kronometertime.ts @@ -0,0 +1,149 @@ +export class KronometerTime implements IKSPTime { + private _exactDate: number = 0; + + public readonly utDisplayMode = "offset"; + + constructor(date: number | DateYDHMS, public readonly config: KronometerSettings){ + if(typeof date == "number") { + this.dateSeconds = date; + } else { + this.displayYDHMS = date; + } + } + + public stringYDHMS(precision: "h" | "hm" | "hms", display: "emt" | "ut"): string { + let year: number, day: number, hour: number, minute: number, second: number; + if(display == "ut"){ + const ydhms = this.displayYDHMS; + year = ydhms.year; + day = ydhms.day; + hour = ydhms.hour; + minute = ydhms.minute; + second = ydhms.second; + } else { + const t = this._exactDate; + year = Math.floor(t / 31536000); + day = Math.floor((t % 31536000) / 86400); + hour = Math.floor((t % 86400) / 3600); + minute = Math.floor((t % 3600) / 60); + second = (t % 60); + } + + let hmsStr = ""; + switch(precision){ + case "hms": hmsStr = `:${(second >= 10 ? "" : "0")}${second.toFixed(0)}${hmsStr}`; + case "hm": hmsStr = `:${(minute >= 10 ? "" : "0")}${minute}${hmsStr}`; + } + hmsStr = `${(hour >= 10 ? "" : "0")}${hour}${hmsStr}`; + if(precision == "h"){ + hmsStr += "h"; + } + + if(display == "ut"){ + return `Year ${year} - Day ${day} - ${hmsStr}`; + } else { + return `T+ ${year}y - ${day}d - ${hmsStr}`; + } + } + + public toUT(from: number | IKSPTime): IKSPTime { + if(typeof from == "number") + return new KronometerTime(from + this._exactDate, this.config); + else + return new KronometerTime(from.dateSeconds + this._exactDate, this.config); + } + + public get dateSeconds(){ + return this._exactDate; + } + + public set dateSeconds(date: number){ + this._exactDate = date; + } + + public get displayYDHMS(): DateYDHMS { + const daysInShortYear = Math.floor(this.config.orbitalPeriod / this.config.solarDayLength); + const daysInLongYear = Math.ceil(this.config.orbitalPeriod / this.config.solarDayLength); + + const shortYear = this.config.solarDayLength * daysInShortYear; + + let chanceOfLeapDay = (this.config.orbitalPeriod / this.config.solarDayLength) % 1; + + if (daysInShortYear === daysInLongYear) { + chanceOfLeapDay = 0; + } + + let left = this._exactDate; + + let leap = 0; + let year = 0; + let day = 0; + let hours = 0; + let minutes = 0; + let seconds = 0; + + while (left >= shortYear) { + left -= shortYear; + leap += chanceOfLeapDay; + year++; + + while (Math.floor(leap) >= 1) { + leap = Math.max(leap - 1, 0); + + if (left >= this.config.solarDayLength) { + left -= this.config.solarDayLength; + } else { + year--; + day += daysInShortYear; + } + } + } + + day += Math.floor(left / this.config.solarDayLength); + left -= Math.floor(left / this.config.solarDayLength) * this.config.solarDayLength; + + if (left >= this.config.solarDayLength) { + day++; + left -= this.config.solarDayLength; + } + + hours = Math.floor(left / 3600); + left -= hours * 3600; + + minutes = Math.floor(left / 60); + left -= minutes * 60; + + seconds = Math.floor(left); + + return { + year: year + 1, + day: day + 1, + hour: hours, + minute: minutes, + second: seconds + }; + } + + public set displayYDHMS(dateYDHMS: DateYDHMS){ + let {year, day, hour, minute, second} = dateYDHMS; + const daysInShortYear = Math.floor(this.config.orbitalPeriod / this.config.solarDayLength); + + let chanceOfLeapDay = (this.config.orbitalPeriod / this.config.solarDayLength) % 1; + + let exactDate = 0; + + let days = (day - 1) + Math.floor((year - 1) * chanceOfLeapDay) + ((year - 1) * daysInShortYear); + + exactDate += days * this.config.solarDayLength; + + exactDate += hour * 3600; + exactDate += minute * 60; + exactDate += second; + + this._exactDate = exactDate; + } + + public get defaultDate(){ + return this.config.initialDate; + } +} \ No newline at end of file diff --git a/src/main/time/time.ts b/src/main/time/time.ts index 8fbe775..e90cd4a 100644 --- a/src/main/time/time.ts +++ b/src/main/time/time.ts @@ -1,6 +1,6 @@ import {RealKSPTime} from "./realtime.js"; import {BaseKSPTime} from "./basetime.js"; -import {KronometerTime} from "./KronometerTime.js"; +import {KronometerTime} from "./kronometertime.js"; export function KSPTime(date: number | DateYDHMS, config: BaseTimeSettings | RealTimeSettings | KronometerSettings, dateMode: "elapsed" | "offset"): IKSPTime { switch (config.type) {