Skip to content

Commit

Permalink
Merge pull request #171 from jama5262/ISSUE-170_work_on_diff_implemen…
Browse files Browse the repository at this point in the history
…tation

[ISSUE-170] Move the diff implemetation to a new file
  • Loading branch information
jama5262 committed Dec 29, 2022
2 parents d608fd4 + 34c5b9f commit 04c0489
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 74 deletions.
102 changes: 99 additions & 3 deletions lib/src/display.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import 'package:intl/intl.dart';

import 'getter.dart';
import 'enums/units.dart';
import 'manipulator.dart';
import 'utils/exception.dart';

class Display {
final Getter getter;
final Manipulator manipulator;

Display(this.getter, this.manipulator);

String formatToISO8601(DateTime dateTime) => dateTime.toIso8601String();

String format(DateTime dateTime, String pattern, String ordinal) {
Expand All @@ -20,9 +27,48 @@ class Display {
return '';
}

num diff(DateTime firstDateTime, DateTime secondsDateTime, Units units,
num diff(DateTime firstDateTime, DateTime secondDateTime, Units unit,
bool asFloat) {
return 0;
final firstDateTimeMicrosecondsSinceEpoch =
getter.microsecondsSinceEpoch(firstDateTime);
final secondDateTimeMicrosecondsSinceEpoch =
getter.microsecondsSinceEpoch(secondDateTime);
final diffMicrosecondsSinceEpoch = firstDateTimeMicrosecondsSinceEpoch -
secondDateTimeMicrosecondsSinceEpoch;

var diff;

switch (unit) {
case Units.MICROSECOND:
diff = diffMicrosecondsSinceEpoch;
break;
case Units.MILLISECOND:
diff = diffMicrosecondsSinceEpoch / Duration.microsecondsPerMillisecond;
break;
case Units.SECOND:
diff = diffMicrosecondsSinceEpoch / Duration.microsecondsPerSecond;
break;
case Units.MINUTE:
diff = diffMicrosecondsSinceEpoch / Duration.microsecondsPerMinute;
break;
case Units.HOUR:
diff = diffMicrosecondsSinceEpoch / Duration.microsecondsPerHour;
break;
case Units.DAY:
diff = diffMicrosecondsSinceEpoch / Duration.microsecondsPerDay;
break;
case Units.WEEK:
diff = (diffMicrosecondsSinceEpoch / Duration.microsecondsPerDay) / 7;
break;
case Units.MONTH:
diff = _monthDiff(firstDateTime, secondDateTime);
break;
case Units.YEAR:
diff = _monthDiff(firstDateTime, secondDateTime) / 12;
break;
}

return asFloat ? _absFloor(diff) : diff;
}

String _replaceEscapePattern(String input) {
Expand All @@ -41,9 +87,59 @@ class Display {
return pattern;
}

// tod understand what this regex pattern does
// todo understand what this regex pattern does
Pattern _matchesOrdinalDatePattern() {
return RegExp(
'''(?<unquote>[^"'\\s]\\w*)|(?:["][^"]+?["])|(?:['][^']+?['])''');
}

num _monthDiff(DateTime firstDateTime, DateTime secondDateTime) {
if (getter.date(firstDateTime) < getter.date(secondDateTime)) {
return -(_monthDiff(secondDateTime, firstDateTime));
}

final monthDiff =
((getter.year(secondDateTime) - getter.year(firstDateTime)) * 12) +
(getter.month(secondDateTime) - getter.month(firstDateTime));

final thirdDateTime = _addMonths(firstDateTime, monthDiff);
final thirdDateTimeMicrosecondsSinceEpoch =
getMicrosecondsSinceEpoch(thirdDateTime);

final diffMicrosecondsSinceEpoch =
getMicrosecondsSinceEpoch(secondDateTime) -
thirdDateTimeMicrosecondsSinceEpoch;

var offset;

if (diffMicrosecondsSinceEpoch < 0) {
final fifthDateTime = _addMonths(firstDateTime, monthDiff - 1);
offset = diffMicrosecondsSinceEpoch /
(thirdDateTimeMicrosecondsSinceEpoch -
getMicrosecondsSinceEpoch(fifthDateTime));
} else {
final fifthDateTime = _addMonths(firstDateTime, monthDiff + 1);
offset = diffMicrosecondsSinceEpoch /
(getMicrosecondsSinceEpoch(fifthDateTime) -
thirdDateTimeMicrosecondsSinceEpoch);
}

return -(monthDiff + offset);
}

int _absFloor(num number) {
if (number < 0) {
return number.ceil();
} else {
return number.floor();
}
}

DateTime _addMonths(DateTime dateTime, int months) {
return manipulator.add(dateTime, 0, 0, 0, 0, 0, 0, 0, months, 0);
}

int getMicrosecondsSinceEpoch(DateTime dateTime) {
return getter.microsecondsSinceEpoch(dateTime);
}
}
2 changes: 1 addition & 1 deletion lib/src/getter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Getter {
return int.parse(DateFormat('Q').format(dateTime));
}

// todo see if you can use a parse and formatter class
// todo see if you can use a formatter class
int dayOfYear(DateTime dateTime) {
return int.parse(DateFormat('D').format(dateTime));
}
Expand Down
156 changes: 95 additions & 61 deletions lib/src/manipulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,61 @@ class Manipulator {
switch (unit) {
case Units.MICROSECOND:
newDateTime = DateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
dateTime.millisecond,
dateTime.microsecond);
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
getter.millisecond(dateTime),
getter.microsecond(dateTime));
break;
case Units.MILLISECOND:
newDateTime = DateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
dateTime.millisecond);
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
getter.millisecond(dateTime));
break;
case Units.SECOND:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
dateTime.hour, dateTime.minute, dateTime.second);
newDateTime = DateTime(
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime));
break;
case Units.MINUTE:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
dateTime.hour, dateTime.minute);
newDateTime = DateTime(
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime));
break;
case Units.HOUR:
newDateTime = DateTime(
dateTime.year, dateTime.month, dateTime.day, dateTime.hour);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime),
getter.date(dateTime), getter.hour(dateTime));
break;
case Units.DAY:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime),
getter.date(dateTime));
break;
case Units.WEEK:
var newDate = subtractDuration(dateTime,
Duration(days: getter.dayOfWeek(dateTime, startOfWeek) - 1));
newDateTime = DateTime(newDate.year, newDate.month, newDate.day);
newDateTime = DateTime(
getter.year(newDate), getter.month(newDate), getter.date(newDate));
break;
case Units.MONTH:
newDateTime = DateTime(dateTime.year, dateTime.month);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime));
break;
case Units.YEAR:
newDateTime = DateTime(dateTime.year);
newDateTime = DateTime(getter.year(dateTime));
break;
}
return newDateTime;
Expand All @@ -125,61 +136,77 @@ class Manipulator {
switch (unit) {
case Units.MICROSECOND:
newDateTime = DateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
dateTime.millisecond,
dateTime.microsecond);
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
getter.millisecond(dateTime),
getter.microsecond(dateTime));
break;
case Units.MILLISECOND:
newDateTime = DateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
dateTime.millisecond,
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
getter.millisecond(dateTime),
999);
break;
case Units.SECOND:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
dateTime.hour, dateTime.minute, dateTime.second, 999, 999);
newDateTime = DateTime(
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
999,
999);
break;
case Units.MINUTE:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
dateTime.hour, dateTime.minute, 59, 999, 999);
newDateTime = DateTime(
getter.year(dateTime),
getter.month(dateTime),
getter.date(dateTime),
getter.hour(dateTime),
getter.minute(dateTime),
59,
999,
999);
break;
case Units.HOUR:
newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day,
dateTime.hour, 59, 59, 999, 999);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime),
getter.date(dateTime), getter.hour(dateTime), 59, 59, 999, 999);
break;
case Units.DAY:
newDateTime = DateTime(
dateTime.year, dateTime.month, dateTime.day, 23, 59, 59, 999, 999);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime),
getter.date(dateTime), 23, 59, 59, 999, 999);
break;
case Units.WEEK:
var newDate = addDuration(
dateTime,
Duration(
days: DateTime.daysPerWeek -
getter.dayOfWeek(dateTime, startOfWeek)));
newDateTime = DateTime(
newDate.year, newDate.month, newDate.day, 23, 59, 59, 999, 999);
newDateTime = DateTime(getter.year(newDate), getter.month(newDate),
getter.date(newDate), 23, 59, 59, 999, 999);
break;
case Units.MONTH:
var endMonthDate = Getter.daysInMonthArray[dateTime.month];
if (Query.isLeapYear(dateTime.year) && dateTime.month == 2) {
var endMonthDate = Getter.daysInMonthArray[getter.month(dateTime)];
if (Query.isLeapYear(getter.year(dateTime)) &&
getter.month(dateTime) == 2) {
endMonthDate = 29;
}
newDateTime = DateTime(
dateTime.year, dateTime.month, endMonthDate, 23, 59, 59, 999, 999);
newDateTime = DateTime(getter.year(dateTime), getter.month(dateTime),
endMonthDate, 23, 59, 59, 999, 999);
break;
case Units.YEAR:
newDateTime = DateTime(dateTime.year, 12, 31, 23, 59, 59, 999, 999);
newDateTime =
DateTime(getter.year(dateTime), 12, 31, 23, 59, 59, 999, 999);
break;
}
return newDateTime;
Expand All @@ -191,15 +218,22 @@ class Manipulator {

DateTime _addMonths(DateTime dateTime, int months) {
final modMonths = months % 12;
var newYear = dateTime.year + ((months - modMonths) ~/ 12);
var newMonth = dateTime.month + modMonths;
var newYear = getter.year(dateTime) + ((months - modMonths) ~/ 12);
var newMonth = getter.month(dateTime) + modMonths;
if (newMonth > 12) {
newYear++;
newMonth -= 12;
}
final newDay =
min(dateTime.day, getter.daysInMonth(DateTime(newYear, newMonth)));
return DateTime(newYear, newMonth, newDay, dateTime.hour, dateTime.minute,
dateTime.second, dateTime.millisecond, dateTime.microsecond);
final newDay = min(
getter.date(dateTime), getter.daysInMonth(DateTime(newYear, newMonth)));
return DateTime(
newYear,
newMonth,
newDay,
getter.hour(dateTime),
getter.minute(dateTime),
getter.second(dateTime),
getter.millisecond(dateTime),
getter.microsecond(dateTime));
}
}
21 changes: 13 additions & 8 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import 'package:intl/intl.dart';

