TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Golang Time: Now, Parse and Duration

This Go example uses the time package to get and modify times. It uses Now, Year, Month and Day.

Time. Programs often handle times and dates.

They read strings in from files, parse them, test them for validity. Times are changed.

With the time package, we access many usable funcs on the Time struct. These methods are reusable and tested. This is a clear advantage.

Now example. Let us start with this simple example. We import the "time" package in the import block at the top. We invoke the Now method from the time package. It returns a struct.

Year: With year we get an int equal to the year field in the Time struct. Here it returns 2015.

Month: This returns the month of the time struct as an int. When we use Println, its String method displays it in a readable way.

Day: This is the day field of the Time struct—not the total number of days in the time.

Based on:

Golang 1.4

Golang program that uses time, Year, Month, Day

package main

import (
    "fmt"
    "time"
)

func main() {
    // Get the current time.
    t := time.Now()
    fmt.Println(t)

    // Print year, month and day of the current time.
    fmt.Println(t.Year())
    fmt.Println(t.Month())
    fmt.Println(t.Day())
}

Output

2015-01-28 02:33:21.0351194 -0800 PST
2015
January
28

Parse. With this func we convert a string to a Time struct. Parse receives two strings: a form string and the value we are parsing.

Return: Parse() returns two things: the Time struct and an error (if any). We can use the blank identifier to ignore the error.

Parse parses a formatted string and returns the time value it represents. The layout defines the format by showing how the reference time, defined to be Mon Jan 2 15:04:05 -0700 MST 2006 would be interpreted if it were the value.

Time: golang.org

Golang program that uses time, Parse

package main

import (
    "fmt"
    "time"
)

func main() {
    // This is the value we are trying to parse.
    value := "January 28, 2015"

    // The form must be January 2,2006.
    form := "January 2, 2006"

    // Parse the string according to the form.
    t, _ := time.Parse(form, value)
    fmt.Println(t)
}

Output

2015-01-28 00:00:00 +0000 UTC

Sub, Duration. This example combines many time concepts. It uses Now() twice to measure the time required to run a piece of code. Then it uses Sub() to get a duration.

Seconds: This is a method on the duration that is returned by Sub. It is a floating-point number.

Minutes: This is like seconds, but divided by 60. It is mainly a convenience method.

Golang program that uses Sub, Duration, Seconds, Minutes

package main

import (
    "fmt"
    "time"
)

func main() {

    t0 := time.Now()

    // Do a slow operation.
    count := 0
    for i := 0; i < 100000; i++ {
	for x := 0; x < i; x++ {
	    count++
	}
    }

    t1 := time.Now()

    // Display result.
    fmt.Println(count)

    // Get duration.
    d := t1.Sub(t0)
    fmt.Println("Duration", d)
    // Get seconds from duration.
    s := d.Seconds()
    fmt.Println("Seconds", s)
    // Get minutes from duration.
    m := d.Minutes()
    fmt.Println("Minutes", m)
}

Output

4999950000
Duration 1.8052613s
Seconds 1.8052613000000002
Minutes 0.030087688333333334

Benchmarks. The time module can be used to take benchmarks. We use Sub to get a difference (a duration) between two times returned by Now calls. An example is present on the map page.

Map

With the Time struct, we gain a way to represent a point in time (a Date). And with the methods in time, we can manipulate it. We cannot go back in time, but we can represent it.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf