All files time.ts

82.66% Statements 62/75
81.81% Branches 36/44
89.18% Functions 33/37
82.43% Lines 61/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 3121x             1x             42x           32x       2x       2x       2x       2x       2x               12x       5x       5x       5x       15x       2x       3x           1x 1x 1x 1x 1x 1x 1x 1x           1x             53x           244x       36x             36x       36x       36x                           36x             36x             36x             10x                                         45x 45x 24x       21x 2x       19x 2x     17x 2x         15x         45x                     5x                     40x                           8x 8x 8x       8x       3x       13x       13x                     13x     13x                                               13x 377x       13x   18x              
import * as number from './number';
 
/**
 * 時間を扱う。
 *
 * 細かいのは時間できたときに、うん。
 */
export class TimeSpan {
 
	/**
	 * 生成。
	 *
	 * @param _ticks ミリ秒。
	 */
	private constructor(private _ticks: number) {
	}
 
	//#region property
 
	public get ticks(): number {
		return this._ticks;
	}
 
	public get totalMilliseconds(): number {
		return this._ticks;
	}
 
	public get totalSeconds(): number {
		return this._ticks / 1000;
	}
 
	public get totalMinutes(): number {
		return this._ticks / 1000 / 60;
	}
 
	public get totalHours(): number {
		return this._ticks / 1000 / 60 / 60;
	}
 
	public get totalDays(): number {
		return this._ticks / 1000 / 60 / 60 / 24;
	}
 
	//#endregion
 
	//#region function
 
	public static fromMilliseconds(milliSeconds: number): TimeSpan {
		return new TimeSpan(milliSeconds);
	}
 
	public static fromSeconds(seconds: number): TimeSpan {
		return new TimeSpan(seconds * 1000);
	}
 
	public static fromMinutes(minutes: number): TimeSpan {
		return new TimeSpan(minutes * 60 * 1000);
	}
 
	public static fromHours(hours: number): TimeSpan {
		return new TimeSpan(hours * 60 * 60 * 1000);
	}
 
	public static fromDays(hours: number): TimeSpan {
		return new TimeSpan(hours * 24 * 60 * 60 * 1000);
	}
 
	public equals(timeSpan: TimeSpan): boolean {
		return this.ticks === timeSpan.ticks;
	}
 
	public compare(timeSpan: TimeSpan): number {
		return this.ticks - timeSpan.ticks;
	}
 
	//#endregion
}
 
export enum DayOfWeek {
	Sunday = 0,
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
}
 
/**
 * UTC周り多分間違ってる。
 */
export class DateTime {
 
	/**
	 * 生成。
	 *
	 * @param _timestamp タイムスタンプ。
	 */
	private constructor(private readonly _timestamp: Date, private readonly _isUtc: boolean) {
	}
 
	//#region property
 
	public get isUtc(): boolean {
		return this._isUtc;
	}
 
	public get year(): number {
		return this.isUtc
			? this._timestamp.getUTCFullYear()
			: this._timestamp.getFullYear()
			;
	}
 
	public get month(): number {
		const month = this.isUtc
			? this._timestamp.getUTCMonth()
			: this._timestamp.getMonth()
			;
		return month + 1;
	}
 
	public get day(): number {
		return this.isUtc
			? this._timestamp.getUTCDate()
			: this._timestamp.getDate()
			;
	}
 
	public get dayOfWeek(): DayOfWeek {
		return this.isUtc
			? this._timestamp.getUTCDay()
			: this._timestamp.getDay()
			;
	}
 
	public get hour(): number {
		return this.isUtc
			? this._timestamp.getUTCHours()
			: this._timestamp.getHours()
			;
	}
 
	public get minute(): number {
		return this.isUtc
			? this._timestamp.getUTCMinutes()
			: this._timestamp.getMinutes()
			;
	}
 
	public get second(): number {
		return this.isUtc
			? this._timestamp.getUTCSeconds()
			: this._timestamp.getSeconds()
			;
	}
 
	public get millisecond(): number {
		return this.isUtc
			? this._timestamp.getUTCMilliseconds()
			: this._timestamp.getMilliseconds()
			;
	}
 
	//#endregion
 
	//#region function
 
	public static now(): DateTime {
		return new DateTime(new Date(), false);
	}
 
	public static utcNow(): DateTime {
		const now = new Date();
		const utc = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
		return new DateTime(new Date(utc), true);
	}
 
	private static createCore(isUtc: boolean, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number): DateTime {
		let date: Date | null = null;
		if (hour === undefined) {
			date = isUtc
				? new Date(Date.UTC(year, month - 1, day))
				: new Date(year, month - 1, day)
				;
		} else if (minute === undefined) {
			date = isUtc
				? new Date(Date.UTC(year, month - 1, day, hour))
				: new Date(year, month - 1, day, hour)
				;
		} else if (second === undefined) {
			date = isUtc
				? new Date(Date.UTC(year, month - 1, day, hour, minute))
				: new Date(year, month - 1, day, hour, minute);
		} else if (millisecond === undefined) {
			date = isUtc
				? new Date(Date.UTC(year, month - 1, day, hour, minute, second))
				: new Date(year, month - 1, day, hour, minute, second)
				;
		} else {
			date = isUtc
				? new Date(Date.UTC(year, month - 1, day, hour, minute, second, millisecond))
				: new Date(year, month - 1, day, hour, minute, second, millisecond)
				;
		}
		return new DateTime(date, isUtc);
	}
 
	//#region create
 
	public static create(year: number, month: number, day: number): DateTime;
	public static create(year: number, month: number, day: number, hour: number): DateTime;
	public static create(year: number, month: number, day: number, hour: number, minute: number): DateTime;
	public static create(year: number, month: number, day: number, hour: number, minute: number, second: number): DateTime;
	public static create(year: number, month: number, day: number, hour: number, minute: number, second: number, millisecond: number): DateTime;
	public static create(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number): DateTime {
		return this.createCore(false, year, month, day, hour, minute, second, millisecond);
	}
	//#endregion
 
	//#region createUtc
	public static createUtc(year: number, month: number, day: number): DateTime;
	public static createUtc(year: number, month: number, day: number, hour: number): DateTime;
	public static createUtc(year: number, month: number, day: number, hour: number, minute: number): DateTime;
	public static createUtc(year: number, month: number, day: number, hour: number, minute: number, second: number): DateTime;
	public static createUtc(year: number, month: number, day: number, hour: number, minute: number, second: number, millisecond: number): DateTime;
	public static createUtc(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number): DateTime {
		return this.createCore(true, year, month, day, hour, minute, second, millisecond);
	}
	//#endregion
 
	public static parse(timestamp: string): DateTime | null {
		const date = new Date(timestamp);
		Iif (Number.isNaN(date.getDate())) {
			return null;
		}
 
		return new DateTime(date, false);
	}
 
	public add(timeSpan: TimeSpan): DateTime {
		const current = this._timestamp.getTime();
		const addTime = new Date(current + timeSpan.ticks);
		return new DateTime(addTime, this.isUtc);
	}
 
	public equals(dateTime: DateTime): boolean {
		return this._timestamp.getTime() === dateTime._timestamp.getTime();
	}
 
	public compare(dateTime: DateTime): number {
		return this._timestamp.getTime() - dateTime._timestamp.getTime();
	}
 
	public toString(format?: string): string {
		Iif (format === undefined) {
			return this._timestamp.toISOString();
		}
 
		switch (format) {
			case 'U':
				return this._timestamp.toUTCString();
 
			case 'S':
				return this._timestamp.toString();
 
			case 'L':
				return this._timestamp.toLocaleString();
 
			default:
				break;
		}
 
		const map = new Map([
			//['y', (this.year.toString())],
			//['yy', number.padding(this.year - 2000, 2, '0')],
			//['yyy', number.padding(this.year, 3, '0')],
			['yyyy', number.padding(this.year, 4, '0')],
			['yyyyy', number.padding(this.year, 5, '0')],
 
 
			['M', this.month.toString()],
			['MM', number.padding(this.month, 2, '0')],
 
			['d', this.day.toString()],
			['dd', number.padding(this.day, 2, '0')],
 
			['H', this.hour.toString()],
			['HH', number.padding(this.hour, 2, '0')],
 
			['m', this.minute.toString()],
			['mm', number.padding(this.minute, 2, '0')],
 
			['s', this.second.toString()],
			['ss', number.padding(this.second, 2, '0')],
		]);
 
		const pattern = Array.from(map.keys())
			.sort((a, b) => b.length - a.length)
			.join('|')
			;
 
		return format.replace(
			new RegExp('(' + pattern + ')', 'g'),
			m => map.get(m) ?? m
		);
	}
 
	//#endregion
}