Created
October 11, 2019 23:17
-
-
Save isomorphisms/d8c467e62e20459065d2671bd9f0c109 to your computer and use it in GitHub Desktop.
how to print out time zones for various places in golang.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
"fmt" | |
) | |
func check(e error) { if( e!= nil) { panic(e) } } | |
func main() { | |
pr := fmt.Println | |
t := time.Now() | |
ny,err := time.LoadLocation("America/New_York") | |
check(err) | |
posey,err := time.LoadLocation("America/Indiana/Indianapolis") | |
check(err) | |
laporte,err := time.LoadLocation("America/Chicago") | |
check(err) | |
tell_city,err := time.LoadLocation("America/Indiana/Tell_City") | |
check(err) | |
pr("Location: ", t.Location(), "\t", "Time: ", t ) | |
pr("Location: ", ny, "\t", "Time: ", time.Now().In(ny) ) | |
pr("Location: ", posey, "\t", "Time: ", time.Now().In(posey) ) | |
pr("Location: ", laporte, "\t", "Time: ", time.Now().In(laporte) ) | |
pr("Location: ", tell_city, "\t", "Time: ", time.Now().In(tell_city) ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment