English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statements

Golang Functions & Methods

Golang Structs

Golang Slices & Arrays

Golang Strings (String)

Golang Pointers

Golang Interfaces

Golang Concurrency

Golang Exceptions (Error)

Autres éléments divers en Golang

Time (date et heure) du langage Go

Go offre un bon soutien pour les opérations temporelles. L'époque Unix est utilisée comme référence pour les opérations temporelles.

Nous pouvons utiliser la méthode Date fournie par le paquet time pour construire l'objet temps. Ce paquet contient des méthodes telles que year(), month(), day(), location() et autres.

Nous utilisons l'objet temps pour appeler ces méthodes.

Exemple de temps Go

package main
import "fmt"
import "time"
func main() {
	p := fmt.Println
	present := time.Now()//l'heure actuelle
	p(present)
	DOB := time.Date(1993, 02, 28, 9,04,39,213 ,time.Local)
	fmt.Println(DOB)
	fmt.Println(DOB.Year())
	fmt.Println(DOB.Month())
	fmt.Println(DOB.Day())
	fmt.Println(DOB.Hour())
	fmt.Println(DOB.Minute())
	fmt.Println(DOB.Second())
	fmt.Println(DOB.Nanosecond())
	fmt.Println(DOB.Location())
	fmt.Println(DOB.Weekday())
	fmt.Println(DOB.Before(present))
	fmt.Println(DOB.After(present))
	fmt.Println(DOB.After(present))
	fmt.Println(DOB.Equal(present))
	diff := present.Sub(DOB)
	fmt.Println(diff)
	fmt.Println(diff.Hours())
	fmt.Println(diff.Minutes())
	fmt.Println(diff.Seconds())
	fmt.Println(diff.Nanoseconds())
	fmt.Println(DOB.Add(diff))-fmt.Println(DOB.Add(diff))
}

Sortie :

2017-10-04 17:10:13.474931994 +053diff))+0 IST m=334969
1993-02-28 09:04:390.000213 +0530 IST
1993
.000000
28
9
4
39
213
Local
dimanche
true
false
false
215624h5m34.474931781s
215624.09290970326
1.2937445574582197e+07
7.762467344749318e+08
776246734474931781
2017-10-04 17:10:13.474931994 +0530 IST
1968-07-25 00:59:04.525068432 +0530 IST
Processus terminé avec le code de sortie 0

Exemple de temps Go2

package main
import (
	"fmt"
	"time"
)
func main() {
	present := time.Now()
	fmt.Println("Aujourd'hui : ", present.Format("Mon, Jan 2, 2006 à 3:04pm"))
	someTime := time.Date(2017, time.March, 30, 11, 30, 55, 123456, time.Local)
	// Utiliser time.Equal() pour comparer les temps
	sameTime := someTime.Equal(present)
	fmt.Println("someTime equals to now ? : ", sameTime)
	//Calculer la différence de temps entre aujourd'hui et avant
	diff := present.Sub(someTime)
	//Convertir la différence en jours
	days := int(diff.Hours()) / 24)
	fmt.Printf("30ème mars 2017 était %d jours avant hier 
", days)
}

Sortie :

Aujourd'hui : Mer, oct 4, 2017 à 5:15pm
someTime equals to now ? : false
30ème mars 2017 était 188 il y a quelques jours