1
0
mirror of https://github.com/thebaer/tildes.git synced 2018-07-20 07:15:21 +00:00

Show user's local time

This commit is contained in:
Matt Baer 2015-02-11 00:09:10 -05:00
parent feb0786090
commit dca3c0131a
2 changed files with 28 additions and 1 deletions

View File

@ -8,10 +8,15 @@
<style type="text/css"> <style type="text/css">
div.user { div.user {
margin: 0; margin: 0;
width: 50em;
} }
div.user h2 { div.user h2 {
margin: 0; margin: 0;
} }
span.time {
float: right;
color: #999;
}
</style> </style>
</head> </head>
@ -23,7 +28,7 @@
{{range .Users}} {{range .Users}}
<div class="user"> <div class="user">
<h2><a href="/~{{.Name}}/">~{{.Name}}</a></h2> <span class="location">{{Location .Region .Country}}</span> <h2><a href="/~{{.Name}}/">~{{.Name}}</a></h2> <span class="location">{{Location .Region .Country}} </span> <span class="time">{{.CurrentTime}}</span>
</div> </div>
{{end}} {{end}}

View File

@ -37,6 +37,7 @@ type User struct {
IP string IP string
Region string Region string
Country string Country string
CurrentTime string
} }
var ipRegex = regexp.MustCompile("(([0-9]{1,3}[.-]){3}[0-9]{1,3})") var ipRegex = regexp.MustCompile("(([0-9]{1,3}[.-]){3}[0-9]{1,3})")
@ -85,6 +86,26 @@ func who() []User {
return users return users
} }
func getTimeInZone(tz string) string {
cmd := exec.Command("date", "+%A %H:%M")
cmd.Env = append(cmd.Env, fmt.Sprintf("TZ=%s", tz))
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println(err)
}
if err := cmd.Start(); err != nil {
fmt.Println(err)
}
r := bufio.NewReader(stdout)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
return scanner.Text()
}
return ""
}
func getGeo(u *User) { func getGeo(u *User) {
fmt.Printf("Fetching %s location...\n", u.Name) fmt.Printf("Fetching %s location...\n", u.Name)
@ -109,6 +130,7 @@ func getGeo(u *User) {
region := dat["region_name"].(string) region := dat["region_name"].(string)
country := dat["country_name"].(string) country := dat["country_name"].(string)
u.CurrentTime = getTimeInZone(dat["time_zone"].(string))
u.Region = region u.Region = region
u.Country = country u.Country = country
} }