Skip to content

Instantly share code, notes, and snippets.

@HeySreelal
Created March 8, 2024 14:21
Show Gist options
  • Save HeySreelal/b738740b7e2ec24b50cfee3abadf06fc to your computer and use it in GitHub Desktop.
Save HeySreelal/b738740b7e2ec24b50cfee3abadf06fc to your computer and use it in GitHub Desktop.
Yeah, just run it and see how large a billion actually is!
void main() {
final million = 1e6;
final billion = 1e9;
final mDur = Duration(seconds: million.toInt());
final bDur = Duration(seconds: billion.toInt());
print('1 million seconds is\n${durToString(mDur)}');
print("");
print('1 billion seconds is\n${durToString(bDur)}');
}
String durToString(Duration d) {
// A Years, B Months, C Days, D Hours, E Minutes, F Seconds
final years = d.inDays ~/ 365;
final months = (d.inDays % 365) ~/ 30;
final days = (d.inDays % 365) % 30;
final hours = d.inHours % 24;
final minutes = d.inMinutes % 60;
final seconds = d.inSeconds % 60;
final yStr = years > 0 ? '$years years' : '';
final mStr = months > 0 ? '$months months' : '';
final dStr = days > 0 ? '$days days' : '';
final hStr = hours > 0 ? '$hours hours' : '';
final minStr = minutes > 0 ? '$minutes minutes' : '';
final secStr = seconds > 0 ? '$seconds seconds' : '';
final parts = [yStr, mStr, dStr, hStr, minStr, secStr];
final nonEmptyParts = parts.where((p) => p.isNotEmpty).toList();
if (nonEmptyParts.length == 1) {
return nonEmptyParts.first;
}
final lastPart = nonEmptyParts.removeLast();
return '${nonEmptyParts.join(', ')} and $lastPart';
}
@HeySreelal
Copy link
Author

Run it, and then you'll see:

1 million seconds is
11 days, 13 hours, 46 minutes and 40 seconds

1 billion seconds is
31 years, 8 months, 19 days, 1 hours, 46 minutes and 40 seconds

That's it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment