-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Original commit message: [date] Skip leading zeros when parsing date string 1. Skip leading zeros when parsing date string 2. Add necessary unittests Bug: v8:12256 Change-Id: Ibc1f320382a2e33175f7f57542c8fe48afd05fa8 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3223239 Reviewed-by: Shu-yu Guo <syg@chromium.org> Reviewed-by: Igor Sheludko <ishell@chromium.org> Commit-Queue: Shu-yu Guo <syg@chromium.org> Cr-Commit-Position: refs/heads/main@{#77592} Refs: v8/v8@cced52a PR-URL: #40656 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
f260bbc
commit 7cbbe0d
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2021 the V8 project authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
const dates = [{ year: '2021', month: '10', day: '22', hour: '10', minute: '12', second: '32' }, | ||
{ year: '2021', month: '8', day: '3', hour: '9', minute: '9', second: '6' }]; | ||
|
||
for (let date of dates) { | ||
const { year, month, day, hour, minute, second } = date; | ||
const s0 = `${year}-${month}-${day} ${hour}:${minute}:${second}Z`; | ||
|
||
// V8 reads at most kMaxSignificantDigits (9) to build the value of a numeral, | ||
// so let's test up to 9 leading zeros. | ||
|
||
// For years | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${'0'.repeat(i) + year}-${month}-${day} ${hour}:${minute}:${second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// For months | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${year}-${'0'.repeat(i) + month}-${day} ${hour}:${minute}:${second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// For days | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${year}-${month}-${'0'.repeat(i) + day} ${hour}:${minute}:${second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// For hours | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${year}-${month}-${day} ${'0'.repeat(i) + hour}:${minute}:${second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// For minutes | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${year}-${month}-${day} ${hour}:${'0'.repeat(i) + minute}:${second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// For seconds | ||
for (let i = 1; i < 10; i++) { | ||
const s1 = `${year}-${month}-${day} ${hour}:${minute}:${'0'.repeat(i) + second}Z`; | ||
assertTrue(new Date(s0).getTime() == new Date(s1).getTime()); | ||
} | ||
|
||
// With same input date string, | ||
// Date() and Date.parse() should return the same date | ||
assertTrue(new Date(s0).getTime() == Date.parse(s0)); | ||
} |