Initial commit: Krow Worker App
This commit is contained in:
51
lib/core/formatters.dart
Normal file
51
lib/core/formatters.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
final _money = NumberFormat.currency(locale: 'en_US', symbol: r'$');
|
||||
final _time = DateFormat('h:mm a');
|
||||
final _dayFull = DateFormat('EEEE d MMMM');
|
||||
final _dayShort = DateFormat('EEE, d MMM yyyy');
|
||||
final _monthYear = DateFormat('MMMM yyyy');
|
||||
|
||||
abstract final class Fmt {
|
||||
static String money(num v) => _money.format(v);
|
||||
|
||||
static String time(DateTime d) => _time.format(d);
|
||||
|
||||
static String window(DateTime start, DateTime end) =>
|
||||
'${_time.format(start)} - ${_time.format(end)}';
|
||||
|
||||
static String dayFull(DateTime d) => _dayFull.format(d);
|
||||
|
||||
static String dayShort(DateTime d) => _dayShort.format(d);
|
||||
|
||||
static String monthYear(DateTime d) => _monthYear.format(d);
|
||||
|
||||
/// `5h 48m`, the form used on every hours figure.
|
||||
static String duration(Duration d) {
|
||||
final h = d.inHours;
|
||||
final m = d.inMinutes.remainder(60);
|
||||
if (h == 0) return '${m}m';
|
||||
return '${h}h ${m.toString().padLeft(2, '0')}m';
|
||||
}
|
||||
|
||||
/// `05:48:12`, for the running clock-in timer.
|
||||
static String stopwatch(Duration d) {
|
||||
final h = d.inHours.toString().padLeft(2, '0');
|
||||
final m = d.inMinutes.remainder(60).toString().padLeft(2, '0');
|
||||
final s = d.inSeconds.remainder(60).toString().padLeft(2, '0');
|
||||
return '$h:$m:$s';
|
||||
}
|
||||
|
||||
/// A human countdown to a shift start: `in 41 min`, `in 3 hr`, `tomorrow`.
|
||||
static String untilStart(DateTime start, {DateTime? now}) {
|
||||
final from = now ?? DateTime.now();
|
||||
final diff = start.difference(from);
|
||||
if (diff.isNegative) return 'Started';
|
||||
if (diff.inMinutes < 60) return 'In ${diff.inMinutes} min';
|
||||
if (diff.inHours < 24) return 'In ${diff.inHours} hr';
|
||||
return 'In ${diff.inDays} d';
|
||||
}
|
||||
|
||||
static bool sameDay(DateTime a, DateTime b) =>
|
||||
a.year == b.year && a.month == b.month && a.day == b.day;
|
||||
}
|
||||
Reference in New Issue
Block a user