Skip to content

Commit

Permalink
Merge pull request #169 from jama5262/ISSUE-168_create_separate_files…
Browse files Browse the repository at this point in the history
…_for_some_implementations

[ISSUE168] Create separate files for main implementations
  • Loading branch information
jama5262 committed Dec 29, 2022
2 parents 4062477 + 32525b7 commit d608fd4
Show file tree
Hide file tree
Showing 19 changed files with 3,788 additions and 11 deletions.
75 changes: 75 additions & 0 deletions lib/src/default_display.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:intl/intl.dart';

class DefaultDisplay {
// todo add examples as docs comments here
// check of doc comments from here will be shown or is it from the jiffy file
String E(DateTime dateTime) => DateFormat.E().format(dateTime);

String EEEE(DateTime dateTime) => DateFormat.EEEE().format(dateTime);

String Md(DateTime dateTime) => DateFormat.Md().format(dateTime);

String MEd(DateTime dateTime) => DateFormat.MEd().format(dateTime);

String MMM(DateTime dateTime) => DateFormat.MMM().format(dateTime);

String MMMd(DateTime dateTime) => DateFormat.MMMd().format(dateTime);

String MMMEd(DateTime dateTime) => DateFormat.MMMEd().format(dateTime);

String MMMM(DateTime dateTime) => DateFormat.MMMM().format(dateTime);

String MMMMd(DateTime dateTime) => DateFormat.MMMMd().format(dateTime);

String MMMMEEEEd(DateTime dateTime) =>
DateFormat.MMMMEEEEd().format(dateTime);

String QQQ(DateTime dateTime) => DateFormat.QQQ().format(dateTime);

String QQQQ(DateTime dateTime) => DateFormat.QQQQ().format(dateTime);

String yM(DateTime dateTime) => DateFormat.yM().format(dateTime);

String yMd(DateTime dateTime) => DateFormat.yMd().format(dateTime);

String yMEd(DateTime dateTime) => DateFormat.yMEd().format(dateTime);

String yMMM(DateTime dateTime) => DateFormat.yMMM().format(dateTime);

String yMMMd(DateTime dateTime) => DateFormat.yMMMd().format(dateTime);

String yMMMdjm(DateTime dateTime) =>
DateFormat.yMMMd().add_jm().format(dateTime);

String yMMMEd(DateTime dateTime) => DateFormat.yMMMEd().format(dateTime);

String yMMMEdjm(DateTime dateTime) =>
DateFormat.yMMMEd().add_jm().format(dateTime);

String yMMMM(DateTime dateTime) => DateFormat.yMMMM().format(dateTime);

String yMMMMd(DateTime dateTime) => DateFormat.yMMMMd().format(dateTime);

String yMMMMdjm(DateTime dateTime) =>
DateFormat.yMMMMd().add_jm().format(dateTime);

String yMMMMEEEEd(DateTime dateTime) =>
DateFormat.yMMMMEEEEd().format(dateTime);

String yMMMMEEEEdjm(DateTime dateTime) =>
DateFormat.yMMMMEEEEd().add_jm().format(dateTime);

String yQQQ(DateTime dateTime) => DateFormat.yQQQ().format(dateTime);

String yQQQQ(DateTime dateTime) => DateFormat.yQQQQ().format(dateTime);

String Hm(DateTime dateTime) => DateFormat.Hm().format(dateTime);

String Hms(DateTime dateTime) => DateFormat.Hms().format(dateTime);

String j(DateTime dateTime) => DateFormat.j().format(dateTime);

String jm(DateTime dateTime) => DateFormat.jm().format(dateTime);

String jms(DateTime dateTime) => DateFormat.jms().format(dateTime);
}
49 changes: 49 additions & 0 deletions lib/src/display.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:intl/intl.dart';

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

