JSONの日付/時刻文字列からGoのtime.Timeへ

JSONの日付・時刻文字列をGoのtime.Timeフィールドに変換して処理します。ISO 8601のパース、カスタム時刻フォーマット、タイムゾーン処理を解説。

Advanced Types

詳細な説明

JSON日付文字列からtime.Timeへの変換

Goの encoding/json パッケージは、RFC 3339 / ISO 8601の日付文字列を time.Time フィールドに自動的にパースします。これはJSON APIで最も一般的な日付フォーマットです。

JSONの例

{
  "id": 42,
  "title": "Deploy v2.0",
  "created_at": "2024-06-15T09:30:00Z",
  "updated_at": "2024-06-15T14:22:33+09:00",
  "due_date": "2024-07-01"
}

生成されるGo構造体

import "time"

type Task struct {
    ID        int       `json:"id"`
    Title     string    `json:"title"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
    DueDate   string    `json:"due_date"`
}

自動的にパースされるもの

time.Time 型はRFC 3339をそのままサポートします:

  • "2024-06-15T09:30:00Z" — UTC
  • "2024-06-15T14:22:33+09:00" — タイムゾーンオフセット付き

カスタム処理が必要なもの

日付のみの文字列("2024-07-01")は time.Time に自動的にアンマーシャリングされません。2つの選択肢があります:

  1. string のまま保持し、time.Parse("2006-01-02", s) で手動パース
  2. カスタム型を作成し、UnmarshalJSON を実装:
type DateOnly struct {
    time.Time
}

func (d *DateOnly) UnmarshalJSON(b []byte) error {
    s := strings.Trim(string(b), "\"")
    t, err := time.Parse("2006-01-02", s)
    if err != nil {
        return err
    }
    d.Time = t
    return nil
}

タイムゾーンの認識

time.Time はJSON文字列からタイムゾーンを保持します。"Z" サフィックスのタイムスタンプでは CreatedAt.Location() がUTCを返します。一貫した比較のために常に .UTC() または .In(loc) を使用してください。

NullableなTimestamp

nullになりうるタイムスタンプには *time.Time を使用します:

DeletedAt *time.Time `json:"deleted_at,omitempty"`

ユースケース

タイムスタンプを持つすべてのAPI — イベントログ、監査証跡、スケジューリングシステム — は適切な時刻処理が必要です。time.Timeを使用すると、タイムゾーン対応の比較、フォーマット、期間の算術演算がすぐに利用できます。

試してみる — JSON to Go Struct Converter

フルツールを開く