refactor: remove date-fns package (#9634)

* refactor: remove date-fns

Since TypeORM only uses one function from the date-fns library
it is unnecessary to install this large package.

* refactor: add DateUtils.parseDateAsISO

* refactor: reintroduced comment
This commit is contained in:
jdgjsag67251 2023-04-05 13:29:25 +02:00 committed by GitHub
parent 98f22052be
commit 1fcd9f3884
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11046 additions and 1667 deletions

12674
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -220,7 +220,6 @@
"buffer": "^6.0.3",
"chalk": "^4.1.2",
"cli-highlight": "^2.1.11",
"date-fns": "^2.29.3",
"debug": "^4.3.4",
"dotenv": "^16.0.3",
"glob": "^8.1.0",

View File

@ -1,5 +1,4 @@
import { ColumnMetadata } from "../metadata/ColumnMetadata"
import { parseISO } from "date-fns"
/**
* Provides utilities to transform hydrated and persisted data.
@ -47,22 +46,10 @@ export class DateUtils {
toUtc: boolean = false,
useMilliseconds = true,
): Date {
/**
* new Date(ISOString) is not a reliable parser to date strings.
* It's better to use 'date-fns' parser to parser string in ISO Format.
*
* The problem here is with wrong timezone.
*
* For example:
*
* ``new Date('2021-04-28')`` will generate `2021-04-28T00:00:00.000Z`
* in my timezone, which is not true for my timezone (GMT-0300). It should
* be `2021-04-28T03:00:00.000Z` as `new Date(2021, 3, 28)` generates.
*
* https://stackoverflow.com/a/2587398
*/
let date =
typeof mixedDate === "string" ? parseISO(mixedDate) : mixedDate
typeof mixedDate === "string"
? DateUtils.parseDateAsISO(mixedDate)
: mixedDate
if (toUtc)
date = new Date(
@ -279,4 +266,23 @@ export class DateUtils {
return String(value)
}
}
/**
* Parse a date without the UTC-offset of the device
*
* The problem here is with wrong timezone.
*
* For example:
*
* ``new Date('2021-04-28')`` will generate `2021-04-28T00:00:00.000Z`
* in my timezone, which is not true for my timezone (GMT-0300). It should
* be `2021-04-28T03:00:00.000Z` as `new Date(2021, 3, 28)` generates.
*/
private static parseDateAsISO(dateString: string): Date {
const date = new Date(dateString)
const offset = date.getTimezoneOffset() * 60 * 1000
const utc = date.getTime() + offset
return new Date(utc)
}
}