考勤时间计算
package main import ( "encoding/json" "fmt" "io/ioutil" "os" "strconv" "time" ) type ResponseStruct struct { Signs []Sign `json:"signs"` } type Sign struct { InTime string `json:"inTime"` OutTime string `json:"outTime"` WorkDay bool `json:"workDay"` } func main() { jsonFile, err := os.Open("data.json") if err != nil { fmt.Println(err) } defer jsonFile.Close() byteValue, err := ioutil.ReadAll(jsonFile) if err != nil { fmt.Printf("ReadAll error: %s\n", err) } var response ResponseStruct err = json.Unmarshal(byteValue, &response) if err != nil { fmt.Println(err) } data, err := json.MarshalIndent(response.Signs, "", " ") if err != nil { fmt.Printf("JSON marshaling failed: %s", err) } fmt.Printf("%s\n", data) layout := "2006-01-02 15:04:05" var sum_minute int = 0 var sum_hour float32 = 0.0 var sum_minute2 int = 0 var sum_hour2 float32 = 0.0 var monthString string fmt.Printf("Please enter the KaoQin month: ") fmt.Scanln(&monthString) for _, value := range response.Signs { var KaoqinMonth time.Month Kaoqin, err := strconv.Atoi(monthString) if err != nil { fmt.Println(err) } KaoqinMonth = time.Month(Kaoqin) //工作日加班时长 if value.WorkDay && value.OutTime != "" { formatTime, err := time.Parse(layout, value.OutTime) fmt.Printf("工作日:%v\n", formatTime) if err != nil { fmt.Println("%s", err) } if formatTime.Month() == KaoqinMonth { if formatTime.Hour() >= 18 { sum_minute += formatTime.Minute() sum_hour += float32(formatTime.Hour() - 18) } } if KaoqinMonth == 12 { KaoqinMonth = 0 } if formatTime.Month() == KaoqinMonth+1 && formatTime.Day() <= 19 { if formatTime.Hour() >= 18 { sum_minute += formatTime.Minute() sum_hour += float32(formatTime.Hour() - 18) } } if formatTime.Month() == KaoqinMonth+1 && formatTime.Day() >= 20 { if formatTime.Hour() >= 18 { sum_minute2 += formatTime.Minute() sum_hour2 += float32(formatTime.Hour() - 18) } } } //周末加班时长 if !value.WorkDay && value.OutTime != "" { startTime, err := time.Parse(layout, value.InTime) if err != nil { fmt.Println("%s", err) } endTime, err := time.Parse(layout, value.OutTime) if err != nil { fmt.Println("%s", err) } fmt.Printf("周末:\nstartTime: %s\nendTime: %s\n", startTime, endTime) if startTime.Month() == KaoqinMonth { sum_minute += 60 + endTime.Minute() - startTime.Minute() sum_hour += float32(endTime.Hour() - startTime.Hour() - 1) } if KaoqinMonth == 12 { KaoqinMonth = 0 } if startTime.Month() == KaoqinMonth+1 && startTime.Day() <= 19 { sum_minute += 60 + endTime.Minute() - startTime.Minute() sum_hour += float32(endTime.Hour() - startTime.Hour() - 1) } if startTime.Month() == KaoqinMonth+1 && startTime.Day() >= 20 { sum_minute2 += 60 + endTime.Minute() - startTime.Minute() sum_hour2 += float32(endTime.Hour() - startTime.Hour() - 1) } } } sum_hour += float32(sum_minute) / 60.0 sum_hour2 += float32(sum_minute2) / 60.0 fmt.Printf("last month and current month <= 19 sum_hour = %f\n", sum_hour) fmt.Printf("current month >= 20 sum_hour2 = %f\n", sum_hour2) }
原始数据如上,通过按下F12,对浏览器进行调试,获取到response数据,对感兴趣的字段进行反序列化,存储到相应的
Struct结构体,再进行处理,得到考勤时间。哈哈哈,因为赖得手工去计算,所以就写了个脚本去帮我。
不能不说,用go处理json数据真是方便呐。