Skip to content

Instantly share code, notes, and snippets.

@isomorphisms
Created October 11, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isomorphisms/909e8de5c55e6087148b53d64b62a6b3 to your computer and use it in GitHub Desktop.
Save isomorphisms/909e8de5c55e6087148b53d64b62a6b3 to your computer and use it in GitHub Desktop.
How to print out the time in golang.
package main
import (
"log"
"fmt"
"net/http"
"time"
)
type placeError struct {
where string
}
func (e *placeError) Error() string { return fmt.Sprintf("I have no idea where %s is.", e.where) }
// error handling should APPEND all the places I can't find and then log this to the user.
type timeFormatter struct {
format string
}
func (timeUnFormatted *timeFormatter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
marengo,_ := time.LoadLocation("America/Indiana/Marengo")
now := time.Now().In(marengo).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Marengo, Indiana."))
w.Write( []byte("\n\n") )
dublin,_ := time.LoadLocation("Europe/Dublin")
now = time.Now().In(dublin).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Dublin, Ireland."))
w.Write( []byte("\n\n") )
// kinshasa,_ := time.LoadLocation("Africa/Kinshasa")
// now = time.Now().In(kinshasa).Format(timeUnFormatted.format)
// w.Write([]byte("It's now " + now + " in Kinshasa, Zaire."))
//
tehran,_ := time.LoadLocation("Asia/Tehran")
now = time.Now().In(tehran).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Tehran, Iran."))
w.Write( []byte("\n\n") )
xinjiang, _ := time.LoadLocation("Asia/Urumqi")
now = time.Now().In(xinjiang).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Ürümqi, Xinjiang-Uighur Special Autonomous Region."))
w.Write( []byte("\n\n") )
//canberra, _ := time.LoadLocation("Australia/Melbourne")
//now = time.Now().In(canberra).Format(timeUnFormatted.format)
//w.Write([]byte("It's now " + now + " in Canberra, Victoria, Australia."))
//w.Write( []byte("\n\n") )
tasmania, _ := time.LoadLocation("Australia/Hobart")
now = time.Now().In(tasmania).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Tasmania."))
w.Write( []byte("\n\n") )
tokyo, _ := time.LoadLocation("Asia/Tokyo")
now = time.Now().In(tokyo).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Tokyo."))
w.Write( []byte("\n\n") )
chiangmai, _ := time.LoadLocation("Asia/Bangkok")
now = time.Now().In(chiangmai).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Chiang Mai."))
w.Write( []byte("\n\n") )
burma, _ := time.LoadLocation("Asia/Yangon")
now = time.Now().In(burma).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Burma."))
w.Write( []byte("\n\n") )
timor, _ := time.LoadLocation("Asia/Dili")
now = time.Now().In(timor).Format(timeUnFormatted.format)
w.Write([]byte("It's now " + now + " in Timor Leste."))
w.Write( []byte("\n\n") )
}
func main() {
router := http.NewServeMux()
formatsTimeInRFC1123 := &timeFormatter{format: time.RFC1123}
router.Handle("/time", formatsTimeInRFC1123)
log.Println("Listening...")
http.ListenAndServe(":3111", router)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment