From 2fc4957fce2f30631c353abff564d7453c769746 Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Sat, 12 Oct 2019 19:01:17 +0300 Subject: [PATCH 1/4] [Started] Started this branch --- lib/src/jiffy.dart | 81 +++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/lib/src/jiffy.dart b/lib/src/jiffy.dart index 34a0b09..6cb37a9 100644 --- a/lib/src/jiffy.dart +++ b/lib/src/jiffy.dart @@ -5,7 +5,6 @@ class Jiffy { DateTime get dateTime => _dateTime; - Jiffy([String time = "", String pattern = ""]); Jiffy.unit(num timestamp); @@ -21,51 +20,51 @@ 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; + int get year => _dateTime.year; // MANIPULATE - String add() {} - - String subtract() {} - - void startOf() {} - - void endOf() {} - - String local() {} - - String utc() {} +// 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) {} } From 4b2f67fbf521ff71935ccd1c3b27a7810e280d33 Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Sun, 13 Oct 2019 11:58:36 +0300 Subject: [PATCH 2/4] Setup the Jiffy datetime --- example/jiffy_example.dart | 3 ++- lib/src/exception/exception.dart | 4 ++++ lib/src/jiffy.dart | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 lib/src/exception/exception.dart diff --git a/example/jiffy_example.dart b/example/jiffy_example.dart index 1988bfb..64e7344 100644 --- a/example/jiffy_example.dart +++ b/example/jiffy_example.dart @@ -1,7 +1,8 @@ import 'package:jiffy/jiffy.dart'; +import 'package:jiffy/src/exception/exception.dart'; main() async { - print(Jiffy("Changed", "Jama").minute); + print(Jiffy("", "yyyy").year); } diff --git a/lib/src/exception/exception.dart b/lib/src/exception/exception.dart new file mode 100644 index 0000000..2292647 --- /dev/null +++ b/lib/src/exception/exception.dart @@ -0,0 +1,4 @@ +class JiffyException implements Exception { + final String cause; + JiffyException(this.cause); +} \ No newline at end of file diff --git a/lib/src/jiffy.dart b/lib/src/jiffy.dart index 6cb37a9..787aa5c 100644 --- a/lib/src/jiffy.dart +++ b/lib/src/jiffy.dart @@ -1,11 +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); @@ -22,6 +33,9 @@ class Jiffy { int get quarter => int.parse(DateFormat("Q").format(_dateTime)); int get year => _dateTime.year; +// PARSE + void parse() {} + // MANIPULATE // String add() {} // From ca033059d33c40664dcc6571fe26baed1b169cdb Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Sun, 13 Oct 2019 12:17:53 +0300 Subject: [PATCH 3/4] [Finished] Set the Jiffy datetime instance tests --- example/jiffy_example.dart | 4 +--- test/jiffy_datetime_test.dart | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/jiffy_datetime_test.dart diff --git a/example/jiffy_example.dart b/example/jiffy_example.dart index 64e7344..688c433 100644 --- a/example/jiffy_example.dart +++ b/example/jiffy_example.dart @@ -2,7 +2,5 @@ import 'package:jiffy/jiffy.dart'; import 'package:jiffy/src/exception/exception.dart'; main() async { - - print(Jiffy("", "yyyy").year); - + print(Jiffy("Oct", "yyyy").year); } 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')"); + } + }); + }); +} From cce3af1846fb54a935cdbad80b2f4fdbfca36de4 Mon Sep 17 00:00:00 2001 From: Jama Mohamed Date: Sun, 13 Oct 2019 12:31:21 +0300 Subject: [PATCH 4/4] [Finished] Minor changes --- example/jiffy_example.dart | 1 - lib/src/exception/exception.dart | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/example/jiffy_example.dart b/example/jiffy_example.dart index 688c433..db9b44a 100644 --- a/example/jiffy_example.dart +++ b/example/jiffy_example.dart @@ -1,5 +1,4 @@ import 'package:jiffy/jiffy.dart'; -import 'package:jiffy/src/exception/exception.dart'; main() async { print(Jiffy("Oct", "yyyy").year); diff --git a/lib/src/exception/exception.dart b/lib/src/exception/exception.dart index 2292647..57a6cb6 100644 --- a/lib/src/exception/exception.dart +++ b/lib/src/exception/exception.dart @@ -1,4 +1,4 @@ class JiffyException implements Exception { final String cause; JiffyException(this.cause); -} \ No newline at end of file +}