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

Add Duration.parse #16059

Open
DartBot opened this issue Jan 13, 2014 · 12 comments
Open

Add Duration.parse #16059

DartBot opened this issue Jan 13, 2014 · 12 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 library-core type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jan 13, 2014

This issue was originally filed by @seaneagan


Duration.parse based on ISO-8601 (like DateTime.parse) would be useful for things like command line options, configuration files, url parameters.

Java: http://download.java.net/jdk8/docs/api/java/time/Duration.html

Go has it's own formatting, which I like better, but is not standardized AFAIK: http://golang.org/pkg/time/#ParseDuration

@DartBot
Copy link
Author

DartBot commented Jan 13, 2014

This comment was originally written by @seaneagan


Would probably want to change Duration.toString() to match ISO-8601 or whatever format is chosen for Duration.parse.

@iposva-google
Copy link
Contributor

Added Area-Library, Library-Core, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Jan 14, 2014

Removed Type-Defect label.
Added Type-Enhancement label.

@floitschG
Copy link
Contributor

Issue #19891 has been merged into this issue.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core labels Jul 9, 2014
@jtmcdole
Copy link
Contributor

Yep, this would have been nice to have today.

@Jardo-51
Copy link

Jardo-51 commented Mar 1, 2019

I also needed this today.

@lohnn
Copy link

lohnn commented Sep 24, 2019

Worked on this and added support for it in https://dart-review.googlesource.com/c/sdk/+/118566.

@SimonIT
Copy link

SimonIT commented May 8, 2020

I would like to have a toIso8601String method in addition to parsing.

@Lootwig
Copy link
Contributor

Lootwig commented Apr 11, 2021

@lohnn why was this never merged? Or, maybe better: what can I do to get it merged?

@lohnn
Copy link

lohnn commented Apr 11, 2021

I guess my PR just didn't get any attention. I can take a shot at updating it and then report back here. Then we can see what we need to do to get it merged?

@Lootwig
Copy link
Contributor

Lootwig commented Apr 11, 2021

@lohnn Would definitely be useful!
Two remarks on the code:

  1. for the time being I've copied over the code into my project. I've modified the _parseTime() method like this:
static int _parseTime(String duration, String timeUnit) {
  final timeMatch = RegExp('(\\d+)$timeUnit').firstMatch(duration);
  if (timeMatch == null) {
    return 0;
  }
  return int.parse(timeMatch.group(1));
}
  1. Since you're working in the actual Duration class already, maybe making the static method a factory constructor might be an option? I actually don't know if this is considered more "elegant", just wanted to point out that that's something a mere extension currently can't do.

@Reprevise
Copy link

I am definitely in favor of this, though I'm not sure what format it should parse. For my use case, Duration should be able to parse back the output of it's .toString() method.

var d = const Duration(days: 1, hours: 1, minutes: 33, microseconds: 500);
print(d.toString()); // 25:33:00.000500

I can see the argument for using the ISO 8601 standard though, perhaps both should be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests