diff --git a/README.md b/README.md index b6bcd5c..ac0aca6 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ [![Pub Version](https://img.shields.io/badge/pub-v2.1.2-blue)](https://pub.dev/packages/jiffy) [![Platform](https://img.shields.io/badge/platform-flutter%7Cweb%7Cdart%20vm-orange)](https://github.com/jama5262/jiffy) -Jiffy is a dart date time package inspired by [momentjs](https://momentjs.com/) for parsing, manipulating, querying and formatting dates +Jiffy is a Flutter (Android, IOS and Web) date time package inspired by [momentjs](https://momentjs.com/) for parsing, manipulating, querying and formatting dates -#### [Full Documentation](https://github.com/jama5262/jiffy/tree/v2.1.2/doc) | [Installation](https://pub.dev/packages/jiffy#-installing-tab-) | [ChangeLog](https://pub.dev/packages/jiffy#-changelog-tab-) | [Examples](https://pub.dev/packages/jiffy#-example-tab-) +### [Full Documentation](https://github.com/jama5262/jiffy/tree/v2.1.2/doc) | [Installation](https://pub.dev/packages/jiffy#-installing-tab-) | [ChangeLog](https://pub.dev/packages/jiffy#-changelog-tab-) | [Examples](https://pub.dev/packages/jiffy#-example-tab-) # Usage @@ -74,8 +74,9 @@ var jiffy4 = Jiffy() jiffy4.format("dd/MM/yyy"); // 20/10/2019 -// Months and year are added in respect to how many days there are in a months and if is a year is a leap year -Jiffy("2010-1-31", "yyyy-MM-dd"); // This is January 31 +// Months and year are added in respect to how many +// days there are in a months and if is a year is a leap year +Jiffy("2010/1/31", "yyyy-MM-dd"); // This is January 31 Jiffy([2010, 1, 31]).add(months: 1); // This is February 28 ``` diff --git a/doc/README.md b/doc/README.md index 7675dee..3b6e279 100644 --- a/doc/README.md +++ b/doc/README.md @@ -6,7 +6,7 @@ [![Pub Version](https://img.shields.io/badge/pub-v2.1.2-blue)](https://pub.dev/packages/jiffy) [![Platform](https://img.shields.io/badge/platform-flutter%7Cweb%7Cdart%20vm-orange)](https://github.com/jama5262/jiffy) -Jiffy is a dart date time package inspired by [momentjs](https://momentjs.com/) for parsing, manipulating, querying and formatting dates +Jiffy is a Flutter (Android, IOS and Web) date time package inspired by [momentjs](https://momentjs.com/) for parsing, manipulating, querying and formatting dates # Table of content - [Before Use](#before-use) @@ -67,10 +67,9 @@ Jiffy().utc(); // Returns a DateTime instance _**But when doing a method chaining, it is recommended to use a variable.**_ The variable will then hold a Jiffy instance. Example ```dart var jiffy = Jiffy() - ..utc() ..add(days: 1) - ..add(hours: 3) ..subtract(minutes: 30); // Returns a Jiffy instance + ..utc() ``` Now `jiffy` variable returns a Jiffy instance. To get the date time, you can call it with the following methods ```dart @@ -93,10 +92,15 @@ Jiffy().format(); // Return a ISO 8601 date time format ### String Creating a Jiffy from a string. See below ```dart -Jiffy("1995-12-25"); +Jiffy("1995-12-25"); // A calendar date part +Jiffy("1995/12/25"); // A calendar date part separated by slash "/" +Jiffy("19951225"); // Basic (short) full date +Jiffy("1995-12-25 12:00:00.000"); // An hour, minute, second, and millisecond time part +Jiffy("1995-12-25T12:00:00.000"); ISO dart format +Jiffy("1995-12-25T12:00:00.000Z"); ISO dart format (UTC) ``` -**_Note: For now, Jiffy supports only `yyyy-MM-dd` string formats. Passing string like `dd-MM-yyyy` will result in an exception. If you do need to pass this format, `dd-MM-yyyy` or any other, should also pass a pattern of that string, Also know as [String Formating](#string-formatting). See below_** +**_Note: For now, Jiffy supports only the above string formats. Passing string like `dd-MM-yyyy` will result in an exception. If you do need to pass this format, `dd-MM-yyyy` or any other, should also pass a pattern of that string, Also know as [String Formating](#string-formatting). See below_** ```dart Jiffy("25-12-1995", "dd-MM-yyyy"); Jiffy("12-1995", "MM-yyyy"); @@ -116,7 +120,7 @@ Jiffy("0ct 19th", "MMM do"); Jiffy("19th October 2019", "do MMMM yyyy"); ``` -Jiffy runs on top of the [Intl DateFormat](https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html) package, you can find all the date time patterns used by Jiffy [here](https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html) +**_Note: Jiffy runs on top of the [Intl DateFormat](https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html) package, you can find all the date time patterns used by Jiffy [here](https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html)_** This is also same for Jiffy default formats. See below ```dart diff --git a/lib/src/jiffy.dart b/lib/src/jiffy.dart index 539edbe..1bf61d9 100644 --- a/lib/src/jiffy.dart +++ b/lib/src/jiffy.dart @@ -2,7 +2,7 @@ import 'dart:math'; import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; -import 'package:jiffy/src/exception/exception.dart'; +import 'package:jiffy/src/utils/exception.dart'; import 'package:jiffy/src/relative_time/relative_time.dart' as relative; import 'package:jiffy/src/utils/normalize_units.dart'; import 'package:jiffy/src/utils/ordinalLocale.dart'; @@ -69,11 +69,21 @@ class Jiffy { input.length > 6 ? input[6] : 0); } } else if (input is String) { - if (matchStringDateTime(input)) { - dateTime = DateFormat("yyyy-MM-dd").parse(input); - } else if (pattern != null) { + if (pattern != null) { dateTime = DateFormat(replacePatternInput(pattern)) .parse(replaceParseInput(input)); + } else if (matchHyphenStringDateTime(input)) { + dateTime = DateFormat("yyyy-MM-dd").parse(input); + } else if (matchDartStringDateTime(input) || + matchISOStringDateTime(input)) { + dateTime = DateTime.parse(input).toLocal(); + } else if (matchSlashStringDateTime(input)) { + dateTime = DateFormat("yyyy/MM/dd").parse(input); + } else if (matchBasicStringDateTime().hasMatch(input)) { + dateTime = DateFormat("yyyy/MM/dd") + .parse(input.replaceAllMapped(matchBasicStringDateTime(), (match) { + return "${match.group(1)}/${match.group(2)}/${match.group(3)}"; + })); } else if (pattern == null) { throw JiffyException( "Date time not recognized, a pattern must be passed, e.g. Jiffy('12, Oct', 'dd, MMM')") diff --git a/lib/src/exception/exception.dart b/lib/src/utils/exception.dart similarity index 100% rename from lib/src/exception/exception.dart rename to lib/src/utils/exception.dart diff --git a/lib/src/utils/normalize_units.dart b/lib/src/utils/normalize_units.dart index 3cb2f86..6776984 100644 --- a/lib/src/utils/normalize_units.dart +++ b/lib/src/utils/normalize_units.dart @@ -1,4 +1,4 @@ -import 'package:jiffy/src/exception/exception.dart'; +import 'package:jiffy/src/utils/exception.dart'; String normalizeUnits(String unit) { String lowerCaseUnit = unit.toLowerCase(); diff --git a/lib/src/utils/regex.dart b/lib/src/utils/regex.dart index d4d0621..0dffa79 100644 --- a/lib/src/utils/regex.dart +++ b/lib/src/utils/regex.dart @@ -1,7 +1,27 @@ -bool matchStringDateTime(String input) { +bool matchDartStringDateTime(String input) { + return RegExp( + r"\d{4}-\d{1,2}-\d{1,2} \d{1,2}(:\d{1,2})?(:\d{1,2})?(.\d+)?(Z?)") + .hasMatch(input); +} + +bool matchISOStringDateTime(String input) { + return RegExp( + r"\d{4}-\d{1,2}-\d{1,2}T\d{1,2}(:\d{1,2})?(:\d{1,2})?(.\d+)?(Z?)") + .hasMatch(input); +} + +RegExp matchBasicStringDateTime() { + return RegExp(r"(\d{4})(\d{1,2})(\d{1,2})$"); +} + +bool matchHyphenStringDateTime(String input) { return RegExp(r"\d{4}-\d{1,2}-\d{1,2}$").hasMatch(input); } +bool matchSlashStringDateTime(String input) { + return RegExp(r"\d{4}\/\d{1,2}\/\d{1,2}$").hasMatch(input); +} + Pattern matchOrdinalDates() { return RegExp(r"(?<=[0-9])(?:st|nd|rd|th)"); } diff --git a/test/jiffy_display_test.dart b/test/jiffy_display_test.dart index 86a46a3..4a7971d 100644 --- a/test/jiffy_display_test.dart +++ b/test/jiffy_display_test.dart @@ -4,14 +4,12 @@ import 'package:test/test.dart'; void main() { group('Test format and default format datetime methods', () { test("Test format method", () { - expect(Jiffy("2019, 10, 16", "yyyy, MM, dd").format("MMMM dd, yyyy"), - "October 16, 2019"); - expect(Jiffy("2019, 10, 16", "yyyy, MM, dd").format("'Today is' dd MMM"), + expect(Jiffy("20191016").format("MMMM dd, yyyy"), "October 16, 2019"); + expect(Jiffy("2019-10-16 12:00").format("'Today is' dd MMM"), "Today is 16 Oct"); - expect(Jiffy("2019, 10, 16", "yyyy, MM, dd").format("MMMMEEEEd"), + expect(Jiffy("2019-10-16T00:00:00.000").format("MMMMEEEEd"), "Wednesday, October 16"); - expect(Jiffy("2019, 10, 16", "yyyy, MM, dd").format(), - "2019-10-16T00:00:00.000"); + expect(Jiffy("2019/10/16").format(), "2019-10-16T00:00:00.000"); }); test("Test from method", () { expect(Jiffy([2019, 10, 16]).E, "Wed"); diff --git a/test/jiffy_manipulation_test.dart b/test/jiffy_manipulation_test.dart index 6450d57..96a921b 100644 --- a/test/jiffy_manipulation_test.dart +++ b/test/jiffy_manipulation_test.dart @@ -74,7 +74,7 @@ void main() { group('Test Jiffy subtracting datetime', () { test("Subtracting milliseconds", () { expect( - Jiffy("2019-10-13 23:00:00:000", "yyyy-MM-dd hh:mm:ss") + Jiffy("2019-10-13 23:00:00") .subtract(duration: Duration(milliseconds: 1)) .toString(), "2019-10-13 22:59:59.999");