Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#38 create date time method #89

Merged
merged 10 commits into from
Jul 28, 2023
Merged

#38 create date time method #89

merged 10 commits into from
Jul 28, 2023

Conversation

haterain0203
Copy link
Collaborator

Issue

close #38

説明

  1. DateTimeのExtensionに現在時刻と比較して以下条件で文字列を返すメソッド追加
  • 同じ日付 →「hh:mm」
  • 1 日前の日付 →「昨日」
  • N (2 <= N <= Nmax) 日前の日付 →「N 日前」(Nmax は 7 にしておく)
  • それより昔の日付 →「yyyy年MM月dd日」
  1. 上記で追加したメソッドのユニットテストを追加

UI

変更なし

その他

DateTime? も受け付けるべきかどうかは担当者が考えて判断する

DateTime?は許容しない設定とした。
DateTime?になるケースとして、特定のデータの日付情報がオプショナルになっている場合などが想定されるが、そういったデータを使ってこのextensionを呼ぶことはないのでは?と考えた。

と考えたのですが、どうでしょうか・・・?

チェックリスト

  • PR の冒頭に関連する Issue 番号を記載しましたか?
  • 本 PR の変更に関して、エディタや IDE で意図しない警告は増えていませんか?(lint 警告やタイポなど)
  • Issue の完了の定義は満たせていますか?

@@ -6,4 +6,40 @@ extension DateTimeExtension on DateTime {

/// 「yyyy年MM月dd日 HH時mm分」形式の文字列を返す。
String formatDateTime() => DateFormat('yyyy年MM月dd日 HH時mm分').format(this);

/// 現在時刻と比較して、相対的な日付文字列を返す。
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc string を書いているのはとても素晴らしいと思います!(たとえば例とかも書いちゃっていいかもですね、Chat GPT にこの実装をまるっと投げて、doc string 書いてください、とか言うと結構いい仕事してくれます)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!

GPT先生の力も借りながら、doc stringを加筆修正しました!

return formatDate();
}

bool _isToday(DateTime now) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このメソッド内の処理を見ると「引数で渡された DateTime が、this と同じ日付か」という処理になっていますね。そうだとしたらメソッド名は _isToday ではなく _isSameDay のような名前になるべきだと思います。

_isToday のメソッド名通り、「this が今日かどうか」というメソッドにしたいのなら、DateTime now は外から渡すのではなく、_isToday の処理内部にあるべきだと思います!

意図しているのはどちらでしょうか?(実装するときに、doc comment に「「引数で渡された DateTime が、this と同じ日付かどうかの真偽値を返す」とか「「this が今日かどうかの真偽値を返す」とかを、事前に書く癖をつけると、自分のやりたいこと・意図を明確にしたコードを書けるようなり、doc comment もついて一石二鳥だと思います!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにーーーーとなりました・・・

「this が今日かどうか」というメソッドにしたいという意図だったので、修正しました。
加えて、doc stringも追加しました。

実装するときに、doc comment に「「引数で渡された DateTime が、this と同じ日付かどうかの真偽値を返す」とか「「this が今日かどうかの真偽値を返す」とかを、事前に書く癖をつけると、自分のやりたいこと・意図を明確にしたコードを書けるようなり、doc comment もついて一石二鳥だと思います!

意識していきます!!!!

return year == now.year && month == now.month && day == now.day;
}

bool _isYesterday(DateTime now) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isToday と同様。

  • 「引数で渡された DateTime が、this の 1 日前の日付か」
  • 「this が昨日かどうか」

を考えて同様の修正をお願いします!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isToday同様に、「this が昨日かどうか」が意図だったので修正しました。

}

/// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す
int? _daysBefore(DateTime now) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isToday と同様。DateTime now を外から渡すので意図どおりか確かめる。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isTodayと同様に修正しました。

/// 現在と比較して2日〜7日前ならその数字を、そうでなければnullを返す
int? _daysBefore(DateTime now) {
final differece = now.difference(this).inDays;
if (differece >= 2 && differece <= 7) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 と 7 がいわゆるマジックナンバーになっているので、次のようなリファクタを見当してください:

extension DateTimeExtension on DateTime {
  /// 適切な変数名にする。適切な doc comment を書く。
  static const _a = 2;

  /// 適切な変数名にする。適切な doc comment を書く。
  static const _b = 7;
  
  // ... 省略

  /// 現在と比較して [_a] 日 〜 [_b] 日前ならその数字を、そうでなければnullを返す
  int? _daysBefore(DateTime now) {
    final differece = now.difference(this).inDays;
    if (differece >= _a && differece <= _b) {
      return differece;
    }
    return null;
  }
}

あと、differece がタイポです。エディタのタイポの警告は出ていませんか?(必要ならタイポチェックの設定をすることを強く勧めます)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらもありがとうございます!
マジックナンバーにならないよう以後留意します!

定数名など問題ないかご確認お願いします🙇‍♂️

タイポの件、失礼しました(前回も同じタイポの指摘を受けたので反省してます)
先ほどタイポチェックの拡張機能を入れました・・・!

@kosukesaigusa
Copy link
Owner

DateTime?は許容しない設定とした。DateTime?になるケースとして、特定のデータの日付情報がオプショナルになっている場合などが想定されるが、そういったデータを使ってこのextensionを呼ぶことはないのでは?と考えた。

特に Firestore を使っているとその Timestamp 型のフィールドが Dart では null 許容の DateTime? になるのはよくあるので、そういうときは一律で空文字を返すのも有りかもと思いつつでした。が、使う側で DateTime? の null チェックをしてから使用するでもいいと思うので、本件この方針で了解しました!

@haterain0203
Copy link
Collaborator Author

DateTime?は許容しない設定とした。DateTime?になるケースとして、特定のデータの日付情報がオプショナルになっている場合などが想定されるが、そういったデータを使ってこのextensionを呼ぶことはないのでは?と考えた。

特に Firestore を使っているとその Timestamp 型のフィールドが Dart では null 許容の DateTime? になるのはよくあるので、そういうときは一律で空文字を返すのも有りかもと思いつつでした。が、使う側で DateTime? の null チェックをしてから使用するでもいいと思うので、本件この方針で了解しました!

確かに、Firestoreはnull 許容の DateTime? になるのはよくありましたね・・・
今回はこのままで良いとのこと承知しました!
ちなみに、、、どちらで対応するのが一般的とかあるのでしょうか?

@kosukesaigusa
Copy link
Owner

確かに、Firestoreはnull 許容の DateTime? になるのはよくありましたね・・・
今回はこのままで良いとのこと承知しました!
ちなみに、、、どちらで対応するのが一般的とかあるのでしょうか?

DateTime? が null だったら空文字を返す」(Firestore の TimestampFieldValue.serverTimestamp() を使用すると一瞬 null になることを意識しちゃっている)は、「いっぱんてな DateTime に関する機能」としてはちょっと乱暴な気もするので、FUKU さんの今回の提案の方がいいなと思いました!

Copy link
Owner

@kosukesaigusa kosukesaigusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正ありがとうございました!!Approved!

@haterain0203 haterain0203 merged commit 7b610f4 into main Jul 28, 2023
1 check passed
@haterain0203 haterain0203 deleted the #38_create_date_time_method branch July 28, 2023 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DateTime 関係の機能実装
2 participants