Last active
March 4, 2016 07:17
-
-
Save rickt/650aeb9635717eca4e35 to your computer and use it in GitHub Desktop.
simple appengine go app that prints out the current time in Japan, Los Angeles & London/UTC
This file contains hidden or 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
// http://japan-timebot-simple.appspot.com | |
package japantime | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
) | |
// constants | |
const ( | |
shortformat = "3:04pm" | |
) | |
// types | |
type times struct { | |
jpn time.Time | |
lax time.Time | |
utc time.Time | |
} | |
func init() { | |
// setup the http handlers | |
http.HandleFunc("/", handler_redirect) | |
http.HandleFunc("/time", handler_time) | |
} | |
// get the current time in JPN, LAX, UTC and return them in a struct of 3x t.Time | |
func getTime() times { | |
var tzu times | |
t := time.Now() | |
// locations | |
loc_lax, _ := time.LoadLocation("America/Los_Angeles") | |
loc_jpn, _ := time.LoadLocation("Japan") | |
loc_utc, _ := time.LoadLocation("UTC") | |
// times | |
tzu.lax = t.In(loc_lax) | |
tzu.jpn = t.In(loc_jpn) | |
tzu.utc = t.In(loc_utc) | |
return tzu | |
} | |
// redirect | |
func handler_redirect(w http.ResponseWriter, r *http.Request) { | |
http.Redirect(w, r, "http://japan-timebot-simple.appspot.com/time", 301) | |
} | |
// print the current time in Japan, LA, London/UTC | |
func handler_time(w http.ResponseWriter, r *http.Request) { | |
var mytimes times | |
mytimes = getTime() | |
fmt.Fprintf(w, mytimes.jpn.Format(shortformat)+" in Japan ("+mytimes.lax.Format(shortformat)+" in Los Angeles, "+mytimes.utc.Format(shortformat)+" in London/UTC)\n") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment