time.Time Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/time-time/ Sat, 01 Feb 2020 06:38:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 time.Time Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/time-time/ 32 32 159787465 Convert Unix TimeStamp to time.Time in Go (Golang) https://vikasboss.github.io/parse-unix-timestamp-time-go/ https://vikasboss.github.io/parse-unix-timestamp-time-go/#respond Sat, 01 Feb 2020 05:25:47 +0000 https://vikasboss.github.io/?p=1282 Time can be represented in GO in both time.Time and Unix Timestamp format. The Unix Timestamp also known as Epoch Time is the number of seconds elapsed since 00:00:00 UTC on 1 January...

The post Convert Unix TimeStamp to time.Time in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Time can be represented in GO in both time.Time and Unix Timestamp format. The Unix Timestamp also known as Epoch Time is the number of seconds elapsed since 00:00:00 UTC on 1 January 1970. This time is also known as the Unix epoch. Below code shows the conversion of

  • time.Time to Unix Timestamp
  • Unix Timestamp to time.Time
package main

import (
    "fmt"
    "time"
)

func main() {
    tNow := time.Now()

    //time.Time to Unix Timestamp
    tUnix := tNow.Unix()
    fmt.Printf("timeUnix %d\n", tUnix)

    //Unix Timestamp to time.Time
    timeT := time.Unix(tUnix, 0)
    fmt.Printf("time.Time: %s\n", timeT)
}

Output:

timeUnix 1257894000
time.Time: 2009-11-10 23:00:00 +0000 UTC

The post Convert Unix TimeStamp to time.Time in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/parse-unix-timestamp-time-go/feed/ 0 1282
Convert time between different timezones in Go (Golang) https://vikasboss.github.io/convert-time-timezones-go/ https://vikasboss.github.io/convert-time-timezones-go/#respond Fri, 31 Jan 2020 18:24:50 +0000 https://vikasboss.github.io/?p=1266 Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location...

The post Convert time between different timezones in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location value associated with that time changes. The location associated with the time.Time object only has a presentation or display logic.

The In function can be used to change the location associated with a particular time.Time object. Whenever the In function is called on any time.Time object (say t) then,

  • A copy of t is created representing the same time instant.
  • t’s location is set to the location passed to In function for display purposes
  • t is returned back

Let’s see the below-working code which can be used to change the location value associated with a particular time.

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()

    loc, _ := time.LoadLocation("UTC")
    fmt.Printf("UTC Time: %s\n", now.In(loc))
   
    loc, _ = time.LoadLocation("Europe/Berlin")
    fmt.Printf("Berlin Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("America/New_York")
    fmt.Printf("New York Time: %s\n", now.In(loc))

    loc, _ = time.LoadLocation("Asia/Dubai")
    fmt.Printf("Dubai Time: %s\n", now.In(loc))
}

Output:

UTC Time: 2020-01-31 18:09:41.705858 +0000 UTC
Berlin Time: 2020-01-31 19:09:41.705858 +0100 CET
New York Time: 2020-01-31 13:09:41.705858 -0500 EST
Dubai Time: 2020-01-31 22:09:41.705858 +0400 +04

The post Convert time between different timezones in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/convert-time-timezones-go/feed/ 0 1266
Time Conversion in Go (Golang) https://vikasboss.github.io/time-conversion-in-golang/ https://vikasboss.github.io/time-conversion-in-golang/#respond Mon, 27 Jan 2020 18:19:03 +0000 https://vikasboss.github.io/?p=1231 Overview Time can be represented in GO in below 5 formats: time.Time object Unix Time (Also known as Epoch Time) – It is the number of seconds elapsed since 00:00:00 UTC on 1...

The post Time Conversion in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

Time can be represented in GO in below 5 formats:

  • time.Time object
  • Unix Time (Also known as Epoch Time) – It is the number of seconds elapsed since 00:00:00 UTC on 1 January 1970. This time is also known as the Unix epoch
  • Unix Nano –  It is the number of nanoseconds elapsed since 00:00:00 UTC on 1 January 1970
  • Unix MilliSecond – It is the number of milliseconds elapsed since 00:00:00 UTC on 1 January 1970
  • Unix MicroSecond – It is the number of microseconds elapsed since 00:00:00 UTC on 1 January 1970

Conversions

time.Time To Others

Let:

timeT := time.Now()

Time Unix

tUnix := timeT.Unix()

Time Unix Millisecond

tUnixMilli := int64(time.Nanosecond) * timeT.UnixNano() / int64(time.Millisecond)

Time Unix Microsecond

tUnixMicro := int64(time.Nanosecond) * timeT.UnixNano() / int64(time.Microsecond)

Time Unix Nanosecond

tUnixNano := timeT.UnixNano()

Full Working Code

package main

import (
    "fmt"
    "time"
)

func main() {
    timeT := time.Now() //It will return time.Time object with current timestamp
    fmt.Printf("time.Time %s\n", timeT)

    //Converstion to Time Unix also known as epoch time
    tUnix := timeT.Unix()
    fmt.Printf("timeUnix: %d\n", tUnix)

    //Conversion to Time Unix Millisecond
    tUnixMilli := int64(time.Nanosecond) * timeT.UnixNano() / int64(time.Millisecond)
    fmt.Printf("timeUnixMilli: %d\n", tUnixMilli)

    //Conversion to Time Unix Microsecond
    tUnixMicro := int64(time.Nanosecond) * timeT.UnixNano() / int64(time.Microsecond)
    fmt.Printf("timeUnixMicro: %d\n", tUnixMicro)

    //Conversion to Time Unix Nanosecond
    tUnixNano := timeT.UnixNano()
    fmt.Printf("timeUnixNano: %d\n", tUnixNano)
}

Output:

time.Time 2020-01-27 23:03:21.528106 +0530 IST m=+0.000191621
timeUnix: 1580146401
timeUnixMilli: 1580146401528
timeUnixMicro: 1580146401528106
timeUnixNano: 1580146401528106000

time Unix To Others

Let

tUnix := time.Now().Unix()

time.Time

tUnix := timeT.Unix()

Time Unix Millisecond

 tUnixMili := tUnix * int64(time.Microsecond)

Time Unix Microsecond

tUnixMicro := tUnix * int64(time.Millisecond)

Time Unix Nanosecond

tUnixNano := tUnix * int64(time.Second)

Full Working Code

package main

import (
    "fmt"
    "time"
)

func main() {
    tUnix := time.Now().Unix()
    fmt.Printf("timeUnix %d\n", tUnix)

    //Conversion to time.Time
    timeT := time.Unix(tUnix, 0)
    fmt.Printf("time.Time: %s\n", timeT)

    //Conversion to Time Unix Millisecond
    tUnixMili := tUnix * int64(time.Microsecond)
    fmt.Printf("timeUnixMilli: %d\n", tUnixMili)

    //Conversion to Time Unix Microsecond
    tUnixMicro := tUnix * int64(time.Millisecond)
    fmt.Printf("timeUnixMicro: %d\n", tUnixMicro)

    //Conversion to Time Unix Nanosecond
    tUnixNano := tUnix * int64(time.Second)
    fmt.Printf("timeUnixNano: %d\n", tUnixNano)
}

Output:

timeUnix 1580146705
time.Time: 2020-01-27 23:08:25 +0530 IST
timeUnixMilli: 1580146705000
timeUnixMicro: 1580146705000000
timeUnixNano: 1580146705000000000

time Unix MilliSecond To Others

Let

tUnixMilli := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)

time.Time

tUnix := tUnixMilli / int64(time.Microsecond)
tUnixNanoRemainder := (tUnixMilli % int64(time.Microsecond)) * int64(time.Millisecond)
timeT := time.Unix(tUnix, tUnixNanoRemainder)

Time Unix

tUnix = tUnixMilli / int64(time.Microsecond)

Time Unix Microsecond

tUnixMicro := tUnixMilli * int64(time.Microsecond)

Time Unix Nanosecond

tUnixNano := tUnixMilli * int64(time.Millisecond)

Full Working Code:

package main

import (
    "fmt"
    "time"
)

func main() {
    tUnixMilli := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)
    fmt.Printf("timeMilli %d\n", tUnixMilli)

    //Conversion to time.Time
    tUnix := tUnixMilli / int64(time.Microsecond)
    tUnixNanoRemainder := (tUnixMilli % int64(time.Microsecond)) * int64(time.Millisecond)
    timeT := time.Unix(tUnix, tUnixNanoRemainder)
    fmt.Printf("time.Time: %s\n", timeT)

    //Conversion to Time Unix
    tUnix = tUnixMilli / int64(time.Microsecond)
    fmt.Printf("timeUnix: %d\n", tUnix)

    //Conversion to Time Unix Microsecond
    tUnixMicro := tUnixMilli * int64(time.Microsecond)
    fmt.Printf("timeUnixMicro: %d\n", tUnixMicro)

    //Conversion to Time Unix Nanosecond
    tUnixNano := tUnixMilli * int64(time.Millisecond)
    fmt.Printf("timeUnixNano: %d\n", tUnixNano)
}

