Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing two-digit year 16 with results in 0016 and not 2016 #263

Closed
DanTup opened this issue Aug 26, 2016 · 13 comments
Closed

Parsing two-digit year 16 with results in 0016 and not 2016 #263

DanTup opened this issue Aug 26, 2016 · 13 comments

Comments

@DanTup
Copy link

DanTup commented Aug 26, 2016

import 'package:intl/intl.dart';
final dateFormat = new DateFormat("d/M/y H:m:s");
print(dateFormat.parse("01/02/16 12:11:12").year);

This code results in a year of 0016 and not 2016. Same thing happens whether I use y or yy in the format. Unfortunately it's tricky to fix this, because you can't even .add 2000 years, because the Duration class only goes up to days!

Other languages make assumptions about two-digit years being 19/20; it probably makes sense to do the same.

@andrewmcdnz
Copy link

andrewmcdnz commented May 15, 2019

Nearly 3 years later I've stumbled across the same issue via the unit tests in my Flutter app. The issue occurs even when using the specific example from the comments in date_format.dart (pattern: "MM/dd/yy", input "01/11/12" ; the resulting year is 0012). This contradicts in the description in date_format.dart (line 202) which states it should resolve to 2012. Needs to be fixed.

Tested with intl: ^0.15.8

@altherat
Copy link

A hacky workaround by adding Duration could be:

dateFormat.parse('01/02/16 12:11:12').add(Duration(milliseconds: DateTime(1970 + 2000).millisecondsSinceEpoch));

@jamesderlin
Copy link
Contributor

jamesderlin commented May 8, 2020

@altherat That hacky workaround isn't correct since it will change the time. (That is, adding 24 * 60 * 60 seconds to a date might not necessarily be the same as incrementing the day by 1 due to leap seconds, daylight saving, ...).

An even bigger problem with your hack is that when this bug is fixed, your hack will be broken.

You'd be better off taking the result of DateFormat.parse and constructing a new DateTime with an adjusted year if it's between 0 and 99 inclusive.

@johnneijzen
Copy link

@jamesderlin that would problem but they did not fixed bug for 4 years already the closest to fixed is dart-archive/intl#125 but it got rejected

@harlandgomez
Copy link

harlandgomez commented May 26, 2020

I think @jamesderlin was right all along. I used the hacky workaround and after sometime it was giving me a wrong month. So I switched back to adding 2000 in the year as in var year = (dateFormat.parse("01/02/16 12:11:12").year + 2000)..

@jamesderlin
Copy link
Contributor

I think @jamesderlin was right all along. I used the hacky workaround and after sometime it was giving me a wrong month. So I switched back to adding 2000 in the year as in var year = (dateFormat.parse("01/02/16 12:11:12").year + 2000)..

Please do not unconditionally add 2000. As I mentioned, such hacks will break when this bug is fixed. Add 2000 only to 2-digit years.

And yes, a fix is forthcoming.

@harlandgomez
Copy link

@jamesderlin, i was just showing how to simply add 2000, of course, there are checks before it arrives to that..

@jamesderlin
Copy link
Contributor

@jamesderlin, i was just showing how to simply add 2000, of course, there are checks before it arrives to that..

Okay, I just don't want people to copy-and-paste that workaround blindly. I have seen other people also suggest workarounds that omit any checks.

@harlandgomez
Copy link

harlandgomez commented May 28, 2020

@jamesderlin, i was just showing how to simply add 2000, of course, there are checks before it arrives to that..

Okay, I just don't want people to copy-and-paste that workaround blindly. I have seen other people also suggest workarounds that omit any checks.

Understood, simple check can be?

    final twoDigitYear = dateFormat.parse("01/02/16 12:11:12").year;
    if(0 <= twoDigitYear && twoDigitYear <= 99){
      year = (twoDigitYear + 2000);
      //..
    }

@jamesderlin
Copy link
Contributor

This is now fixed on master by b1335afc2e72cce096c78aa3b600e8b8424631ab.

@bsneider
Copy link

bsneider commented Jun 7, 2020

@jamesderlin when will 17.0 show up on pub.dev?
Screen Shot 2020-06-07 at 1 31 39 PM

edit (this works in the interim)
dependency_overrides:
intl:
git:
url: git://github.com/dart-lang/intl
branch: master

@jamesderlin
Copy link
Contributor

0.17 probably will not be available on pub.dev until package:intl is updated for NNBD. We're treating this fix as a breaking change. Changes for NNBD are also expected to be breaking, and we'd like to avoid publishing multiple breaking versions in a short timespan. I don't know what the timeline is.

In the meantime, yes, you can have your pubspec.yaml file point to the GitHub repository.

@jamesderlin
Copy link
Contributor

A prerelease version of 0.17 is now available.

mosuem referenced this issue Mar 2, 2023
The `DateFormat` documentation claims that two-digit years will be
interpreted to be within 80 years before and 20 years after the
current date.  I don't know if that was ever true (that documentation
predates the creation of the GitHub repository).

While I'm tempted to just remove that false claim, there are multiple
GitHub issues about this not working, so some people want it.

* https://github.com/dart-lang/intl/issues/123
* https://github.com/dart-lang/intl/issues/275

PiperOrigin-RevId: 311079631
PiperOrigin-RevId: 311101503
PiperOrigin-RevId: 311412441
@mosuem mosuem transferred this issue from dart-archive/intl Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants