Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup jiffy date time #4

Merged
merged 4 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions example/jiffy_example.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:jiffy/jiffy.dart';

main() async {

print(Jiffy("Changed", "Jama").minute);

print(Jiffy("Oct", "yyyy").year);
}
4 changes: 4 additions & 0 deletions lib/src/exception/exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class JiffyException implements Exception {
final String cause;
JiffyException(this.cause);
}
97 changes: 55 additions & 42 deletions lib/src/jiffy.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import 'package:intl/intl.dart';
import 'package:jiffy/src/exception/exception.dart';

class Jiffy {
DateTime _dateTime = DateTime.now();

DateTime get dateTime => _dateTime;


Jiffy([String time = "", String pattern = ""]);
Jiffy([String time, String pattern]) {
if (time == null && pattern == null) {
_dateTime = DateTime.now();
} else if (time != null && pattern == null) {
throw JiffyException(
"JiffyException: When passing time, a pattern must also be passed, e.g. Jiffy('12, Oct', 'dd, MMM')")
.cause;
} else {
_dateTime = DateFormat(pattern).parse(time);
}
}

Jiffy.unit(num timestamp);

Expand All @@ -21,51 +31,54 @@ class Jiffy {
int get week => ((dayOfYear - _dateTime.weekday + 10) / 7).floor();
int get month => _dateTime.month;
int get quarter => int.parse(DateFormat("Q").format(_dateTime));
int get year =>_dateTime.year;

// MANIPULATE
String add() {}

String subtract() {}

void startOf() {}
int get year => _dateTime.year;

void endOf() {}
// PARSE
void parse() {}

String local() {}

String utc() {}
// MANIPULATE
// String add() {}
//
// String subtract() {}
//
// void startOf() {}
//
// void endOf() {}
//
// String local() {}
//
// String utc() {}

// DISPLAY
String format() {}

String fromNow() {}

String from(Jiffy jiffy) {}

int diff(Jiffy jiffy, [String unit]) {}

int valueOf() {}

int unix() {}
// String format() {}
//
// String fromNow() {}
//
// String from(Jiffy jiffy) {}
//
// int diff(Jiffy jiffy, [String unit]) {}
//
// int valueOf() {}
//
// int unix() {}

// QUERY
bool isBefore(Jiffy jiffy) {}

bool isAfter(Jiffy jiffy) {}

bool isSame(Jiffy jiffy) {}

bool isSameOrBefore(Jiffy jiffy) {}

bool isSameOrAfter(Jiffy jiffy) {}

bool _isLeapYear() {}

bool get isLeapYear => _isLeapYear();

bool isJiffy(var input) {}

bool isDateTime(var input) {}
// bool isBefore(Jiffy jiffy) {}
//
// bool isAfter(Jiffy jiffy) {}
//
// bool isSame(Jiffy jiffy) {}
//
// bool isSameOrBefore(Jiffy jiffy) {}
//
// bool isSameOrAfter(Jiffy jiffy) {}
//
// bool _isLeapYear() {}
//
// bool get isLeapYear => _isLeapYear();
//
// bool isJiffy(var input) {}
//
// bool isDateTime(var input) {}

}
45 changes: 45 additions & 0 deletions test/jiffy_datetime_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:jiffy/jiffy.dart';
import 'package:test/test.dart';

void main() {
group('Test get datetime instance', () {
test("Pass without time and pattern", () {
expect(Jiffy().year, DateTime.now().year);
expect(Jiffy().month, DateTime.now().month);
});
test("Pass time and pattern", () {
expect(Jiffy("2009", "yyyy").year, 2009);
expect(Jiffy("Oct, 2009", "MMM, yyyy").year, 2009);
});
test("Pass time and with empty string pattern", () {
expect(Jiffy("2009", "").year, 1970);
});
test("Pass with empty time and pattern string", () {
expect(Jiffy("", "").year, 1970);
});
test("Pass pattern and with empty string time", () {
try {
Jiffy("", "yyyy");
} catch (e) {
expect(e.toString(),
"FormatException: Trying to read yyyy from at position 0");
}
});
test("Pass time with wrong pattern", () {
try {
Jiffy("Oct", "yyyy");
} catch (e) {
expect(e.toString(),
"FormatException: Trying to read yyyy from Oct at position 0");
}
});
test("Pass time, with no pattern parameter", () {
try {
Jiffy("");
} catch (e) {
expect(e.toString(),
"JiffyException: When passing time, a pattern must also be passed, e.g. Jiffy('12, Oct', 'dd, MMM')");
}
});
});
}