parent
							
								
									a07806cd15
								
							
						
					
					
						commit
						88ecf60587
					
				| @ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest'; | |||||||
| import { | import { | ||||||
|   dateToExcelFormat, |   dateToExcelFormat, | ||||||
|   excelFormatToDate, |   excelFormatToDate, | ||||||
|  |   fromTimestamp, | ||||||
|   isExcelFormat, |   isExcelFormat, | ||||||
|   isISO8601DateTimeString, |   isISO8601DateTimeString, | ||||||
|   isISO9075DateString, |   isISO9075DateString, | ||||||
| @ -113,6 +114,46 @@ describe('date-time-converter models', () => { | |||||||
|       expect(isTimestamp('foo')).toBe(false); |       expect(isTimestamp('foo')).toBe(false); | ||||||
|       expect(isTimestamp('')).toBe(false); |       expect(isTimestamp('')).toBe(false); | ||||||
|     }); |     }); | ||||||
|  | 
 | ||||||
|  |     test('should return true for valid Unix timestamps in microseconds', () => { | ||||||
|  |       expect(isTimestamp('1701227351995845')).toBe(true); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('should return false for invalid Unix timestamps in microseconds', () => { | ||||||
|  |       expect(isTimestamp('170122735199584')).toBe(false); | ||||||
|  |       expect(isTimestamp('17012273519958')).toBe(false); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('isTimestampMicroSeconds', () => { | ||||||
|  |     test('should return true for valid Unix timestamps in microseconds', () => { | ||||||
|  |       expect(isTimestamp('1649792026123123')).toBe(true); | ||||||
|  |       expect(isTimestamp('1701227351995845')).toBe(true); | ||||||
|  |       expect(isTimestamp('0')).toBe(true); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('should return false for invalid Unix timestamps in microseconds', () => { | ||||||
|  |       expect(isTimestamp('foo')).toBe(false); | ||||||
|  |       expect(isTimestamp('')).toBe(false); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('should return false for invalid Unix timestamps not in microseconds', () => { | ||||||
|  |       expect(isTimestamp('170122735199584')).toBe(false); | ||||||
|  |       expect(isTimestamp('17012273519958')).toBe(false); | ||||||
|  |     }); | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   describe('fromTimestamp', () => { | ||||||
|  |     test('should return valid Date for valid Unix timestamps in microseconds', () => { | ||||||
|  |       expect(fromTimestamp('1649792026123123').toString()).toBe(new Date(1649792026123).toString()); | ||||||
|  |       expect(fromTimestamp('1701227351995845').toString()).toBe(new Date(1701227351995).toString()); | ||||||
|  |       expect(fromTimestamp('0').toString()).toBe(new Date(0).toString()); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     test('should return Date(0) for invalid Unix timestamps not in microseconds', () => { | ||||||
|  |       expect(fromTimestamp('170122735199584').toString()).toBe(new Date(0).toString()); | ||||||
|  |       expect(fromTimestamp('17012273519958').toString()).toBe(new Date(0).toString()); | ||||||
|  |     }); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   describe('isUTCDateString', () => { |   describe('isUTCDateString', () => { | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| import _ from 'lodash'; | import _ from 'lodash'; | ||||||
|  | import { addMilliseconds } from 'date-fns'; | ||||||
| 
 | 
 | ||||||
| export { | export { | ||||||
|   isISO8601DateTimeString, |   isISO8601DateTimeString, | ||||||
| @ -12,6 +13,8 @@ export { | |||||||
|   dateToExcelFormat, |   dateToExcelFormat, | ||||||
|   excelFormatToDate, |   excelFormatToDate, | ||||||
|   isExcelFormat, |   isExcelFormat, | ||||||
|  |   fromTimestamp, | ||||||
|  |   isTimestampMicroSeconds, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const ISO8601_REGEX | const ISO8601_REGEX | ||||||
| @ -35,7 +38,9 @@ const isISO9075DateString = createRegexMatcher(ISO9075_REGEX); | |||||||
| const isRFC3339DateString = createRegexMatcher(RFC3339_REGEX); | const isRFC3339DateString = createRegexMatcher(RFC3339_REGEX); | ||||||
| const isRFC7231DateString = createRegexMatcher(RFC7231_REGEX); | const isRFC7231DateString = createRegexMatcher(RFC7231_REGEX); | ||||||
| const isUnixTimestamp = createRegexMatcher(/^[0-9]{1,10}$/); | const isUnixTimestamp = createRegexMatcher(/^[0-9]{1,10}$/); | ||||||
| const isTimestamp = createRegexMatcher(/^[0-9]{1,13}$/); | const isTimestamp = createRegexMatcher(/^([0-9]{1,13}|[0-9]{16})$/); | ||||||
|  | const isTimestampMilliSeconds = createRegexMatcher(/^[0-9]{1,13}$/); | ||||||
|  | const isTimestampMicroSeconds = createRegexMatcher(/^[0-9]{16}$/); | ||||||
| const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/); | const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/); | ||||||
| 
 | 
 | ||||||
| const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX); | const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX); | ||||||
| @ -60,3 +65,14 @@ function dateToExcelFormat(date: Date) { | |||||||
| function excelFormatToDate(excelFormat: string | number) { | function excelFormatToDate(excelFormat: string | number) { | ||||||
|   return new Date((Number(excelFormat) - 25569) * 86400 * 1000); |   return new Date((Number(excelFormat) - 25569) * 86400 * 1000); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | function fromTimestamp(timestamp: string, type: 'auto' | 'milliseconds' | 'microseconds' = 'auto') { | ||||||
|  |   let milliSeconds = 0; | ||||||
|  |   if (type === 'microseconds' || isTimestampMicroSeconds(timestamp)) { | ||||||
|  |     milliSeconds = Number(timestamp) / 1000; | ||||||
|  |   } | ||||||
|  |   else if (type === 'milliseconds' || isTimestampMilliSeconds(timestamp)) { | ||||||
|  |     milliSeconds = Number(timestamp); | ||||||
|  |   } | ||||||
|  |   return addMilliseconds(new Date(0), milliSeconds); | ||||||
|  | } | ||||||
|  | |||||||
| @ -10,12 +10,12 @@ import { | |||||||
|   isDate, |   isDate, | ||||||
|   isValid, |   isValid, | ||||||
|   parseISO, |   parseISO, | ||||||
|   parseJSON, |  | ||||||
| } from 'date-fns'; | } from 'date-fns'; | ||||||
| import type { DateFormat, ToDateMapper } from './date-time-converter.types'; | import type { DateFormat, ToDateMapper } from './date-time-converter.types'; | ||||||
| import { | import { | ||||||
|   dateToExcelFormat, |   dateToExcelFormat, | ||||||
|   excelFormatToDate, |   excelFormatToDate, | ||||||
|  |   fromTimestamp, | ||||||
|   isExcelFormat, |   isExcelFormat, | ||||||
|   isISO8601DateTimeString, |   isISO8601DateTimeString, | ||||||
|   isISO9075DateString, |   isISO9075DateString, | ||||||
| @ -73,7 +73,7 @@ const formats: DateFormat[] = [ | |||||||
|   { |   { | ||||||
|     name: 'Timestamp', |     name: 'Timestamp', | ||||||
|     fromDate: date => String(getTime(date)), |     fromDate: date => String(getTime(date)), | ||||||
|     toDate: ms => parseJSON(+ms), |     toDate: ms => fromTimestamp(ms), | ||||||
|     formatMatcher: date => isTimestamp(date), |     formatMatcher: date => isTimestamp(date), | ||||||
|   }, |   }, | ||||||
|   { |   { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user