Skip to content

Commit

Permalink
Copybara import of the project:
Browse files Browse the repository at this point in the history
--
74641d7 by Moritz <[email protected]>:

Fix #483

--
f21ac06 by Moritz <[email protected]>:

Add changelog

--
7613552 by Moritz <[email protected]>:

Changes as per review

PiperOrigin-RevId: 559577268
  • Loading branch information
Googler authored and copybara-github committed Aug 23, 2023
1 parent 5e4fb62 commit cdd78a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 49 deletions.
2 changes: 0 additions & 2 deletions pkgs/intl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
* Update to CLDR v43.
* Switch QAR currency name to Riyal.
* Add license headers to some files.
* Update CVE currency symbol.
* Add `EEEEE` skeleton for `DateFormat`, closing [#176](https://github.com/dart-lang/i18n/issues/176).
* Switch to `3.0.0` SDK.
* Fix issue [#483](https://github.com/dart-lang/i18n/issues/483) about date parsing with a `yy` skeleton.

## 0.18.1
* Update ruble sign and update corresponding test.
Expand Down
63 changes: 22 additions & 41 deletions pkgs/intl/lib/src/intl/date_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class DateBuilder {
/// Ignored if `year < 0` or `year >= 100`.
bool _hasAmbiguousCentury = false;

bool get _hasCentury => !_hasAmbiguousCentury || year < 0 || year >= 100;

/// The locale, kept for logging purposes when there's an error.
final String _locale;

Expand Down Expand Up @@ -70,8 +68,9 @@ class DateBuilder {

/// Sets whether [year] should be treated as ambiguous because it lacks a
/// century.
set hasAmbiguousCentury(bool isAmbiguous) =>
_hasAmbiguousCentury = isAmbiguous;
void setHasAmbiguousCentury(bool isAmbiguous) {
_hasAmbiguousCentury = isAmbiguous;
}

void setMonth(int x) {
month = x;
Expand Down Expand Up @@ -141,7 +140,7 @@ class DateBuilder {
// We have the day of the month, compare directly.
_verify(day, date.day, date.day, 'day', s, date);
}
_verify(_estimatedYear, date.year, date.year, 'year', s, date);
_verify(year, date.year, date.year, 'year', s, date);
}

void _verify(int value, int min, int max, String desc, String originalInput,
Expand Down Expand Up @@ -182,38 +181,12 @@ class DateBuilder {
// TODO(alanknight): Validate the date, especially for things which
// can crash the VM, e.g. large month values.
if (_date != null) return _date!;
DateTime preliminaryResult = _dateTimeConstructor(
_estimatedYear,
month,
dayOrDayOfYear,
hour24,
minute,
second,
fractionalSecond,
utc,
);
if (utc && _hasCentury) {
_date = preliminaryResult;
} else {
_date = _correctForErrors(preliminaryResult, retries);
}
return _date!;
}

int get _estimatedYear {
DateTime preliminaryResult(int year) => _dateTimeConstructor(
year,
month,
dayOrDayOfYear,
hour24,
minute,
second,
fractionalSecond,
utc,
);
int estimatedYear;
if (_hasCentury) {
estimatedYear = year;
DateTime preliminaryResult;
final hasCentury = !_hasAmbiguousCentury || year < 0 || year >= 100;
if (hasCentury) {
preliminaryResult = _dateTimeConstructor(year, month, dayOrDayOfYear,
hour24, minute, second, fractionalSecond, utc);
} else {
var now = clock.now();
if (utc) {
Expand All @@ -225,7 +198,8 @@ class DateBuilder {
var upperDate = _offsetYear(now, 100 - lookBehindYears);
var lowerCentury = (lowerDate.year ~/ 100) * 100;
var upperCentury = (upperDate.year ~/ 100) * 100;
estimatedYear = upperCentury + year;
preliminaryResult = _dateTimeConstructor(upperCentury + year, month,
dayOrDayOfYear, hour24, minute, second, fractionalSecond, utc);

// Our interval must be half-open since there otherwise could be ambiguity
// for a date that is exactly 20 years in the future or exactly 80 years
Expand All @@ -237,14 +211,21 @@ class DateBuilder {
// the upper-bound date.
//
// We don't actually need to check both bounds.
if (preliminaryResult(upperCentury + year).compareTo(upperDate) <= 0) {
if (preliminaryResult.compareTo(upperDate) <= 0) {
// Within range.
assert(preliminaryResult(upperCentury + year).compareTo(lowerDate) > 0);
assert(preliminaryResult.compareTo(lowerDate) > 0);
} else {
estimatedYear = lowerCentury + year;
preliminaryResult = _dateTimeConstructor(lowerCentury + year, month,
dayOrDayOfYear, hour24, minute, second, fractionalSecond, utc);
}
}
return estimatedYear;

if (utc && hasCentury) {
_date = preliminaryResult;
} else {
_date = _correctForErrors(preliminaryResult, retries);
}
return _date!;
}

/// Given a local DateTime, check for errors and try to compensate for them if
Expand Down
2 changes: 1 addition & 1 deletion pkgs/intl/lib/src/intl/date_format_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ class _DateFormatPatternField extends _DateFormatField {

void parseYear(StringStack input, DateBuilder builder) {
handleNumericField(input, builder.setYear);
builder.hasAmbiguousCentury = width == 2;
builder.setHasAmbiguousCentury(width == 2);
}

String formatMonth(DateTime date) {
Expand Down
5 changes: 0 additions & 5 deletions pkgs/intl/test/date_time_strict_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,4 @@ void main() {
expect(format.tryParseStrict('14:0:59'), DateTime(1970, 1, 1, 14, 0, 59));
});
});

test('Year in yy format', () {
expect(DateFormat('yy').parseStrict('23'), DateTime(2023));
expect(DateFormat('yyyy').parseStrict('2023'), DateTime(2023));
});
}

0 comments on commit cdd78a9

Please sign in to comment.