timezones.go 688 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Output:
  2. // Eastern Standard time
  3. // Universal Greenwich time
  4. // Central Standard time
  5. package main
  6. import "fmt"
  7. type TZ int
  8. const (
  9. HOUR TZ = 60 * 60
  10. UTC TZ = 0 * HOUR
  11. EST TZ = -5 * HOUR
  12. CST TZ = -6 * HOUR
  13. )
  14. var timeZones = map[TZ]string{
  15. UTC: "Universal Greenwich time",
  16. EST: "Eastern Standard time",
  17. CST: "Central Standard time"}
  18. func (tz TZ) String() string { // Method on TZ (not ptr)
  19. if zone, ok := timeZones[tz]; ok {
  20. return zone
  21. }
  22. return ""
  23. }
  24. func main() {
  25. fmt.Println(EST) // Print* knows about method String() of type TZ
  26. fmt.Println(0 * HOUR)
  27. fmt.Println(-6 * HOUR)
  28. }
  29. /* Output:
  30. Eastern Standard time
  31. Universal Greenwich time
  32. Central Standard time
  33. */