Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Epoch

This Go code demonstrates working with the time package to obtain Unix timestamps at different precisions. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"time"
)

// The main function, where the execution of the program begins.
func main() {
	// Getting the current time.
	now := time.Now()
	fmt.Println(now)

	// Converting the current time to Unix timestamp at different precisions.

	// Unix timestamp in seconds.
	fmt.Println(now.Unix())

	// Unix timestamp in milliseconds.
	fmt.Println(now.UnixMilli())

	// Unix timestamp in nanoseconds.
	fmt.Println(now.UnixNano())

	// Converting Unix timestamp back to time.Time.

	// Creating a time.Time from Unix timestamp in seconds.
	fmt.Println(time.Unix(now.Unix(), 0))

	// Creating a time.Time from Unix timestamp in nanoseconds.
	fmt.Println(time.Unix(0, now.UnixNano()))
}

Output

2024-01-24 13:21:47.6288203 +0530 IST m=+0.001548401
1706082707
1706082707628
1706082707628820300
2024-01-24 13:21:47 +0530 IST
2024-01-24 13:21:47.6288203 +0530 IST

Explanation:

  1. Getting Current Time:

    • time.Now() returns the current time.

  2. Unix Timestamps:

    • Unix() returns the Unix timestamp in seconds.

    • UnixMilli() returns the Unix timestamp in milliseconds.

    • UnixNano() returns the Unix timestamp in nanoseconds.

  3. Converting Unix Timestamps Back to time.Time:

    • time.Unix() is used to create a time.Time instance from a Unix timestamp.

    • The first argument is the number of seconds, and the second argument is the number of nanoseconds.

This code showcases how to obtain and convert Unix timestamps using the time package in Go. It's useful when you need to work with timestamps in different precisions and convert them back to time.Time.

PreviousTimeNextTime Formatting / Parsing

Last updated 1 year ago

Was this helpful?