如何在time.Format中去除毫秒值中的小数点?

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

How to remove the dot in the milliseconds value in time.Format

问题

我想使用Go语言的time.Format函数生成具有特定格式的时间:

yyyy_MM_dd_HH_mm_ss_SSS    // SSS == 毫秒

根据文档,我得到了以下结果(使用stdFracSecond0):

format := "2006_01_02_15_04_05_.000"
fmt.Println(time.Now().Format(format))

输出结果为:

2021_06_18_10_21_19_.179    

如何去掉毫秒值前面的点号?

以下方式不起作用(毫秒始终为零):

format := "2006_01_02_15_04_05_000"

Playground 示例

英文:

I want to produce a time with this specific format in Go using the time.Format function:

yyyy_MM_dd_HH_mm_ss_SSS    // SSS == milliseconds

Following the documentation, I got to this point (using stdFracSecond0):

format := "2006_01_02_15_04_05_.000"
fmt.Println(time.Now().Format(format))

Which outputs:

2021_06_18_10_21_19_.179    

How can I remove the dot before the milliseconds value?

This does not work (millis are always zero):

format := "2006_01_02_15_04_05_000"

Playground example

答案1

得分: 3

要删除毫秒前的点号,可以使用strings.Replace()方法。以下是相同逻辑的代码:

package main

import (
	"fmt"
	"strings"
	"time"
)

func main() {

	format := "2006_01_02_15_04_05_.000"
	fmt.Println(time.Now().Format(format))
	fmt.Println(strings.Replace(time.Now().Format(format), "_.", "_", 1))

}

输出结果:

2009_11_10_23_00_00_.000
2009_11_10_23_00_00_000

请注意,这只是代码的翻译部分,不包括任何其他内容。

英文:

To remove the dot before milliseconds use strings.Replace() method .Please find the code below with the same logic .

package main

import (
	"fmt"
	"strings"
	"time"
)

func main() {

	format := "2006_01_02_15_04_05_.000"
	fmt.Println(time.Now().Format(format))
	fmt.Println(strings.Replace(time.Now().Format(format), "_.", "_", 1))

}

Output:

2009_11_10_23_00_00_.000
2009_11_10_23_00_00_000

答案2

得分: 2

你可以使用strings.Replace()函数来从格式化的时间字符串中移除.

strings.Replace(time.Now().Format(format), `.`, ``, 1)

在这里运行

英文:

Simply use strings.Replace() to remove . from your formatted time string.

strings.Replace(time.Now().Format(format), `.`, ``, 1)

run here

答案3

得分: 1

do函数高效地从格式中移除了.(点)。

根据格式的约定,小数点后面跟着一个或多个零表示小数秒,打印到给定的小数位数。因此,使用.000而不是.999是安全的。

do函数进行了一些合理性检查,以确保格式是有效的。

package main

import (
	"errors"
	"fmt"
	"os"
	"strings"
	"time"
)

// do函数安全地将点(.)替换为下划线(_)
func do(t string) (string, error) {
	// size是字符串的预期长度
	const size = 24
	if tLen := len(t); tLen != size {
		return "",
			fmt.Errorf(
				"无效的格式大小:期望 %d,得到 %d", size, tLen,
			)
	}

	if t[size-4] != '.' {
		return "", errors.New("无效的格式")
	}

	// 使用strings.Builder进行字符串拼接
	sb := strings.Builder{}
	sb.Grow(size)
	// 对于: "2006_01_02_15_04_05_.000"
	// 将"2006_01_02_15_04_05_"和"000"连接起来
	sb.WriteString(t[0 : size-4])
	sb.WriteString(t[size-3 : size])

	return sb.String(), nil
}

func main() {
	format := "2006_01_02_15_04_05_.000"
	t, err := do(time.Now().Format(format))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	fmt.Println(t)
}
英文:

do function efficiently removes the . (dot) from the format.

As per the format's convention, a decimal point followed by one or more zeros represents a fractional second, printed to the given number of decimal places.
Hence, using the same is safe (eg. .000 instead of .999).

The do function does some sanity check so that format is not valid.

package main

import (
	"errors"
	"fmt"
	"os"
	"strings"
	"time"
)

// do replaces the dot (.) with underscore (_) safely
func do(t string) (string, error) {
	// size is the expected length of the string
	const size = 24
	if tLen := len(t); tLen != size {
		return "",
			fmt.Errorf(
				"invalid format size: expected %d, got %d", size, tLen,
			)
	}

	if t[size-4] != '.' {
		return "", errors.New("invalid format")
	}

	// Use strings.Builder for concatenation
	sb := strings.Builder{}
	sb.Grow(size)
	// For: "2006_01_02_15_04_05_.000"
	// Join "2006_01_02_15_04_05_" and "000"
	sb.WriteString(t[0 : size-4])
	sb.WriteString(t[size-3 : size])

	return sb.String(), nil
}

func main() {
	format := "2006_01_02_15_04_05_.000"
	t, err := do(time.Now().Format(format))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	fmt.Println(t)
}

huangapple
  • 本文由 发表于 2021年6月18日 18:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68033580.html
匿名

发表评论

匿名网友

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

确定