我有一个固定的数据结构,其中包含 YearDay 和 TimeOfDay 字段。YearDay 是当年过去的天数,TimeOfDay 是当天过去的秒数(最多 86400)。YearDay 是 int32,而 TimeOfDay 是 float64。
我想将其转换为 time.Now().UnixNano() 形式,但不确定如何转换。时间模块有一个 YearDay(),但没有给定 yearDay (int32)(可能是一年)的反函数来给我当月的月份和日期。
理想情况下,我想以某种方式解析
d := time.Date(time.Year(), month, day, hour, min, sec, ms, time.UTC)
其中月、日、小时、分钟、秒、毫秒以某种方式预先确定,或者我可以轻松转换为我想要的任何形式的等价物(但主要是 UnixNano())。
我最好的想象是一个复杂的 switch 语句,减去 31, 28(29), 30, 31 ... 并查看 int 何时最终为负以找到月份和日期,但它必须是两个 switch 语句闰年检查以选择使用哪个开关块,同时在 TimeOfDay 上进行多项余数计算。有没有更简单更干净的方法?
编辑:我最终在玩弄它时制作了以下功能,但我肯定会使用 Icza 的解决方案。很高兴知道日子会溢出。谢谢!
func findMonthAndDay(yearDay int32) (int32, int32) {
year := time.Now().Year()
isLeapYear := year%400 == 0 || year%4 == 0 && year%100 != 0 // Calculates if current year is leapyear
// Determines which array to send to for loop
var monthsOfYear [12]int32
if isLeapYear {
monthsOfYear = [12]int32{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
} else {
monthsOfYear = [12]int32{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
}
var currentMonth int32
var currentDayInMonth int32
// Loop through array of months
for i := range monthsOfYear {
// If yearDay - next month #OfDays positive, not correct month
if yearDay-monthsOfYear[i] > 0 {
// Subtract month #OfDays and continue
yearDay = yearDay - monthsOfYear[i]
} else {
currentMonth = int32(i + 1) // Month found (+1 due to index at 0)
currentDayInMonth = yearDay // Remainder of YearDay is day in month
break
}
}
return currentMonth, currentDayInMonth
}
郎朗坤
相关分类