Skip to content

Instantly share code, notes, and snippets.

@RyadPasha
Last active July 26, 2023 15:07
Show Gist options
  • Save RyadPasha/ac3509aad9e4a1a91663777817c82e63 to your computer and use it in GitHub Desktop.
Save RyadPasha/ac3509aad9e4a1a91663777817c82e63 to your computer and use it in GitHub Desktop.
TimeUtility is a utility class that provides a set of methods to handle time-related operations.
/**
* TimeUtility is a utility class that provides a set of methods to handle time-related operations.
*
* @class DatabaseManager
* @author Mohamed Riyad <m@ryad.me>
* @link https://RyadPasha.com
* @copyright Copyright (C) 2023 RyadPasha. All rights reserved.
* @license MIT
* @version 1.0.0-2023.07.26
* @see {@link https://momentjs.com/} for more information on Moment.js
* @see {@link https://gist.github.com/RyadPasha/ac3509aad9e4a1a91663777817c82e63} for updates
*/
import moment from 'moment-timezone'
import config from '../config/config'
class TimeUtility {
/**
* The default timezone to be used in case an invalid timezone is provided or no timezone is specified.
*/
private readonly defaultTimezone: string
/**
* Create a TimeHandler instance.
*
* @param {string} timezone - Optional. The timezone to be used as the default timezone. If not provided or invalid, the default timezone will be GMT (UTC).
*/
constructor(timezone?: string) {
this.defaultTimezone = this.validateTimezone(timezone) || 'Etc/GMT'
}
/**
* Get the current time in the default or specified timezone.
*
* @returns {moment.Moment} The current time as a Moment object.
*/
getCurrentTime(): moment.Moment {
return moment.tz(this.defaultTimezone)
}
/**
* Format a given time according to a specified format string in the default or specified timezone.
*
* @param {moment.Moment} time - The time to be formatted (a Moment object, a Date object, or a date string).
* @param {string} [formatString='YYYY-MM-DD HH:mm:ss'] - Optional. The format string to be used. Defaults to 'YYYY-MM-DD HH:mm:ss'.
* @returns {string} The formatted time as a string.
*/
formatTime(time: moment.Moment, formatString: string = 'YYYY-MM-DD HH:mm:ss'): string {
return time.format(formatString)
}
/**
* Convert a given time to the specified timezone.
*
* @param {moment.Moment | Date | string} time - The time to be converted (a Moment object, a Date object, or a date string).
* @param {string} timezone - The timezone to which the time should be converted.
* @returns {moment.Moment} The converted time as a Moment object.
*/
convertToTimezone(time: moment.Moment | Date | string, timezone: string): moment.Moment {
const tz = this.validateTimezone(timezone)
return moment.tz(time, tz)
}
/**
* Validate if the provided timezone is valid and exists in the moment-timezone database.
* If the timezone is valid, it will be returned; otherwise, the default timezone will be used.
*
* @param {string} timezone - The timezone to be validated.
* @returns {string | null} The validated timezone or null if an invalid timezone was provided.
*/
private validateTimezone(timezone?: string): string | null {
if (timezone && moment.tz.names().includes(timezone)) {
return timezone
} else {
console.warn('Invalid timezone provided. Fallback to GMT (UTC).')
return null
}
}
}
const timeUtility = new TimeUtility(config.TIMEZONE)
export default timeUtility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment