如何在Go中将毫秒(uint64)转换为HH:MM:SS,MMM格式

huangapple go评论77阅读模式
英文:

How to convert millisecond (uint64) into HH:MM:SS,MMM format in Go

问题

在这种情况下,uint64值并没有指定一个具体的日期。
我使用uint64来表示视频中的一个特定时间点(以毫秒为单位)。
如标题所述,我需要将这个整数值转换为可读的格式。我认为使用简单的数学运算来解决这个问题会导致问题(如四舍五入等)。

下面是一个示例场景:

时间点 60104:指的是第0小时、第1分钟、第0秒和第104毫秒。

package main

import "fmt"
// 可能需要导入 time...

func main() {
	videoPoint := uint64(65104)

	// 我想要的结果是 00:01:05,104
	strVideoPoint := ToReadablePoint(videoPoint)
	fmt.Println(strVideoPoint)
}

func ToReadablePoint(point uint64) string {
	// 算法将在这里编写
	return ""
}

我使用了Go语言,但你也可以用C/C++编写算法。关键在于算法的实现。

英文:

In this scenario, the uint64 value does not specify a specific date.
I'm using uint64 to show a specific point of a video in milliseconds.
As in the title I need to convert this integer value. I think solving this using simple math would cause problems (rounding etc.).

Below is an example for the scenario:

Point 60104: Reference to 0th hour, 1st minute, 0th second and 104th millisecond.

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 ""
}

I used Go but you can also write the algorithm in C/C++. The key here is algorithm.

答案1

得分: 3

使用time

如果持续时间小于一天,你可以将其添加到一个具有零时间部分的参考时间戳中,然后使用适当的布局格式化时间。

参考时间可以是time.Time的零值或Unix参考时间。

例如:

ms := int64(65104)

var t time.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("%02d:%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("%02d:%02d:%02d,%03d", 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
英文:

Using the time package

If the duration is less than a day, you may simply add it to a reference timestamp having zero time part, then format the time using a proper layout.

The reference time may be the zero value of time.Time or the unix reference time.

For example:

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"))

This will output (try it on the Go Playground):

00:01:05,104
00:01:05,104

If you want to handle durations bigger than a day, this solution is not suitable. A possible solution is to calculate hours yourself, and use the above method for the rest (minutes, seconds, milliseconds).

For example:

const msInHour = 60 * 60 * 1000

func format(ms int64) string {
	hours := ms / msInHour
	ms = ms % msInHour
	t := time.UnixMilli(ms)
	return fmt.Sprintf("%02d:%s", hours, t.Format("04:05,000"))
}

Testing it:

fmt.Println(format(65104))
fmt.Println(format(27*60*60*1000 + 65104))

This will output (try it on the Go Playground):

00:01:05,104
27:01:05,104

Rolling your own solution

If you don't want to use the time package, you can do it yourself. The algorithm is simply divisions and remainers. E.g. the millisecond part is the remainder after dividing by 1000. The seconds after this is the remainder after dividing by 60 etc.

For example:

func format(n int64) string {
	ms := n % 1000
	n /= 1000
	sec := n % 60
	n /= 60
	min := n % 60
	n = n / 60
	return fmt.Sprintf("%02d:%02d:%02d,%03d", n, min, sec, ms)
}

This also handles durations greater than a day. Testing it:

fmt.Println(format(65104))
fmt.Println(format(27*60*60*1000 + 65104))

This will output (try it on the Go Playground):

00:01:05,104
27:01:05,104

huangapple
  • 本文由 发表于 2022年6月14日 21:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/72617911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定