Skip to content

Commit

Permalink
Merge pull request #40 from jama5262/update-string-parsing
Browse files Browse the repository at this point in the history
Update string parsing
  • Loading branch information
jama5262 authored Jan 8, 2020
2 parents 72febce + 18691f6 commit dfe298a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
```

Expand Down
16 changes: 10 additions & 6 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions lib/src/jiffy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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')")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/src/utils/normalize_units.dart
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
22 changes: 21 additions & 1 deletion lib/src/utils/regex.dart
Original file line number Diff line number Diff line change
@@ -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)");
}
Expand Down
10 changes: 4 additions & 6 deletions test/jiffy_display_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion test/jiffy_manipulation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit dfe298a

Please sign in to comment.