Time
#
// Go က အချိန်နဲ့ ကြာချိန်တွေအတွက် ကျယ်ပြန့်တဲ့ ထောက်ပံ့မှုပေးပါတယ်။
// ဒီမှာ ဥပမာတချို့ ရှိပါတယ်။
package main
import (
"fmt"
"time"
)
func main() {
p := fmt.Println
// လက်ရှိအချိန်ကို ရယူခြင်းဖြင့် စတင်ပါမယ်။
now := time.Now()
p(now)
// နှစ်၊ လ၊ ရက် စသည်တို့ကို ပေးပြီး `time` struct တစ်ခု တည်ဆောက်နိုင်ပါတယ်။
// အချိန်တွေက အမြဲတမ်း `Location` (ဆိုလိုသည်မှာ time zone) နဲ့ ဆက်စပ်နေပါတယ်။
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
// အချိန်တန်ဖိုးရဲ့ အစိတ်အပိုင်းအမျိုးမျိုးကို မျှော်လင့်ထားသလို ဆွဲထုတ်နိုင်ပါတယ်။
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
// တနင်္လာနေ့ကနေ တနင်္ဂနွေနေ့အထိ `Weekday` လည်း ရနိုင်ပါတယ်။
p(then.Weekday())
// ဒီ method တွေက အချိန်နှစ်ခုကို နှိုင်းယှဉ်ပြီး ပထမတစ်ခုက ဒုတိယတစ်ခုရဲ့
// အရှေ့၊ နောက်၊ သို့မဟုတ် တူညီတဲ့အချိန်မှာ ဖြစ်မဖြစ် အသီးသီး စစ်ဆေးပါတယ်။
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
// `Sub` method က အချိန်နှစ်ခုကြားက ကြားကာလကို ကိုယ်စားပြုတဲ့ `Duration` တစ်ခု ပြန်ပေးပါတယ်။
diff := now.Sub(then)
p(diff)
// ကြာချိန်ရဲ့ အရှည်ကို အမျိုးမျိုးသော unit တွေနဲ့ တွက်ချက်နိုင်ပါတယ်။
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
// ပေးထားတဲ့ ကြာချိန်တစ်ခုဖြင့် အချိန်တစ်ခုကို ရှေ့တိုးဖို့ `Add` ကို သုံးနိုင်ပါတယ်။
// သို့မဟုတ် `-` နဲ့တွဲသုံးပြီး ကြာချိန်တစ်ခုဖြင့် နောက်ပြန်ဆုတ်နိုင်ပါတယ်။
p(then.Add(diff))
p(then.Add(-diff))
}
$ go run time.go
2012-10-31 15:50:13.793654 +0000 UTC
2009-11-17 20:34:58.651387237 +0000 UTC
2009
November
17
20
34
58
651387237
UTC
Tuesday
true
false
false
25891h15m15.142266763s
25891.25420618521
1.5534752523711128e+06
9.320851514226677e+07
93208515142266763
2012-10-31 15:50:13.793654 +0000 UTC
2006-12-05 01:19:43.509120474 +0000 UTC
# နောက်တစ်ဆင့်မှာတော့ Unix epoch နဲ့ ဆက်စပ်နေတဲ့
# အချိန်အယူအဆကို ကြည့်ကြပါမယ်။