Output:

timeMilli 1580146846747
time.Time: 2020-01-27 23:10:46.747 +0530 IST
timeUnix: 1580146846
timeUnixMicro: 1580146846747000
timeUnixNano: 1580146846747000000

time Unix MicroSecond To Others

Let

tUnixMicro := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Microsecond)

time.Time

tUnix := tUnixMicro / int64(time.Millisecond)
tUnixNanoRemainder := (tUnixMicro % int64(time.Millisecond)) * int64(time.Microsecond)
timeT := time.Unix(tUnix, tUnixNanoRemainder)

Time Unix

tUnix = tUnixMicro / int64(time.Millisecond)

Time Unix Millisecond

tUnixMilli := tUnixMicro / int64(time.Microsecond)

Time Unix Nanosecond

tUnixNano := tUnixMicro * int64(time.Microsecond)

Full Working Code:

package main

import (
    "fmt"
    "time"
)

func main() {
    tUnixMicro := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Microsecond)
    fmt.Printf("tUnixMicro %d\n", tUnixMicro)

    //Conversion to time.Time
    tUnix := tUnixMicro / int64(time.Millisecond)
    tUnixNanoRemainder := (tUnixMicro % int64(time.Millisecond)) * int64(time.Microsecond)
    timeT := time.Unix(tUnix, tUnixNanoRemainder)
    fmt.Printf("time.Time: %s\n", timeT)

    //Converstion to Time Unix
    tUnix = tUnixMicro / int64(time.Millisecond)
    fmt.Printf("timeUnix: %d\n", tUnix)

    //Converstion to Time Unix Milli
    tUnixMilli := tUnixMicro / int64(time.Microsecond)
    fmt.Printf("timeUnixMill: %d\n", tUnixMilli)

    //Converstion to Time Unix Nano
    tUnixNano := tUnixMicro * int64(time.Microsecond)
    fmt.Printf("timeUnixNano: %d\n", tUnixNano)
}

Output:

tUnixMicro 1580147023233931
time.Time: 2020-01-27 23:13:43.233931 +0530 IST
timeUnix: 1580147023
timeUnixMill: 1580147023233
timeUnixNano: 1580147023233931000

time Unix NanoSecond To Others

Let

tUnixNano := time.Now().UnixNano()

time.Time

tUnix := tUnixNano / int64(time.Second)
tUnixNanoRemainder := (tUnixNano % int64(time.Second))
timeT := time.Unix(tUnix, tUnixNanoRemainder)

Time Unix

tUnix = tUnixNano / int64(time.Second)

Time Unix Millisecond

tUnixMilli := tUnixNano / int64(time.Millisecond)

Time Unix Microsecond

tUnixMicro := tUnixNano / int64(time.Microsecond)

Full Working Code

package main

import (
    "fmt"
    "time"
)

func main() {
    tUnixNano := time.Now().UnixNano()
    fmt.Printf("tUnixNano %d\n", tUnixNano)

    //Conversion to time.Time
    tUnix := tUnixNano / int64(time.Second)
    tUnixNanoRemainder := (tUnixNano % int64(time.Second))
    timeT := time.Unix(tUnix, tUnixNanoRemainder)
    fmt.Printf("time.Time: %s\n", timeT)

    //Conversion to Time Unix
    tUnix = tUnixNano / int64(time.Second)
    fmt.Printf("timeUnix: %d\n", tUnix)

    //Conversion to Time Unix Milli
    tUnixMilli := tUnixNano / int64(time.Millisecond)
    fmt.Printf("timeUnixMilli: %d\n", tUnixMilli)

    //Conversion to Time Unix Micro
    tUnixMicro := tUnixNano / int64(time.Microsecond)
    fmt.Printf("timeUnixMicro: %d\n", tUnixMicro)
}

Output:

tUnixNano 1580147160564568000
time.Time: 2020-01-27 23:16:00.564568 +0530 IST
timeUnix: 1580147160
timeUnixMill: 1580147160564
timeUnixMicro: 1580147160564568

The post Time Conversion in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/time-conversion-in-golang/feed/ 0 1231