Skip to content

Instantly share code, notes, and snippets.

@miketheman
Last active January 18, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miketheman/2a08161c5f889e018938316237c0e810 to your computer and use it in GitHub Desktop.
Save miketheman/2a08161c5f889e018938316237c0e810 to your computer and use it in GitHub Desktop.
round duration to closer second, for a countdown clock - https://dartpad.dartlang.org/2a08161c5f889e018938316237c0e810
void main() {
Duration duration = new Duration(minutes: 1, seconds: 59, milliseconds: 99);
print(secondRounder(duration));
}
// Rounds Duration to seconds based on milliseconds
// This is semi-equivalent to a non-existing `Duration.ceil(milliseconds)`
Duration secondRounder(Duration duration) {
int roundedDuration;
if (duration.inMilliseconds > (duration.inSeconds * 1000)) {
roundedDuration = duration.inSeconds + 1;
} else {
roundedDuration = duration.inSeconds;
}
return new Duration(seconds: roundedDuration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment