當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang time.FixedZone()用法及代碼示例

在Go語言中,時間包提供了確定和查看時間的函數。 Go語言中的FixedZone()函數用於查找始終使用區域和偏移的規定名稱(即UTC以東的秒數)的位置。此外,此函數在時間包下定義。在這裏,您需要導入“time”包才能使用這些函數。

用法:

func FixedZone(name string, offset int) *Location

在這裏,“name”是區域的名稱,offset保留一個整數,* Location是指向該位置的指針。其中“Location”構成了使用中的時間偏移集。

返回值:它返回不斷使用指定的區域名稱和偏移量的位置。

範例1:



// Golang program to illustrate the usage of 
// FixedZone() function 
  
// Including main package 
package main 
  
// Importing fmt and time 
import ( 
    "fmt"
    "time"
) 
  
// Calling main 
func main() { 
  
    // Calling FixedZone method 
    // with its parameter 
    location:= time.FixedZone("UTC-7", -7*50*50) 
  
    // Prints location 
    fmt.Println(location) 
}

輸出:

UTC-7

在此,返回指定的位置。

範例2:

// Golang program to illustrate the usage of 
// FixedZone() function 
  
// Including main package 
package main 
  
// Importing fmt and time 
import ( 
    "fmt"
    "time"
) 
  
// Calling main 
func main() { 
  
    // Calling FixedZone 
    // method with its parameters 
    location:= time.FixedZone("UTC-6", -6*40*40) 
  
    // Calling Date method  
    // with all its parameters 
    // that is date, time, and location 
    tm := time.Date(2020, time.April, 6,  
                 9, 55, 06, 0, location) 
  
    // Prints date,  
    // time and location 
    fmt.Println(tm) 
}

輸出:

2020-04-06 09:55:06 -0240 UTC-6

在這裏,首先調用FixedZone()方法,然後調用Date()方法及其參數,即日期,時間和位置,然後將所有這些參數作為輸出返回。




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 time.FixedZone() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。