Introduction
Epoch time, also known as Unix time or POSIX time, is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds. Go provides functions to work with epoch time through its time
package. In this chapter, you will learn how to get the current epoch time, convert between epoch time and time.Time
, and perform common operations with epoch time in Go.
Getting the Current Epoch Time
You can get the current epoch time using the time.Now
function and the Unix
method.
Example: Getting the Current Epoch Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
epochTime := now.Unix()
fmt.Println("Current Epoch Time:", epochTime)
}
In this example, time.Now
returns the current local time, and Unix
converts it to epoch time in seconds.
Converting Epoch Time to time.Time
You can convert epoch time to a time.Time
object using the time.Unix
function.
Example: Converting Epoch Time to time.Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
epochTime := int64(1672531199) // Example epoch time
t := time.Unix(epochTime, 0)
fmt.Println("Converted Time:", t)
}
In this example, time.Unix
converts the epoch time (1672531199 seconds) to a time.Time
object.
Converting time.Time to Epoch Time
You can convert a time.Time
object to epoch time using the Unix
method.
Example: Converting time.Time
to Epoch Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2023, 6, 15, 14, 30, 0, 0, time.UTC)
epochTime := t.Unix()
fmt.Println("Epoch Time:", epochTime)
}
In this example, a time.Time
object is created using time.Date
, and the Unix
method converts it to epoch time.
Epoch Time with Milliseconds
To work with epoch time in milliseconds, use the UnixNano
method and convert the result to milliseconds.
Example: Getting Current Epoch Time in Milliseconds
Example:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
epochTimeMillis := now.UnixNano() / int64(time.Millisecond)
fmt.Println("Current Epoch Time in Milliseconds:", epochTimeMillis)
}
In this example, UnixNano
returns the current time in nanoseconds since epoch, and dividing by int64(time.Millisecond)
converts it to milliseconds.
Example: Converting Epoch Time in Milliseconds to time.Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
epochTimeMillis := int64(1672531199000) // Example epoch time in milliseconds
t := time.Unix(0, epochTimeMillis*int64(time.Millisecond))
fmt.Println("Converted Time:", t)
}
In this example, time.Unix
is used to convert epoch time in milliseconds to a time.Time
object.
Adding and Subtracting Time
You can add or subtract time from a time.Time
object using the Add
method and time.Duration
.
Example: Adding and Subtracting Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println("Current Time:", t)
// Adding 10 seconds
tPlus10 := t.Add(10 * time.Second)
fmt.Println("Time after 10 seconds:", tPlus10)
// Subtracting 5 minutes
tMinus5 := t.Add(-5 * time.Minute)
fmt.Println("Time before 5 minutes:", tMinus5)
}
In this example, the Add
method is used to add 10 seconds and subtract 5 minutes from the current time.
Parsing and Formatting Epoch Time
You can parse and format epoch time by first converting it to a time.Time
object.
Example: Parsing and Formatting Epoch Time
Example:
package main
import (
"fmt"
"time"
)
func main() {
epochTime := int64(1672531199) // Example epoch time
t := time.Unix(epochTime, 0)
formattedTime := t.Format("2006-01-02 15:04:05")
fmt.Println("Formatted Time:", formattedTime)
parsedTime, err := time.Parse("2006-01-02 15:04:05", formattedTime)
if err != nil {
fmt.Println("Error parsing time:", err)
return
}
fmt.Println("Parsed Time:", parsedTime)
}
In this example, epoch time is converted to a time.Time
object, formatted as a string, and then parsed back into a time.Time
object.
Conclusion
Working with epoch time in Go is straightforward using the time
package. You can easily convert between epoch time and time.Time
objects, handle epoch time in milliseconds, and perform common operations like adding and subtracting time. Understanding these basics will help you effectively manage time-related tasks in your Go programs.