From 630ed107ea7ea051bffe82b71bff7d28e03cb0e2 Mon Sep 17 00:00:00 2001 From: asahi Date: Thu, 16 Jan 2025 13:05:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBgolang=20time=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Golang/Golang Document.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Golang/Golang Document.md b/Golang/Golang Document.md index 937e589..3132146 100644 --- a/Golang/Golang Document.md +++ b/Golang/Golang Document.md @@ -55,6 +55,9 @@ - [JSONFormatter](#jsonformatter) - [设置TimeStampFormat格式](#设置timestampformat格式) - [自定义Formatter](#自定义formatter) + - [golang time](#golang-time) + - [get current time](#get-current-time) + - [monotonic clock](#monotonic-clock) - [syntax](#syntax) - [iota](#iota) @@ -1143,6 +1146,25 @@ func main() { } ``` +## golang time +### get current time +在golang中,可以通过`time`package获取当前时间,该包中提供了许多date和time相关的方法,可以通过`time.Time`类型来表示特定的时间点。 + +当前时间可以通过`time.Now`方法来进行获取,示例如下: +```go +fmt.Println(time.Now()) +``` +其返回结果如下所示: +``` +2025-01-16 12:55:07.748015 +0800 CST m=+0.000000001 +``` +#### monotonic clock +上述返回结果中,`m=`部分代表monotonic clock,monotonic clock在拱廊内部使用,用于衡量时间差异。monotonic用于补偿`程序运行时对computer system clock的日期和时间造成的潜在修改`。 + +通过使用monotonic clock,如果在两次调用`time.Now`间隔5min的场景下,即使在调用的5分钟间隙内认为修改系统时间,两次调用`time.Now`返回值的差额仍然为5min。 + + + ## syntax ### iota `iota`关键字代表连续的整数变量,`0, 1, 2`,每当`const`关键字出现时,其重置为0