diff --git a/example/jiffy_example.dart b/example/jiffy_example.dart index 1988bfb..db9b44a 100644 --- a/example/jiffy_example.dart +++ b/example/jiffy_example.dart @@ -1,7 +1,5 @@ import 'package:jiffy/jiffy.dart'; main() async { - - print(Jiffy("Changed", "Jama").minute); - + print(Jiffy("Oct", "yyyy").year); } diff --git a/lib/src/exception/exception.dart b/lib/src/exception/exception.dart new file mode 100644 index 0000000..57a6cb6 --- /dev/null +++ b/lib/src/exception/exception.dart @@ -0,0 +1,4 @@ +class JiffyException implements Exception { + final String cause; + JiffyException(this.cause); +} diff --git a/lib/src/jiffy.dart b/lib/src/jiffy.dart index 34a0b09..787aa5c 100644 --- a/lib/src/jiffy.dart +++ b/lib/src/jiffy.dart @@ -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); @@ -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) {} } diff --git a/test/jiffy_datetime_test.dart b/test/jiffy_datetime_test.dart new file mode 100644 index 0000000..bfdb15d --- /dev/null +++ b/test/jiffy_datetime_test.dart @@ -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')"); + } + }); + }); +}