在這種情況下,uint64 值不指定特定日期。
我正在使用 uint64 以毫秒為單位顯示視頻的特定點。
如標題所示,我需要轉換此整數值。我認為使用簡單的數學來解決這個問題會導致問題(四舍五入等)。
以下是該場景的示例:
點 60104:參考第 0 小時、第 1 分鐘、第 0 秒和第 104 毫秒。
package main
import "fmt"
// maybe time...
func main() {
videoPoint := uint64(65104)
// I want 00:01:05,104
strVideoPoint := ToReadablePoint(videoPoint)
fmt.Println(strVideoPoint)
}
func ToReadablePoint(point uint64) string {
// algorithm will be written here
return ""
}
我使用 Go,但您也可以用 C/C 撰寫演算法。這里的關鍵是演算法。
uj5u.com熱心網友回復:
使用time包
如果持續時間少于一天,您可以簡單地將其添加到具有零時間部分的參考時間戳,然后使用適當的布局格式化時間。
參考時間可以是time.Timeunix參考時間的零值。
例如:
ms := int64(65104)
var t time.Time // Zero time
t = t.Add(time.Duration(ms) * time.Millisecond)
fmt.Println(t.Format("15:04:05,000"))
t = time.UnixMilli(ms)
fmt.Println(t.Format("15:04:05,000"))
這將輸出(在Go Playground上嘗試):
00:01:05,104
00:01:05,104
如果您想處理大于一天的持續時間,則此解決方案不適合。一個可能的解決方案是自己計算小時數,其余部分使用上述方法(分鐘、秒、毫秒)。
例如:
const msInHour = 60 * 60 * 1000
func format(ms int64) string {
hours := ms / msInHour
ms = ms % msInHour
t := time.UnixMilli(ms)
return fmt.Sprintf("d:%s", hours, t.Format("04:05,000"))
}
測驗它:
fmt.Println(format(65104))
fmt.Println(format(27*60*60*1000 65104))
這將輸出(在Go Playground上嘗試):
00:01:05,104
27:01:05,104
推出自己的解決方案
如果你不想使用這個time包,你可以自己做。該演算法只是除法和余數。例如,毫秒部分是除以 1000 后的余數。此后的秒數是除以 60 后的余數等。
例如:
func format(n int64) string {
ms := n % 1000
n /= 1000
sec := n % 60
n /= 60
min := n % 60
n = n / 60
return fmt.Sprintf("d:d:d,d", n, min, sec, ms)
}
這也處理超過一天的持續時間。測驗它:
fmt.Println(format(65104))
fmt.Println(format(27*60*60*1000 65104))
這將輸出(在Go Playground上嘗試):
00:01:05,104
27:01:05,104
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/491183.html
上一篇:POSThttps://localhost:5000/storednet::ERR_SSL_PROTOCOL_ERROR
下一篇:分割有理貝塞爾曲線
