|
| 1 | +import { getNepalDateAndTime } from './utils' |
| 2 | + |
| 3 | +/** |
| 4 | + * Represents a Gregorian date/time in Nepal's timezone (Asia/Kathmandu, UTC+05:45). |
| 5 | + * Behaves like a JavaScript `Date` object, with all getters returning values in Nepal's timezone. |
| 6 | + * Does not convert to the Nepali (Bikram Sambat) calendar. |
| 7 | + * |
| 8 | + * @example |
| 9 | + * |
| 10 | + * // Current date/time in Nepal timezone |
| 11 | + * const date = new NepalTimezoneDate(); |
| 12 | + * date.getYear(); // Returns 2025 |
| 13 | + * date.getMinutes(); // Returns current minutes in Nepal timezone |
| 14 | + * date.toString(); // Returns e.g., "2025-09-01 16:01:00 GMT+0545" |
| 15 | + * |
| 16 | + * @example |
| 17 | + * |
| 18 | + * // From a specific UTC date |
| 19 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 20 | + * date.toString(); // Returns "2024-12-28 20:45:35 GMT+0545" |
| 21 | + */ |
| 22 | +class NepalTimezoneDate { |
| 23 | + private _date: Date |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a NepalTimezoneDate instance for Asia/Kathmandu timezone (UTC+05:45). |
| 27 | + * Accepts the same arguments as the JavaScript `Date` constructor. |
| 28 | + * |
| 29 | + * @param args - Arguments compatible with the `Date` constructor (e.g., timestamp, string, or year/month/day). |
| 30 | + * @example |
| 31 | + * |
| 32 | + * const now = new NepalTimezoneDate(); // Current date/time in Nepal timezone |
| 33 | + * const specific = new NepalTimezoneDate(2024, 11, 28, 15, 0, 35); // 2024-12-28 15:00:35 UTC |
| 34 | + * const fromString = new NepalTimezoneDate('2024-12-28T15:00:35Z'); // From ISO string |
| 35 | + * const fromTimestamp = new NepalTimezoneDate(1703766035170); // From timestamp |
| 36 | + */ |
| 37 | + constructor(...args: ConstructorParameters<typeof Date>) { |
| 38 | + this._date = new Date(...args) |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the four-digit year in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 43 | + * |
| 44 | + * @returns The year (e.g., 2024). |
| 45 | + * @example |
| 46 | + * |
| 47 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 48 | + * date.getYear(); // Returns 2024 |
| 49 | + */ |
| 50 | + getYear(): number { |
| 51 | + return getNepalDateAndTime(this._date).year |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the month index (0-11) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 56 | + * |
| 57 | + * @returns The month index (0 = January, 11 = December). |
| 58 | + * @example |
| 59 | + * |
| 60 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 61 | + * date.getMonth(); // Returns 11 |
| 62 | + */ |
| 63 | + getMonth(): number { |
| 64 | + return getNepalDateAndTime(this._date).month0 |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Returns the day of the month (1-31) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 69 | + * |
| 70 | + * @returns The day of the month. |
| 71 | + * @example |
| 72 | + * |
| 73 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 74 | + * date.getDate(); // Returns 28 |
| 75 | + */ |
| 76 | + getDate(): number { |
| 77 | + return getNepalDateAndTime(this._date).day |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the hour (0-23) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 82 | + * |
| 83 | + * @returns The hour of the day. |
| 84 | + * @example |
| 85 | + * |
| 86 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 87 | + * date.getHours(); // Returns 20 |
| 88 | + */ |
| 89 | + getHours(): number { |
| 90 | + return getNepalDateAndTime(this._date).hour |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns the minute (0-59) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 95 | + * |
| 96 | + * @returns The minute of the hour. |
| 97 | + * @example |
| 98 | + * |
| 99 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 100 | + * date.getMinutes(); // Returns 45 |
| 101 | + */ |
| 102 | + getMinutes(): number { |
| 103 | + return getNepalDateAndTime(this._date).minute |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns the second (0-59) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 108 | + * |
| 109 | + * @returns The second of the minute. |
| 110 | + * @example |
| 111 | + * |
| 112 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 113 | + * date.getSeconds(); // Returns 35 |
| 114 | + */ |
| 115 | + getSeconds(): number { |
| 116 | + return getNepalDateAndTime(this._date).second |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the millisecond (0-999) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 121 | + * |
| 122 | + * @returns The millisecond of the second. |
| 123 | + * @example |
| 124 | + * |
| 125 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35.170Z'); |
| 126 | + * date.getMilliseconds(); // Returns 170 |
| 127 | + */ |
| 128 | + getMilliseconds(): number { |
| 129 | + return getNepalDateAndTime(this._date).ms |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Returns the day of the week (0-6, Sunday-Saturday) in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 134 | + * |
| 135 | + * @returns The day of the week (0 = Sunday, 6 = Saturday). |
| 136 | + * @example |
| 137 | + * |
| 138 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 139 | + * date.getDay(); // Returns 6 (Saturday) |
| 140 | + */ |
| 141 | + getDay(): number { |
| 142 | + return getNepalDateAndTime(this._date).weekDay |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns the Unix timestamp (milliseconds since epoch, UTC). |
| 147 | + * |
| 148 | + * @returns The timestamp in milliseconds. |
| 149 | + * @example |
| 150 | + * |
| 151 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 152 | + * date.getTime(); // Returns 1703766035000 |
| 153 | + */ |
| 154 | + getTime(): number { |
| 155 | + return this._date.getTime() |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Returns a formatted string in Nepal timezone (Asia/Kathmandu, UTC+05:45). |
| 160 | + * Format: `YYYY-MM-DD HH:mm:ss GMT+0545` |
| 161 | + * |
| 162 | + * @returns A string representing the date and time in Nepal timezone. |
| 163 | + * @example |
| 164 | + * |
| 165 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 166 | + * date.toString(); // Returns "2024-12-28 20:45:35 GMT+0545" |
| 167 | + */ |
| 168 | + toString(): string { |
| 169 | + const np = getNepalDateAndTime(this._date) |
| 170 | + return ( |
| 171 | + `${np.year}-${String(np.month0 + 1).padStart(2, '0')}-${String(np.day).padStart(2, '0')} ` + |
| 172 | + `${String(np.hour).padStart(2, '0')}:${String(np.minute).padStart(2, '0')}:${String(np.second).padStart(2, '0')} GMT+0545` |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Returns a copy of the underlying JavaScript `Date` object. |
| 178 | + * |
| 179 | + * @returns A new `Date` instance with the same timestamp. |
| 180 | + * @example |
| 181 | + * |
| 182 | + * const date = new NepalTimezoneDate('2024-12-28T15:00:35Z'); |
| 183 | + * const jsDate = date.toDate(); // Returns Date object for 2024-12-28T15:00:35Z |
| 184 | + */ |
| 185 | + toDate(): Date { |
| 186 | + return new Date(this._date) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +export default NepalTimezoneDate |
0 commit comments