class Display {
String formatToISO8601(DateTime dateTime) => dateTime.toIso8601String();

String format(DateTime dateTime, String pattern, String ordinal) {
final escapedPattern = _replaceEscapePattern(pattern);
final newPattern = _replaceOrdinalDatePattern(escapedPattern, ordinal);
try {
return DateFormat(newPattern).format(dateTime);
} on Exception catch (e) {
throw JiffyException(e.toString());
}
}

String from(DateTime firstDateTime, DateTime secondDateTime) {
return '';
}

num diff(DateTime firstDateTime, DateTime secondsDateTime, Units units,
bool asFloat) {
return 0;
}

String _replaceEscapePattern(String input) {
return input.replaceAll('[', '\'').replaceAll(']', '\'');
}

String _replaceOrdinalDatePattern(String input, String suffix) {
final regex = _matchesOrdinalDatePattern().allMatches(input);
var pattern = input;
regex.forEach((match) {
if (match.group(1) == 'do') {
pattern = input.replaceRange(
match.start, match.end, 'd${suffix.isNotEmpty ? "'$suffix'" : ''}');
}
});
return pattern;
}

// tod understand what this regex pattern does
Pattern _matchesOrdinalDatePattern() {
return RegExp(
'''(?<unquote>[^"'\\s]\\w*)|(?:["][^"]+?["])|(?:['][^']+?['])''');
}
}
13 changes: 12 additions & 1 deletion lib/src/enums/units.dart
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
enum Units { MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, YEAR }
// todo reverse so that the year starts
enum Units {
MICROSECOND,
MILLISECOND,
SECOND,
MINUTE,
HOUR,
DAY,
WEEK,
MONTH,
YEAR
}
75 changes: 75 additions & 0 deletions lib/src/getter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:intl/intl.dart';

import 'enums/startOfWeek.dart';
import 'query.dart';

class Getter {
int microsecond(DateTime dateTime) => dateTime.microsecond;

int microsecondsSinceEpoch(DateTime dateTime) {
return dateTime.microsecondsSinceEpoch;
}

int millisecond(DateTime dateTime) => dateTime.millisecond;

int millisecondsSinceEpoch(DateTime dateTime) {
return dateTime.millisecondsSinceEpoch;
}

int second(DateTime dateTime) => dateTime.second;

int minute(DateTime dateTime) => dateTime.minute;

int hour(DateTime dateTime) => dateTime.hour;

int date(DateTime dateTime) => dateTime.day;

int dayOfWeek(DateTime dateTime, StartOfWeek startOfWeek) {
var weekDays = [1, 2, 3, 4, 5, 6, 7, 1, 2];
var weekDayIndex = dateTime.weekday - 1;

switch (startOfWeek) {
case StartOfWeek.MONDAY:
break;
case StartOfWeek.SUNDAY:
weekDayIndex += 1;
break;
case StartOfWeek.SATURDAY:
weekDayIndex += 2;
break;
}

return weekDays[weekDayIndex];
}

int daysInMonth(DateTime dateTime) =>
_daysInMonth(dateTime.year, dateTime.month);

int weekOfYear(DateTime dateTime, StartOfWeek startOfWeek) {
return ((dayOfYear(dateTime) - dayOfWeek(dateTime, startOfWeek) + 10) / 7)
.floor();
}

int month(DateTime dateTime) => dateTime.month;

int quarterOfYear(DateTime dateTime) {
return int.parse(DateFormat('Q').format(dateTime));
}

// todo see if you can use a parse and formatter class
int dayOfYear(DateTime dateTime) {
return int.parse(DateFormat('D').format(dateTime));
}

int year(DateTime dateTime) => dateTime.year;

int _daysInMonth(int year, int month) {
var result = daysInMonthArray[month];
if (month == 2 && Query.isLeapYear(year)) result++;
return result;
}

static final List<int> daysInMonthArray = List.from(
[0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
growable: false);
}
Loading

0 comments on commit d608fd4

Please sign in to comment.