diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-08-22 17:07:33 +0200 |
---|---|---|
committer | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2017-08-25 11:05:36 +0200 |
commit | db6ad16520b210083bd9026a3648bb5211c10c28 (patch) | |
tree | d2455fde919bf6f154f64753683b1d6f4d491e15 /server/sonar-web/src/main/js/helpers/dates.ts | |
parent | f6276b3b6fecce2b160ed8bdc62a3e87439249e4 (diff) | |
download | sonarqube-db6ad16520b210083bd9026a3648bb5211c10c28.tar.gz sonarqube-db6ad16520b210083bd9026a3648bb5211c10c28.zip |
Use date parsing function from date-fns instead of new Date()
Diffstat (limited to 'server/sonar-web/src/main/js/helpers/dates.ts')
-rw-r--r-- | server/sonar-web/src/main/js/helpers/dates.ts | 56 |
1 files changed, 20 insertions, 36 deletions
diff --git a/server/sonar-web/src/main/js/helpers/dates.ts b/server/sonar-web/src/main/js/helpers/dates.ts index 5bbb50b3bff..3bba40df67e 100644 --- a/server/sonar-web/src/main/js/helpers/dates.ts +++ b/server/sonar-web/src/main/js/helpers/dates.ts @@ -17,9 +17,14 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - -const MILLISECONDS_IN_MINUTE = 60 * 1000; -const MILLISECONDS_IN_DAY = MILLISECONDS_IN_MINUTE * 60 * 24; +import { + differenceInDays as _differenceInDays, + differenceInSeconds as _differenceInSeconds, + differenceInYears as _differenceInYears, + isSameDay as _isSameDay, + parse, + startOfDay as _startOfDay +} from 'date-fns'; function pad(number: number) { if (number < 10) { @@ -28,31 +33,24 @@ function pad(number: number) { return number; } -function compareDateAsc(dateLeft: Date, dateRight: Date): number { - var timeLeft = dateLeft.getTime(); - var timeRight = dateRight.getTime(); +type ParsableDate = string | number | Date; - if (timeLeft < timeRight) { - return -1; - } else if (timeLeft > timeRight) { - return 1; - } else { - return 0; - } +export function parseDate(rawDate: ParsableDate): Date { + return parse(rawDate); } -export function toShortNotSoISOString(date: Date): string { +export function toShortNotSoISOString(rawDate: ParsableDate): string { + const date = parseDate(rawDate); return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()); } -export function toNotSoISOString(date: Date): string { +export function toNotSoISOString(rawDate: ParsableDate): string { + const date = parseDate(rawDate); return date.toISOString().replace(/\..+Z$/, '+0000'); } export function startOfDay(date: Date): Date { - const startDay = new Date(date); - startDay.setHours(0, 0, 0, 0); - return startDay; + return _startOfDay(date); } export function isValidDate(date: Date): boolean { @@ -60,31 +58,17 @@ export function isValidDate(date: Date): boolean { } export function isSameDay(dateLeft: Date, dateRight: Date): boolean { - const startDateLeft = startOfDay(dateLeft); - const startDateRight = startOfDay(dateRight); - return startDateLeft.getTime() === startDateRight.getTime(); + return _isSameDay(dateLeft, dateRight); } export function differenceInYears(dateLeft: Date, dateRight: Date): number { - const sign = compareDateAsc(dateLeft, dateRight); - const diff = Math.abs(dateLeft.getFullYear() - dateRight.getFullYear()); - const tmpLeftDate = new Date(dateLeft); - tmpLeftDate.setFullYear(dateLeft.getFullYear() - sign * diff); - const isLastYearNotFull = compareDateAsc(tmpLeftDate, dateRight) === -sign; - return sign * (diff - (isLastYearNotFull ? 1 : 0)); + return _differenceInYears(dateLeft, dateRight); } export function differenceInDays(dateLeft: Date, dateRight: Date): number { - const startDateLeft = startOfDay(dateLeft); - const startDateRight = startOfDay(dateRight); - const timestampLeft = - startDateLeft.getTime() - startDateLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE; - const timestampRight = - startDateRight.getTime() - startDateRight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE; - return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_DAY); + return _differenceInDays(dateLeft, dateRight); } export function differenceInSeconds(dateLeft: Date, dateRight: Date): number { - const diff = (dateLeft.getTime() - dateRight.getTime()) / 1000; - return diff > 0 ? Math.floor(diff) : Math.ceil(diff); + return _differenceInSeconds(dateLeft, dateRight); } |