From f855a467270f71786a2396cd52cf8925aa78a985 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Mon, 24 Jul 2023 23:18:11 +0900 Subject: [PATCH 1/9] =?UTF-8?q?=E7=8F=BE=E5=9C=A8=E6=99=82=E5=88=BB?= =?UTF-8?q?=E3=81=A8=E6=AF=94=E8=BC=83=E3=81=97=E3=81=A6=E3=80=81=E7=9B=B8?= =?UTF-8?q?=E5=AF=BE=E7=9A=84=E3=81=AA=E6=97=A5=E4=BB=98=E6=96=87=E5=AD=97?= =?UTF-8?q?=E5=88=97=E3=82=92=E8=BF=94=E3=81=99=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/src/date_time.dart | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index e1fa2e63..68f3eb38 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -6,4 +6,40 @@ extension DateTimeExtension on DateTime { /// 「yyyy年MM月dd日 HH時mm分」形式の文字列を返す。 String formatDateTime() => DateFormat('yyyy年MM月dd日 HH時mm分').format(this); + + /// 現在時刻と比較して、相対的な日付文字列を返す。 + String formatRelativeDate() { + final now = DateTime.now(); + if (_isToday(now)) { + return DateFormat('HH:mm').format(this); + } + if (_isYesterday(now)) { + return '昨日'; + } + final daysBeforeResult = _daysBefore(now); + if (daysBeforeResult != null) { + return '$daysBeforeResult日前'; + } + return formatDate(); + } + + bool _isToday(DateTime now) { + return year == now.year && month == now.month && day == now.day; + } + + bool _isYesterday(DateTime now) { + final yesterday = DateTime(now.year, now.month, now.day - 1); + return year == yesterday.year && + month == yesterday.month && + day == yesterday.day; + } + + /// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す + int? _daysBefore(DateTime now) { + final differece = now.difference(this).inDays; + if (differece >= 2 && differece <= 7) { + return differece; + } + return null; + } } From fb28c03310943bd65ffd4169d48b54fe49ebae84 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 12:00:40 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=9F?= =?UTF-8?q?=E5=90=84=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AE=E3=83=A6?= =?UTF-8?q?=E3=83=8B=E3=83=83=E3=83=88=E3=83=86=E3=82=B9=E3=83=88=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/date_time_test.dart | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/dart_flutter_common/test/date_time_test.dart b/packages/dart_flutter_common/test/date_time_test.dart index 888069b1..89e8304b 100644 --- a/packages/dart_flutter_common/test/date_time_test.dart +++ b/packages/dart_flutter_common/test/date_time_test.dart @@ -1,5 +1,6 @@ import 'package:dart_flutter_common/dart_flutter_common.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:intl/intl.dart'; void main() { group('test DateTimeExtension', () { @@ -10,11 +11,49 @@ void main() { expect('1998年05月02日', formattedDate); }); - test('test formatDateTime', () { + test('test formatDateTime', () { final date = DateTime(1998, 5, 2, 10, 7); final formattedDate = date.formatDateTime(); expect('1998年05月02日 10時07分', formattedDate); }); + + test('test formatRelativeDate for today', () { + final now = DateTime.now(); + final date = DateTime(now.year, now.month, now.day, 12, 15); + final formattedRelativeDate = date.formatRelativeDate(); + + expect(formattedRelativeDate, DateFormat('HH:mm').format(date)); + }); + + test('test formatRelativeDate for yesterday', () { + final now = DateTime.now(); + final date = DateTime( + now.year, + now.month, + now.day - 1, + ); + final formattedRelativeDate = date.formatRelativeDate(); + + expect(formattedRelativeDate, '昨日'); + }); + + for (var i = 2; i <= 7; i++) { + test('test formatRelativeDate for $i daysBefore', () { + final now = DateTime.now(); + final date = DateTime(now.year, now.month, now.day - i); + final formattedRelativeDate = date.formatRelativeDate(); + + expect(formattedRelativeDate, '$i日前'); + }); + } + + test('test formatRelativeDate for over a week ago', () { + final now = DateTime.now(); + final date = DateTime(now.year, now.month, now.day - 8); + final formattedRelativeDate = date.formatRelativeDate(); + + expect(formattedRelativeDate, DateFormat('yyyy年MM月dd日').format(date)); + }); }); } From 6d1c1cf1ef5ca21ee23ae8c0f5d03295ac0f5061 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 13:41:49 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E3=82=BF=E3=82=A4=E3=83=9D=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=EF=BC=88=E3=81=8A=E3=82=88=E3=81=B3vscode=E3=81=AE?= =?UTF-8?q?=E6=8B=A1=E5=BC=B5=E6=A9=9F=E8=83=BD=E8=BF=BD=E5=8A=A0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index 68f3eb38..843b7679 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -36,9 +36,9 @@ extension DateTimeExtension on DateTime { /// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す int? _daysBefore(DateTime now) { - final differece = now.difference(this).inDays; - if (differece >= 2 && differece <= 7) { - return differece; + final difference = now.difference(this).inDays; + if (difference >= 2 && difference <= 7) { + return difference; } return null; } From 682083311eb84f24428643b86a83d844bdbd838f Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 13:47:27 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=5FisToday()=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index 843b7679..e80f6e82 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -10,7 +10,7 @@ extension DateTimeExtension on DateTime { /// 現在時刻と比較して、相対的な日付文字列を返す。 String formatRelativeDate() { final now = DateTime.now(); - if (_isToday(now)) { + if (_isToday()) { return DateFormat('HH:mm').format(this); } if (_isYesterday(now)) { @@ -23,7 +23,9 @@ extension DateTimeExtension on DateTime { return formatDate(); } - bool _isToday(DateTime now) { + /// thisが今日かどうかの真偽値を返す + bool _isToday() { + final now = DateTime.now(); return year == now.year && month == now.month && day == now.day; } From 02d2f4eef7e95a9561fa085a47f6ca85004f74da Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 13:49:03 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=5FisYesterday()=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index e80f6e82..75be302e 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -29,7 +29,9 @@ extension DateTimeExtension on DateTime { return year == now.year && month == now.month && day == now.day; } - bool _isYesterday(DateTime now) { + /// thisが昨日かどうかの真偽値を返す + bool _isYesterday() { + final now = DateTime.now(); final yesterday = DateTime(now.year, now.month, now.day - 1); return year == yesterday.year && month == yesterday.month && From 655b052c1c6aa33078b04c62debffcebca8bc251 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 14:08:00 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=5FdaysBefore()=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/src/date_time.dart | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index 75be302e..0a1ce5ad 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -1,6 +1,15 @@ import 'package:intl/intl.dart'; extension DateTimeExtension on DateTime { + + /// _daysBeforeメソッドで使用される定数。 + /// 「N日前」と表示される最小日数を示す。 + static const _daysBeforeLowerLimit = 2; + + /// _daysBeforeメソッドで使用される定数。 + /// 「N日前」と表示される最大日数を示す。 + static const _daysBeforeUpperLimit = 7; + /// 「yyyy年MM月dd日」形式の文字列を返す。 String formatDate() => DateFormat('yyyy年MM月dd日').format(this); @@ -38,10 +47,14 @@ extension DateTimeExtension on DateTime { day == yesterday.day; } - /// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す - int? _daysBefore(DateTime now) { + /// thisが、現在時刻と比較して + /// [_daysBeforeLowerLimit] 日〜 [_daysBeforeUpperLimit] 日前ならその数字を、 + /// そうでなければnullを返す + int? _daysBefore() { + final now = DateTime.now(); final difference = now.difference(this).inDays; - if (difference >= 2 && difference <= 7) { + if (difference >= _daysBeforeLowerLimit && + difference <= _daysBeforeUpperLimit) { return difference; } return null; From 69acd09a263ee1373173177cb8a756c98def85a1 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 14:09:07 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E5=A4=89?= =?UTF-8?q?=E6=95=B0now=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index 0a1ce5ad..cab3a971 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -18,14 +18,13 @@ extension DateTimeExtension on DateTime { /// 現在時刻と比較して、相対的な日付文字列を返す。 String formatRelativeDate() { - final now = DateTime.now(); if (_isToday()) { return DateFormat('HH:mm').format(this); } - if (_isYesterday(now)) { + if (_isYesterday()) { return '昨日'; } - final daysBeforeResult = _daysBefore(now); + final daysBeforeResult = _daysBefore(); if (daysBeforeResult != null) { return '$daysBeforeResult日前'; } From aebff0f3018e9c2bcb015f9f96b5c33133d6772a Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 14:17:20 +0900 Subject: [PATCH 8/9] =?UTF-8?q?doc=20string=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index cab3a971..4efeaf42 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -16,7 +16,11 @@ extension DateTimeExtension on DateTime { /// 「yyyy年MM月dd日 HH時mm分」形式の文字列を返す。 String formatDateTime() => DateFormat('yyyy年MM月dd日 HH時mm分').format(this); - /// 現在時刻と比較して、相対的な日付文字列を返す。 + /// thisと現在時刻と比較して相対的な日付文字列を返す。 + /// thisが今日であれば、「HH:mm」 + /// 昨日であれば、「昨日」 + /// _daysBeforeLowerLimitから_daysBeforeUpperLimitの範囲内であれば、「N日前」 + /// それ以外は 'yyyy年MM月dd日' 形式の日付 String formatRelativeDate() { if (_isToday()) { return DateFormat('HH:mm').format(this); @@ -31,13 +35,15 @@ extension DateTimeExtension on DateTime { return formatDate(); } - /// thisが今日かどうかの真偽値を返す + /// thisが今日かどうかの真偽値を返す。 + /// thisと現在時刻の「年、月、日」が全て一致する場合には「今日」であるため真を返す。 bool _isToday() { final now = DateTime.now(); return year == now.year && month == now.month && day == now.day; } - /// thisが昨日かどうかの真偽値を返す + /// thisが昨日かどうかの真偽値を返す。 + /// thisと現在時刻より1日前の「年、月、日」が全て一致する場合には「昨日」であるため真を返す。 bool _isYesterday() { final now = DateTime.now(); final yesterday = DateTime(now.year, now.month, now.day - 1); From 54edd84ea3e5b73e4386e169edd9777414cf86c9 Mon Sep 17 00:00:00 2001 From: haterain0203 Date: Fri, 28 Jul 2023 14:20:21 +0900 Subject: [PATCH 9/9] =?UTF-8?q?doc=20string=E5=86=8D=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/dart_flutter_common/lib/src/date_time.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/dart_flutter_common/lib/src/date_time.dart b/packages/dart_flutter_common/lib/src/date_time.dart index 4efeaf42..0b3d3de6 100644 --- a/packages/dart_flutter_common/lib/src/date_time.dart +++ b/packages/dart_flutter_common/lib/src/date_time.dart @@ -43,7 +43,7 @@ extension DateTimeExtension on DateTime { } /// thisが昨日かどうかの真偽値を返す。 - /// thisと現在時刻より1日前の「年、月、日」が全て一致する場合には「昨日」であるため真を返す。 + /// thisと現在の日付より1日前の「年、月、日」が全て一致する場合には「昨日」であるため真を返す。 bool _isYesterday() { final now = DateTime.now(); final yesterday = DateTime(now.year, now.month, now.day - 1); @@ -52,8 +52,8 @@ extension DateTimeExtension on DateTime { day == yesterday.day; } - /// thisが、現在時刻と比較して - /// [_daysBeforeLowerLimit] 日〜 [_daysBeforeUpperLimit] 日前ならその数字を、 + /// thisと現在時刻との間の日数を計算し、 + /// その日数が[_daysBeforeLowerLimit] 日〜 [_daysBeforeUpperLimit] 日の範囲内ならその数字を、 /// そうでなければnullを返す int? _daysBefore() { final now = DateTime.now();