Skip to content

Commit

Permalink
Merge branch 'main' into addConformanceTesting
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem authored Aug 24, 2023
2 parents df54430 + 5e4fb62 commit 125da5d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
2 changes: 2 additions & 0 deletions pkgs/intl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* 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: 41 additions & 22 deletions pkgs/intl/lib/src/intl/date_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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 @@ -68,9 +70,8 @@ class DateBuilder {

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

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

void _verify(int value, int min, int max, String desc, String originalInput,
Expand Down Expand Up @@ -181,12 +182,38 @@ 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!;
}

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

// 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 @@ -211,21 +237,14 @@ class DateBuilder {
// the upper-bound date.
//
// We don't actually need to check both bounds.
if (preliminaryResult.compareTo(upperDate) <= 0) {
if (preliminaryResult(upperCentury + year).compareTo(upperDate) <= 0) {
// Within range.
assert(preliminaryResult.compareTo(lowerDate) > 0);
assert(preliminaryResult(upperCentury + year).compareTo(lowerDate) > 0);
} else {
preliminaryResult = _dateTimeConstructor(lowerCentury + year, month,
dayOrDayOfYear, hour24, minute, second, fractionalSecond, utc);
estimatedYear = lowerCentury + year;
}
}

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

/// 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.setHasAmbiguousCentury(width == 2);
builder.hasAmbiguousCentury = width == 2;
}

String formatMonth(DateTime date) {
Expand Down
5 changes: 5 additions & 0 deletions pkgs/intl/test/date_time_strict_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ 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 125da5d

Please sign in to comment.