← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 4 of 10

time Package का उपयोग

time package Go को clock पढ़ने, यह मापने कि चीज़ों में कितना समय लगता है, और दुनिया में आपका program कहीं भी चले एक जैसे तरीके से dates के साथ काम करने देता है।
Syntax
go
now := time.Now()
d := time.Duration(n) * time.Second
t.Format("2006-01-02")
time.Sleep(d)

Getting the Current Time

time.Now() current moment को represent करती एक time.Time value return करता है, जो एक पूरी date, time, और time zone रखती है, और formatting व arithmetic को support करती है।

उदाहरण: Getting the Current Time

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create now and infer the type
	now := time.Now()
	// Print the values followed by a newline
	fmt.Println("current year:", now.Year())
}

Measuring Elapsed Time

time.Since(start) एक time.Duration return करता है जो बताता है कि एक recorded start time के बाद से कितना समय बीता -- किसी operation में कितना समय लगता है यह measure करने का standard तरीका।

उदाहरण: Measuring Elapsed Time

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create start and infer the type
	start := time.Now()
	// Short declaration: create sum and infer the type
	sum := 0
	// Loop: repeat while the condition holds
	for i := 0; i < 1000000; i++ {
		sum += i
	}
	elapsed := time.Since(start)
	// Print the values followed by a newline
	fmt.Println("sum:", sum, "took at least 0ns:", elapsed >= 0)
}

Working with Durations

एक time.Duration समय के एक span को represent करता है और आमतौर पर time.Second या time.Millisecond जैसे constants से बनाया जाता है, जो sleeps और timeouts जैसे duration-based code को self-documenting बनाता है।

Note: स्पष्टता के लिए हमेशा raw integers के बजाय named constants (time.Second, आदि) से durations बनाएं।

उदाहरण: Working with Durations

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create d and infer the type
	d := 2 * time.Second
	// Print the values followed by a newline
	fmt.Println("duration:", d)
	time.Sleep(1 * time.Millisecond)
	// Print the values followed by a newline
	fmt.Println("slept briefly")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. internal monotonic clock readings के कारण Sub/Before/After/Equal ज़्यादा सही होने के बावजूद दो time.Time values को == से compare करना।
  2. यह भूल जाना कि time.Sleep पूरे goroutine को रोकता है, यह बाकी जो भी कर रहा हो उसे block करते हुए।
  3. स्पष्टता के लिए time.Second जैसे constants उपयोग करने के बजाय किसी time.Duration की numeric value (nanoseconds) को एक human unit के साथ confuse करना।
चैप्टर सारांश
  • time.Now current local time को एक time.Time value के रूप में return करता है।
  • time.Duration समय की एक लंबाई को represent करता है, time.Second या time.Millisecond जैसे constants से express किया गया।
  • time.Since यह compute करता है कि किसी दिए time.Time के बाद से कितना समय बीता।
  • time.Sleep current goroutine को एक दी गई duration के लिए रोकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.