最近公司需要每月计算餐补(按工作日,每日15元),然后自己去弄发票。
遂写个程序帮助自己计算工作日和这个月的全勤餐补。
程序如下(公司的年假比国家的长,所以如果和我不是一家公司的话需要复用还需要自行修改holiday和specialday数组)。
package main import ( "os" "fmt" "time" "strconv" ) func PrintUsage(){ fmt.Println("Usage:./calMoney [month]. Exp: ./calMoney 2 ") } func calDays(curYear int64,curMonth int64) int64{ if curMonth != 2{ if curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11{ return 30 }else{ return 31 } }else{ if (curYear%4 == 0 && curYear%100 != 0) || curYear%400 == 0 { return 29 }else{ return 28 } } } func isHoliday(curDate string) bool{ //是2016年的节假日,其他年份需要相应调整 var holiday []string = []string{ "01-01","01-02","01-03", "02-03","02-04","02-05","02-06","02-07","02-08","02-09","02-10","02-11","02-12","02-13","02-14", "04-04","04-05","04-06", "05-01","05-02","05-03", "06-09","06-10","06-11", "09-15","09-16","09-17", "10-01","10-02","10-03","10-04","10-05","10-06","10-07",} for _,curHoliday := range holiday{ if curHoliday == curDate{ return true } } return false } func isSpecialday(curDate string) bool{ //是2016年的调休【即星期六星期天需要上班的日子】,其他年份需要相应调整 var specialday []string = []string{ "06-12","09-18","10-08","10-09",} for _,curSpecialDay := range specialday{ if curSpecialDay == curDate{ return true } } return false } func CalMoney(curYear int64,curMonth int64){ dayOfMonth := calDays(curYear,curMonth) workDay := 0 var curDay int64 for curDay=1; curDay<=dayOfMonth; curDay++{ var curTimeStr string if curMonth < 10{ if curDay < 10{ curTimeStr = fmt.Sprintf("%d-0%d-0%d 00:00:00",curYear,curMonth,curDay) }else{ curTimeStr = fmt.Sprintf("%d-0%d-%d 00:00:00",curYear,curMonth,curDay) } }else{ if curDay < 10{ curTimeStr = fmt.Sprintf("%d-%d-0%d 00:00:00",curYear,curMonth,curDay) }else{ curTimeStr = fmt.Sprintf("%d-%d-%d 00:00:00",curYear,curMonth,curDay) } } loc, _ := time.LoadLocation("Local") curTime,_ := time.ParseInLocation("2006-01-02 15:04:05",curTimeStr,loc) if ((curTime.Weekday() == time.Saturday || curTime.Weekday() == time.Sunday) && !isSpecialday(curTimeStr[5:10])) || isHoliday(curTimeStr[5:10]){ continue }else{ fmt.Println(curTimeStr[0:10]) } workDay += 1 } //餐补一天15元 moneyOfDay := 15 //当月餐补 fmt.Printf("total money:%d\n",workDay*moneyOfDay) } func main(){ var curYear int64 = 2016 if len(os.Args) != 2 { PrintUsage() return } curMonth,err := strconv.ParseInt(os.Args[1],10,64) if err != nil { PrintUsage() return } CalMoney(curYear,curMonth) }
2月份的工作日和餐补如截图所示。