Home > Backend Development > Golang > How to List Available Time Zones in Go for HTML Templates?

How to List Available Time Zones in Go for HTML Templates?

Mary-Kate Olsen
Release: 2024-11-28 18:10:11
Original
533 people have browsed it

How to List Available Time Zones in Go for HTML Templates?

Listing Available Time Zones in Go for HTML Templates

In web applications, displaying a list of time zones and allowing users to select their preferred one is a common feature. In Go, the time.Format() function offers a convenient way to format time values according to specified time zones. However, retrieving a list of valid time zones can be somewhat tedious.

To obtain a comprehensive list of time zones in Go, you can traverse the system's time zone directories. The following code sample demonstrates how to achieve this:

package main

import (
    "fmt"
    "os"
    "strings"
)

var zoneDirs = []string{
    "/usr/share/zoneinfo/",
    "/usr/share/lib/zoneinfo/",
    "/usr/lib/locale/TZ/",
}

var zoneDir string

func main() {
    for _, zoneDir = range zoneDirs {
        ReadFile("")
    }
}

func ReadFile(path string) {
    files, _ := os.ReadDir(zoneDir + path)
    for _, f := range files {
        if f.Name() != strings.ToUpper(f.Name()[:1]) + f.Name()[1:] {
            continue
        }
        if f.IsDir() {
            ReadFile(path + "/" + f.Name())
        } else {
            fmt.Println((path + "/" + f.Name())[1:])
        }
    }
}
Copy after login

Once you have this list, you can populate an HTML template with available time zones using a select element, allowing users to conveniently choose their preferred time zone.

The above is the detailed content of How to List Available Time Zones in Go for HTML Templates?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template