LoginSignup
1
0

More than 3 years have passed since last update.

Goで経過時間を計算するパッケージを作った

Posted at

動機

年月を計算する処理がなかったから。

X は Y から A 年 B ヶ月 C 日 D 時間 F 分 E 秒 経過しています

というのをやろうとすると、

C 日 D 時間 F 分 E 秒

はタイムスタンプの差から計算することができますが、それ以降はうるう年などの関係から単純に計算することができません

作ったもの

使い方

d := New(time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), time.Date(2001, 12, 10, 11, 10, 20, 0, time.Local))

// 時間差の取得
years, months, days, hours, mins, sec := d.Term()
// years:  1
// months: 11
// days:   9
// hours:  11
// mins:   10
// sec:    20

// 年単位で計算で経過時間を計測
years = d.Years()
// 1

// 月単位で計算で経過時間を計測
months = d.Months()
// 23

// 日単位で計算で経過時間を計測
days = d.Days()
// 709

// 時間単位で計算で経過時間を計測
hours = d.Hours()
// 17027

// 分単位で計算で経過時間を計測
mins = d.Minutes()
// 1021630

// 秒単位で計算で経過時間を計測
sec = d.Seconds()
// 61297820
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0