package backup

import (
	"log"
	"time"
	"strings"

	"box-gm/utils"
)

func getNextTime(now time.Time, startTime string) time.Time {
	arr := strings.Split(startTime, ":")
	hour := utils.StringToInt(arr[0])
	min := utils.StringToInt(arr[1])
	sec := utils.StringToInt(arr[2])
	next := time.Date(now.Year(), now.Month(), now.Day(), hour, min, sec, 0, now.Location())
	if now.Before(next) {
		return next
	}

	next = now.Add(time.Hour * 24)
	next = time.Date(next.Year(), next.Month(), next.Day(), hour, min, sec, 0, next.Location())
	return next
}

// 备份程序初始化
func Init_backup_event_proc() {
	for {
		now := time.Now()
		//返回一个指定时间的下次执行的时间戳
		timeBackup := utils.GetKeyConf("backup", "time_event")
		if timeBackup == "" {
			break
		}
		next := getNextTime(now, timeBackup)
		t := time.NewTimer(next.Sub(now))
		log.Printf("下一次日志备份开始时间为[%s]", next.String())
		<-t.C
		log.Println("备份日志")
		go run_event_backup()
	}
}

// 开始备份
func run_event_backup() {

}