LCOV - code coverage report
Current view: top level - shared/util - date.go Coverage Total Hit
Test: coverage.lcov Lines: 0.0 % 49 0
Test Date: 2026-04-14 06:42:22 Functions: - 0 0

            Line data    Source code
       1              : // Package util はアプリケーション全体で共有される汎用的なユーティリティを集めたパッケージです。
       2              : // 本ファイル (date.go) では日付関連の関数(年齢・年代計算など誕生日からの年齢・年代計算など、ビュー層での派生情報生成)を提供します。
       3              : package util
       4              : 
       5              : import (
       6              :         "time"
       7              : )
       8              : 
       9              : // CalcAge は誕生日から現在の満年齢を返します。
      10              : // birth が nil または未来日の場合は 0 を返します。
      11            0 : func CalcAge(birth *time.Time) int {
      12            0 :         if birth == nil {
      13            0 :                 return 0
      14            0 :         }
      15            0 :         now := time.Now()
      16            0 :         y, m, d := now.Date()
      17            0 :         by, bm, bd := birth.Date()
      18            0 :         age := y - by
      19            0 :         if (m < bm) || (m == bm && d < bd) {
      20            0 :                 age--
      21            0 :         }
      22            0 :         if age < 0 {
      23            0 :                 return 0
      24            0 :         }
      25            0 :         return age
      26              : }
      27              : 
      28              : // CalcAgeGroup は年齢から年代(10の倍数)を整数で返します。
      29              : // 例: 25 → 20, 38 → 30, 61 → 60。
      30              : // 年齢が0以下の場合は 0 を返します。
      31            0 : func CalcAgeGroup(age int) int {
      32            0 :         if age <= 0 {
      33            0 :                 return 0
      34            0 :         }
      35            0 :         if age < 10 {
      36            0 :                 return 0 // 10歳未満扱い
      37            0 :         }
      38            0 :         return (age / 10) * 10
      39              : }
      40              : 
      41              : // FormatDatePtr は *time.Time を ISO8601 (YYYY-MM-DD) 形式の *string に変換します。
      42              : // nil の場合は nil を返します。
      43            0 : func FormatDatePtr(t *time.Time) *string {
      44            0 :         if t == nil {
      45            0 :                 return nil
      46            0 :         }
      47            0 :         s := t.Format("2006-01-02")
      48            0 :         return &s
      49              : }
      50              : 
      51              : // FormatDatePtrWithLayout は指定レイアウトで *time.Time を *string に変換します。
      52            0 : func FormatDatePtrWithLayout(t *time.Time, layout string) *string {
      53            0 :         if t == nil {
      54            0 :                 return nil
      55            0 :         }
      56            0 :         s := t.Format(layout)
      57            0 :         return &s
      58              : }
      59              : 
      60              : // ParseYearMonth は "2006-01" 形式の文字列ポインタをパースして *time.Time を返します。
      61              : // 入力が nil または空文字の場合は nil を返します。
      62              : // パースエラー時は error を返します。
      63            0 : func ParseYearMonth(s *string) (*time.Time, error) {
      64            0 :         if s == nil || *s == "" {
      65            0 :                 return nil, nil
      66            0 :         }
      67            0 :         t, err := time.Parse("2006-01", *s)
      68            0 :         if err != nil {
      69            0 :                 return nil, err
      70            0 :         }
      71            0 :         return &t, nil
      72              : }
      73              : 
      74              : // ParseYearMonthVal は "2006-01" 形式の文字列ポインタをパースして time.Time (値) を返します。
      75              : // 入力が nil/空文字/エラーの場合はゼロ値を返します(必須項目用)。
      76              : // エラーハンドリングを厳密にしたい場合は上記 ParseYearMonth を使って呼び出し元で処理してください。
      77            0 : func ParseYearMonthVal(s *string) (time.Time, error) {
      78            0 :         if s == nil || *s == "" {
      79            0 :                 return time.Time{}, nil
      80            0 :         }
      81            0 :         return time.Parse("2006-01", *s)
      82              : }
        

Generated by: LCOV version 2.3.1-1