import 'enums/units.dart';
import 'getter.dart';
import 'utils/exception.dart';

class Parser {
final Getter getter;

Parser(this.getter);

DateTime fromString(String input, String? pattern, List<String> ordinals) {
if (pattern != null) {
if (pattern.trim().isEmpty) {
Expand Down Expand Up @@ -49,14 +54,14 @@ class Parser {
throw JiffyException('The provided datetime map cannot be empty');
}
return DateTime(
input[Units.YEAR] ?? DateTime.now().year,
input[Units.MONTH] ?? DateTime.now().month,
input[Units.DAY] ?? DateTime.now().day,
input[Units.HOUR] ?? DateTime.now().hour,
input[Units.MINUTE] ?? DateTime.now().minute,
input[Units.SECOND] ?? DateTime.now().second,
input[Units.MILLISECOND] ?? DateTime.now().millisecond,
input[Units.MICROSECOND] ?? DateTime.now().microsecond);
input[Units.YEAR] ?? getter.year(DateTime.now()),
input[Units.MONTH] ?? getter.month(DateTime.now()),
input[Units.DAY] ?? getter.date(DateTime.now()),
input[Units.HOUR] ?? getter.hour(DateTime.now()),
input[Units.MINUTE] ?? getter.minute(DateTime.now()),
input[Units.SECOND] ?? getter.second(DateTime.now()),
input[Units.MILLISECOND] ?? getter.millisecond(DateTime.now()),
input[Units.MICROSECOND] ?? getter.microsecond(DateTime.now()));
}

DateTime parseString(String input, String pattern) {
Expand Down

0 comments on commit 04c0489

Please sign in to comment.