gameRewardsEndsIn function

String? gameRewardsEndsIn(
  1. DateTime? expiresAt, {
  2. DateTime? now,
})

"Ends in 3d" / "Ends in 5h" / "Ends in 12m": the largest whole unit, at least a minute; null when the expiry is unknown or already past.

Implementation

String? gameRewardsEndsIn(DateTime? expiresAt, {DateTime? now}) {
  if (expiresAt == null) return null;
  final seconds = expiresAt.difference(now ?? DateTime.now()).inSeconds;
  if (seconds <= 0) return null;
  if (seconds >= 86400) return 'Ends in ${seconds ~/ 86400}d';
  if (seconds >= 3600) return 'Ends in ${seconds ~/ 3600}h';
  final minutes = seconds ~/ 60;
  return 'Ends in ${minutes < 1 ? 1 : minutes}m';
}