Skip to content

Instantly share code, notes, and snippets.

@suganoo suganoo/01 time_pkg.go
Last active Dec 21, 2018

Embed
What would you like to do?
Tips of Tim pkg
package main
import (
"fmt"
"time"
"strconv"
)
func main() {
fmt.Println("----- Basic usage -----")
now := time.Now()
fmt.Println("Now() : " + now.String())
time1 := time.Date(2018, 10, 15, 16, 48, 32, 12345, time.Local)
fmt.Println("Date() : " + time1.String())
fmt.Println()
fmt.Println("----- Functions -----")
fmt.Println("Year() : " + strconv.Itoa(now.Year()))
fmt.Println("Month() : " + now.Month().String())
fmt.Println("int(now.Month()) : " + strconv.Itoa(int(now.Month())))
fmt.Println("Day() : " + strconv.Itoa(now.Day()))
fmt.Println("Hour() : " + strconv.Itoa(now.Hour()))
fmt.Println("Minute() : " + strconv.Itoa(now.Minute()))
fmt.Println("Second() : " + strconv.Itoa(now.Second()))
fmt.Println("Nanosecond() : " + strconv.Itoa(now.Nanosecond()))
fmt.Println("Weekday() : " + now.Weekday().String())
fmt.Println("int(Weekday()) : " + strconv.Itoa(int(now.Weekday())))
fmt.Println("Unix() : " + strconv.FormatInt(now.Unix(), 10))
fmt.Println("UnixNano() : " + strconv.FormatInt(now.UnixNano(), 10))
iso_year, iso_week := now.ISOWeek()
fmt.Println("ISOWeek() year : " + strconv.Itoa(iso_year))
fmt.Println("ISOWeek() week : " + strconv.Itoa(iso_week))
fmt.Println()
fmt.Println("----- Simple Functions -----")
fmt.Println("Date() Clock()")
year, month, day := now.Date()
hour, min, sec := now.Clock()
fmt.Println(year)
fmt.Println(int(month))
fmt.Println(day)
fmt.Println(hour)
fmt.Println(min)
fmt.Println(sec)
fmt.Println()
fmt.Println("YearDay() : " + strconv.Itoa(now.YearDay()))
fmt.Println("UTC() : " + now.UTC().String())
fmt.Println("Local() : " + now.Local().String())
zoneName, zoneOffset := now.Zone()
fmt.Println("Zone() (name,offset) : " + "( " + zoneName + " , " + strconv.Itoa(zoneOffset) + " )")
fmt.Println()
fmt.Println("----- Add -----")
fmt.Println("Now() : " + now.String())
now = now.Add(time.Duration(-1) * time.Hour)
fmt.Println("time 1 hour ago : " + now.String())
fmt.Println()
fmt.Println("----- Parse() Format()-----")
var timeParse01 = time.Time{}
timeParse01, _ = time.Parse("2006/01/02 15:04:05 (MST)", "2018/12/21 13:45:38 (JST)")
fmt.Println("Parse() : " + timeParse01.String())
fmt.Println("Format(ANSIC) : " + timeParse01.Format(time.ANSIC))
fmt.Println("Format(RFC1123Z) : " + timeParse01.Format(time.RFC1123Z))
fmt.Println()
fmt.Println("----- ParseDuration() -----")
duration, _ := time.ParseDuration("2h35m10s")
fmt.Println("Duration String : " + duration.String())
fmt.Println("Duration in minutes : " + strconv.FormatFloat(duration.Minutes(), 'f', 0, 64))
fmt.Println("Duration in seconds : " + strconv.FormatFloat(duration.Seconds(), 'f', 0, 64))
fmt.Println()
fmt.Println("----- Sub() Since() Until() -----")
time01 := time.Date(2018, 10, 10, 15, 0, 0, 0, time.Local)
time02 := time.Date(2018, 10, 25, 15, 30, 10, 0, time.Local)
fmt.Println("time01 : " + time01.String())
fmt.Println("time02 : " + time02.String())
time01Subtime02 := time01.Sub(time02)
time02Subtime01 := time02.Sub(time01)
fmt.Println("time01.Sub(time02) : " + time01Subtime02.String())
fmt.Println("time02.Sub(time01) : " + time02Subtime01.String())
fmt.Println()
time03 := time.Date(2018, 12, 10, 0, 0, 0, 0, time.Local)
time04 := time.Date(2018, 12, 25, 0, 0, 0, 0, time.Local)
fmt.Println("Now() : " + now.String())
fmt.Println("time03 : " + time03.String())
fmt.Println("time04 : " + time04.String())
fmt.Println("time.Since(time03) : " + time.Since(time03).String())
fmt.Println("time.Until(time04) : " + time.Until(time04).String())
fmt.Println()
fmt.Println("----- After() Before() -----")
date11m := time.Date(2018, 11, 1, 0, 0, 0, 0, time.Local)
date12m := time.Date(2018, 12, 1, 0, 0, 0, 0, time.Local)
fmt.Println(date11m)
fmt.Println(date12m)
isDate11mAfterDate12m := date11m.After(date12m)
isDate12mAfterDate11m := date12m.After(date11m)
fmt.Printf("date11m.After(date12m) = %v\n", isDate11mAfterDate12m)
fmt.Printf("date12m.After(date11m) = %v\n", isDate12mAfterDate11m)
isDate11mBeforeDate12m := date11m.Before(date12m)
isDate12mBeforeDate11m := date12m.Before(date11m)
fmt.Printf("date11m.Before(date12m) = %v\n", isDate11mBeforeDate12m)
fmt.Printf("date12m.Before(date11m) = %v\n", isDate12mBeforeDate11m)
fmt.Println()
fmt.Println("----- Equal() -----")
secondsEastOfUTC := int((8 * time.Hour).Seconds())
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
timeBeijin := time.Date(2018, 2, 1, 20, 15, 45, 0, beijing)
timeUtc := time.Date(2018, 2, 1, 12, 15, 45, 0, time.UTC)
fmt.Println(timeBeijin)
fmt.Println(timeUtc)
datesEqualUsingEqualOperator := timeBeijin == timeUtc
datesEqualUsingFunction := timeBeijin.Equal(timeUtc)
fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator)
fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction)
fmt.Println()
fmt.Println("----- Round() Truncate() -----")
time10 := time.Date(2018, 12, 10, 14, 15, 45, 987654321, time.Local)
fmt.Println("time10 : " + time10.String())
fmt.Println("time10.Round(time.Hour) : " + time10.Round(time.Hour).String())
fmt.Println("time10.Round(time.Minute) : " + time10.Round(time.Minute).String())
fmt.Println("time10.Round(time.Second) : " + time10.Round(time.Second).String())
fmt.Println("time10.Truncate(time.Hour) : " + time10.Truncate(time.Hour).String())
fmt.Println("time10.Truncate(time.Minute): " + time10.Truncate(time.Minute).String())
fmt.Println("time10.Truncate(time.Second): " + time10.Truncate(time.Second).String())
fmt.Println()
fmt.Println("----- IsZero() -----")
timeRight, _ := time.Parse("2006/01/02 15:04", "2018/12/21 13:45")
timeFail, _ := time.Parse("2006/01/02 15:04", "2018/12/21 hoge 13:45")
fmt.Println("timeRight : " + timeRight.String())
fmt.Println("timeFail : " + timeFail.String())
fmt.Println("timeRight.IsZero() : " + strconv.FormatBool(timeRight.IsZero()))
fmt.Println("timeFail.IsZero() : " + strconv.FormatBool(timeFail.IsZero()))
fmt.Println()
}
----- Basic usage -----
Now() : 2018-12-21 18:39:05.7407367 +0900 JST m=+0.026072201
Date() : 2018-10-15 16:48:32.000012345 +0900 JST
----- Functions -----
Year() : 2018
Month() : December
int(now.Month()) : 12
Day() : 21
Hour() : 18
Minute() : 39
Second() : 5
Nanosecond() : 740736700
Weekday() : Friday
int(Weekday()) : 5
Unix() : 1545385145
UnixNano() : 1545385145740736700
ISOWeek() year : 2018
ISOWeek() week : 51
----- Simple Functions -----
Date() Clock()
2018
12
21
18
39
5
YearDay() : 355
UTC() : 2018-12-21 09:39:05.7407367 +0000 UTC
Local() : 2018-12-21 18:39:05.7407367 +0900 JST
Zone() (name,offset) : ( JST , 32400 )
----- Add -----
Now() : 2018-12-21 18:39:05.7407367 +0900 JST m=+0.026072201
time 1 hour ago : 2018-12-21 17:39:05.7407367 +0900 JST m=-3599.973927799
----- Parse() Format()-----
Parse() : 2018-12-21 13:45:38 +0900 JST
Format(ANSIC) : Fri Dec 21 13:45:38 2018
Format(RFC1123Z) : Fri, 21 Dec 2018 13:45:38 +0900
----- ParseDuration() -----
Duration String : 2h35m10s
Duration in minutes : 155
Duration in seconds : 9310
----- Sub() Since() Until() -----
time01 : 2018-10-10 15:00:00 +0900 JST
time02 : 2018-10-25 15:30:10 +0900 JST
time01.Sub(time02) : -360h30m10s
time02.Sub(time01) : 360h30m10s
Now() : 2018-12-21 17:39:05.7407367 +0900 JST m=-3599.973927799
time03 : 2018-12-10 00:00:00 +0900 JST
time04 : 2018-12-25 00:00:00 +0900 JST
time.Since(time03) : 282h39m5.8604157s
time.Until(time04) : 77h20m54.1395843s
----- After() Before() -----
2018-11-01 00:00:00 +0900 JST
2018-12-01 00:00:00 +0900 JST
date11m.After(date12m) = false
date12m.After(date11m) = true
date11m.Before(date12m) = true
date12m.Before(date11m) = false
----- Equal() -----
2018-02-01 20:15:45 +0800 Beijing Time
2018-02-01 12:15:45 +0000 UTC
datesEqualUsingEqualOperator = false
datesEqualUsingFunction = true
----- Round() Truncate() -----
time10 : 2018-12-10 14:15:45.987654321 +0900 JST
time10.Round(time.Hour) : 2018-12-10 14:00:00 +0900 JST
time10.Round(time.Minute) : 2018-12-10 14:16:00 +0900 JST
time10.Round(time.Second) : 2018-12-10 14:15:46 +0900 JST
time10.Truncate(time.Hour) : 2018-12-10 14:00:00 +0900 JST
time10.Truncate(time.Minute): 2018-12-10 14:15:00 +0900 JST
time10.Truncate(time.Second): 2018-12-10 14:15:45 +0900 JST
----- IsZero() -----
timeRight : 2018-12-21 13:45:00 +0000 UTC
timeFail : 0001-01-01 00:00:00 +0000 UTC
timeRight.IsZero() : false
timeFail.IsZero() : true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.