main
wpl 2024-10-11 20:38:52 +08:00
parent b77fecd9f4
commit cb978f67ed
7 changed files with 152 additions and 0 deletions

BIN
day06/log_demo/log_demo.exe Normal file

Binary file not shown.

40
day06/log_demo/main.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"project/day06/mylogger_test"
"time"
)
// type logger struct {
// }
// func Newlog()Logger {
// return Logger{}
// }
func main() {
// fileObj, err := os.OpenFile("./xx.log", os.O_APPEND|os.O_CREATE, 0644)
// if err != nil {
// fmt.Printf("error:%v", err)
// return
// }
//debug
//trace
//info
//warning
//error
//fatal
//日志要支持开关
//日志要有时间、行号、文件名、日志级别、日志信息
//日志文件要切割
log := mylogger_test.NewLog()
for {
log.Debug("这是一条Debug log")
log.Info("这是一条info log")
log.Warning("这是一条warning log")
log.Error("error")
time.Sleep(time.Second * 3)
}
}

View File

@ -0,0 +1,29 @@
package mylogger_test
import "fmt"
//logger 日志结构体
type Logger struct {
}
//Newlog 构造函数
func NewLog() Logger {
return Logger{}
}
func (l Logger) Debug(msg string) {
fmt.Println(msg)
}
func (l Logger) Info(msg string) {
fmt.Println(msg)
}
func (l Logger) Warning(msg string) {
fmt.Println(msg)
}
func (l Logger) Error(msg string) {
fmt.Println(msg)
}
func (l Logger) Fatal(msg string) {
fmt.Println(msg)
}

46
day06/time/main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Year())
fmt.Println(now)
fmt.Println(now.Date())
fmt.Println(time.Unix(1564803667, 0))
//时间间隔
fmt.Println(time.Second)
//now +24小时
fmt.Println(now.Add(24 * time.Millisecond * 1000 * 60 * 60))
//格式化时间
t, _ := time.Parse(time.Stamp, "Jan 11,2024 at 7.12am(PST)")
fmt.Println(time.Stamp)
fmt.Println(t)
//按照对应的格式解析字符串类型的时间
timeObj, err := time.Parse("2006-01-02", "2019-08-03")
if err != nil {
fmt.Printf("parse time failed,err:%v\n", err)
return
}
fmt.Println(timeObj)
fmt.Println(timeObj.Unix())
//sub 两个时间相减
nextYear, err := time.Parse("2006-01-02", "2024-10-11")
if err != nil {
fmt.Printf("parse time failed,err:%v\n", err)
return
}
d := now.Sub(nextYear)
fmt.Println(d)
fmt.Println("------------------")
nextYear1 := nextYear.UTC()
fmt.Println(nextYear1)
fmt.Println("-----------")
time.Sleep(time.Duration(100) * time.Second)
fmt.Println("100s has gone")
}

BIN
day06/time/time.exe Normal file

Binary file not shown.

37
day06/zone/main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"fmt"
"time"
)
// func f1() {
// }
func f2() {
now := time.Now() //当前时间
fmt.Println(now)
//根据东八区的时区和格式去解析一个字符串格式的时间
//根据字符串加载时区
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
if err != nil {
fmt.Printf("locate time failed,err:%v\n", err)
return
}
}
//按照指定时区解析时间
timeObj, err := time.ParseInLocation("2006-01-02 15:04:05", "2024-10-11 19:59:00", loc)
if err != nil {
fmt.Printf("parse time failed,err:%v\n", err)
return
}
fmt.Println(timeObj)
td := timeObj.Sub(now)
fmt.Println(td)
}
func main() {
f2()
}

BIN
day06/zone/zone.exe Normal file

Binary file not shown.