Created
August 8, 2025 10:58
-
-
Save ikenfin/9b1ea235a07c7f2e7549338b570712e1 to your computer and use it in GitHub Desktop.
TimeInterval value object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Represents a time interval with conversion between units | |
*/ | |
class TimeInterval { | |
/** | |
* @private | |
* @param {number} seconds | |
*/ | |
constructor(seconds) { | |
this._seconds = seconds | |
} | |
// Conversion constants | |
static MILLIS_IN_SECOND = 1000 | |
static SECONDS_IN_MINUTE = 60 | |
static SECONDS_IN_HOUR = 3600 | |
static SECONDS_IN_DAY = 86400 | |
/** | |
* Create from seconds | |
* @param {number} seconds | |
* @returns {TimeInterval} | |
*/ | |
static fromSeconds(seconds) { | |
return new TimeInterval(seconds) | |
} | |
/** | |
* Create from minutes | |
* @param {number} minutes | |
* @returns {TimeInterval} | |
*/ | |
static fromMinutes(minutes) { | |
return new TimeInterval(minutes * this.SECONDS_IN_MINUTE) | |
} | |
/** | |
* Create from hours | |
* @param {number} hours | |
* @returns {TimeInterval} | |
*/ | |
static fromHours(hours) { | |
return new TimeInterval(hours * this.SECONDS_IN_HOUR) | |
} | |
/** | |
* Create from days | |
* @param {number} days | |
* @returns {TimeInterval} | |
*/ | |
static fromDays(days) { | |
return new TimeInterval(days * this.SECONDS_IN_DAY) | |
} | |
/** | |
* Convert to milliseconds | |
* @returns {number} | |
*/ | |
toMillis() { | |
return this._seconds * TimeInterval.MILLIS_IN_SECOND | |
} | |
/** | |
* Convert to seconds | |
* @returns {number} | |
*/ | |
toSeconds() { | |
return this._seconds | |
} | |
/** | |
* Convert to minutes | |
* @returns {number} | |
*/ | |
toMinutes() { | |
return this._seconds / TimeInterval.SECONDS_IN_MINUTE | |
} | |
/** | |
* Convert to hours | |
* @returns {number} | |
*/ | |
toHours() { | |
return this._seconds / TimeInterval.SECONDS_IN_HOUR | |
} | |
/** | |
* Convert to days | |
* @returns {number} | |
*/ | |
toDays() { | |
return this._seconds / TimeInterval.SECONDS_IN_DAY | |
} | |
} | |
// Экспорт для использования в модульной системе | |
export { TimeInterval } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment