Home  >  Article  >  Backend Development  >  Is there a Go function to check if it\'s Daylight Saving Time (DST)?

Is there a Go function to check if it\'s Daylight Saving Time (DST)?

Linda Hamilton
Linda HamiltonOriginal
2024-11-02 06:12:02315browse

Is there a Go function to check if it's Daylight Saving Time (DST)?

Go function to check if it's Daylight Saving Time (DST)

In Ruby, there's the Time#dst? function, which returns true if the current time is in Daylight Saving Time (DST). Is there a similar function available in the Go standard library?

The answer is yes. In August 2021, Go 1.17 was released, which introduces the time.Time method IsDST.

Usage:

The syntax of IsDST is as follows:

<code class="go">func (t Time) IsDST() bool</code>
  • t is a time value representing the time to check.
  • IsDST returns true if the time t is in DST, and false otherwise.

Example:

Here's an example of how to use the IsDST method:

<code class="go">package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    if t.IsDST() {
        fmt.Println("It is daylight saving time")
    } else {
        fmt.Println("It is not daylight saving time")
    }
}</code>

The above is the detailed content of Is there a Go function to check if it\'s Daylight Saving Time (DST)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn