A Flutter package for getting date intervals (units of time between two DateTime values)
Creates an object that has interval information for years. months, days, hours, minutes, seconds between two dates. (Milli-seconds and Micro-seconds are zeroed out)
enum CalendarDirection { between, sinceEnd, untilEnd }
enum CalendarItem { years, months, days, hours, minutes, seconds }
CalendarItems({
@required Set<CalendarItem> setOfCalendarItems,
@required DateTime startEvent,
@required DateTime endEvent,
})
factory DateTimeIntervals.fromCurrentDateTime({
@required Set<CalendarItem> setOfCalendarItems,
@required DateTime eventDateTime,
})
final DateTime startTime = DateTime.now().subtract(Duration(days:1));
final DateTime endTime = DateTime.now();
final calendarItems = CalendarItems(setOfCalendarItems:[hour, minute, second],
startEvent: startTime,
endEvent: endTime);
final int years = calendarItems.years; // null
final int days = calendarItems.days; // null
final int hours = calendarItems.hours; // 24
final int minutes = calendarItems.minutes; // 0
final int seconds = calendarItems.seconds; // 0
final CalendarDirection direction = calendarItems.direction; // CalendarDirection.between
final startEvent = DateTime(2020, 4, 11, 17, 33, 00);
final calculator = DateTimeIntervals.fromCurrentDateTime(setOfCalendarItems: {
CalendarItem.months,
CalendarItem.days,
CalendarItem.hours,
CalendarItem.minutes,
CalendarItem.seconds,
}, eventDateTime: startEvent);
final CalendarDirection direction = calendarItems.direction;
// CalendarDirection.sinceEnd (until test is run after 12-Dec-2020, then it will be 'untilEnd')
DateTimeIntervals as a class that hold properties that detail the interval.
NOTE: Any CalendarItem omitted will result in the next item having the cumulative value. Example: If the interval is 1hr and 1min but CalendarItem.hour IS NOT in the setOfCalendarItems, then the ‘minutes’ will be 61.
Because years and months are inexact (years because of leap year, months because the number of days between 15-Feb and 15-Mar can vary because of leap year but still be a month, and since some months have 30 or 31 days there is also variance in what a ‘month’ is.) requests that include ‘Year’ and/or ‘Month’ are inexact.
Any request for intervals that don’t include both year and month are more exact, because of the Year/Month variance described.
// Yields a comma seperated string of 'enum CalendarItem's
String calendarItemsAsString(Set<CalendarItem> items);
// string is text string of 'num CalendarItem's
Set<CalendarItem> calendarItemsFrom({String string});
/// string with the number of years/months/days formatted as 'yy/mm/dd (zero padded to a minimal width of 2)
/// any nulled values will display '--'
String launchDate({bool includeDirection});
/// string with number of hours/minutes/seconds formatted as 'hh:mm:ss (zero padded to a minimal width of 2)
/// any nulled values will display '--'
String launchTime({bool includeDirection});
Author: GitHubStuff
Source Code: https://github.com/GitHubStuff/date_time_intervals
#flutter #dart #mobile-apps