| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"flag" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"strconv" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
format = "2006-01-02 15:04" // Format that will be used for times. |
| 15 |
outFormat = format + " MST" // Output format. |
| 16 |
tz = "Local" // String descriptor for timezone. |
| 17 |
fromLoc *time.Location = time.Local // Go time.Location for the named timezone. |
| 18 |
fromUnix bool // Input times are Unix timestamps. |
| 19 |
toLoc *time.Location = time.UTC // Go time.Location for output timezone. |
| 20 |
) |
| 21 |
|
| 22 |
func usage(w io.Writer) { |
| 23 |
fmt.Fprintf(w, `Usage: utc [-f format] [-o format] [-q] [-t] [-u] [-z zone] [time(s)...] |
| 24 |
utc -h | utc help |
| 25 |
|
| 26 |
|
| 27 |
utc converts times to UTC. If no arguments are provided, prints the |
| 28 |
current time in UTC. If the only argument provided is "-", utc reads |
| 29 |
newline-separated timestamps from standard input. If the argument is |
| 30 |
"help", it will print an extended help message with examples. If both |
| 31 |
the input and output timezones are the same (e.g., the local time zone |
| 32 |
is UTC), a warning message will be printed on standard error. This can |
| 33 |
be suppressed with the -q option. |
| 34 |
|
| 35 |
Flags: |
| 36 |
|
| 37 |
-f format Go timestamp format for input times. See the Go docs |
| 38 |
(e.g. https://golang.org/pkg/time/#pkg-constants) |
| 39 |
for an explanation of this format. |
| 40 |
|
| 41 |
Default value: %s |
| 42 |
|
| 43 |
-h Print this help message. |
| 44 |
|
| 45 |
-o format Go timestamp format for outputting times. |
| 46 |
It uses the same format as the '-f' argument. |
| 47 |
|
| 48 |
Default value: %s |
| 49 |
|
| 50 |
-q Suppress the timezone check warning message. |
| 51 |
|
| 52 |
-t Input times are Unix timestamps. Use with |
| 53 |
-u to convert the timestamp to the timezone |
| 54 |
specified by the -z option (which defaults |
| 55 |
to %s). |
| 56 |
|
| 57 |
-u Timestamps are in UTC format and should be |
| 58 |
converted to the timezone specified by the |
| 59 |
-z argument (which defaults to '%s'). Note |
| 60 |
that this isn't particularly useful with |
| 61 |
no arguments. |
| 62 |
|
| 63 |
-z zone Text form of the time zone; this can be in short |
| 64 |
time zone abbreviation (e.g. MST) or a |
| 65 |
location (e.g. America/Los_Angeles). This |
| 66 |
has no effect when printing the current |
| 67 |
time. |
| 68 |
|
| 69 |
Default value: %s |
| 70 |
`, format, outFormat, tz, tz, tz) |
| 71 |
} |
| 72 |
|
| 73 |
func usageExamples() { |
| 74 |
usage(os.Stdout) |
| 75 |
fmt.Println(` |
| 76 |
Examples (note that the examples are done in the America/Los_Angeles / |
| 77 |
PST8PDT time zone): |
| 78 |
|
| 79 |
+ Getting the current time in UTC: |
| 80 |
$ utc |
| 81 |
2016-06-14 14:30 PDT = 2016-06-14 21:30 UTC |
| 82 |
+ Converting a local timestamp to UTC: |
| 83 |
$ utc '2016-06-14 21:30' |
| 84 |
2016-06-14 21:30 PDT = 2016-06-15 04:30 UTC |
| 85 |
+ Converting a local EST timestamp to UTC (on a machine set to |
| 86 |
PST8PDT): |
| 87 |
$ utc -z EST '2016-06-14 21:30' |
| 88 |
2016-06-14 21:30 EST = 2016-06-15 02:30 UTC |
| 89 |
+ Converting timestamps in the form '14-06-2016 3:04PM': |
| 90 |
$ utc -f '02-01-2006 3:04PM' '14-06-2016 9:30PM' |
| 91 |
2016-06-14 21:30 PDT = 2016-06-15 04:30 UTC |
| 92 |
+ Converting timestamps from standard input: |
| 93 |
$ printf "2016-06-14 14:42\n2016-06-13 11:01" | utc - |
| 94 |
2016-06-14 14:42 PDT = 2016-06-14 21:42 UTC |
| 95 |
2016-06-13 11:01 PDT = 2016-06-13 18:01 UTC |
| 96 |
+ Converting a UTC timestamp to the local time zone: |
| 97 |
$ utc -u '2016-06-14 21:30' |
| 98 |
2016-06-14 21:30 UTC = 2016-06-14 14:30 PDT |
| 99 |
+ Converting a UTC timestamp to EST (on a machine set to |
| 100 |
PST8PDT): |
| 101 |
$ utc -u -z EST '2016-06-14 21:30' |
| 102 |
2016-06-14 21:30 UTC = 2016-06-14 16:30 EST |
| 103 |
+ Using a different output format: |
| 104 |
$ utc -o '2006-01-02T15:03:04-0700' '2016-06-14 21:30' |
| 105 |
2016-06-14T21:09:30-0700 = 2016-06-15T04:04:30+0000 |
| 106 |
+ Converting a Unix timestamp to a UTC time: |
| 107 |
$ utc -t 1466052938 |
| 108 |
2016-06-15 21:55 PDT = 2016-06-16 04:55 UTC |
| 109 |
+ Converting a Unix timestamp to local time: |
| 110 |
$ utc -t -u 1466052938 |
| 111 |
2016-06-16 04:55 UTC = 2016-06-15 21:55 PDT |
| 112 |
+ Converting a Unix timestamp to EST: |
| 113 |
$ utc -t -u -z EST 1466052938 |
| 114 |
2016-06-16 04:55 UTC = 2016-06-15 23:55 EST |
| 115 |
+ Example of the warning message when running utc on a machine |
| 116 |
where the local time zone is UTC: |
| 117 |
$ utc |
| 118 |
|
| 119 |
================================================================== |
| 120 |
Note: both input and output timezone offsets are the same --- this |
| 121 |
program may not do what you expect it to. |
| 122 |
|
| 123 |
(Converting from UTC (offset +0000) to UTC (offset +0000).) |
| 124 |
================================================================== |
| 125 |
2016-06-14 23:44 = 2016-06-14 23:44 |
| 126 |
+ Example of the warning message when running utc on a machine |
| 127 |
where the local time zone is GMT: |
| 128 |
$ utc |
| 129 |
|
| 130 |
================================================================== |
| 131 |
Note: both input and output timezone offsets are the same --- this |
| 132 |
program may not do what you expect it to. |
| 133 |
|
| 134 |
(Converting from GMT (offset +0000) to UTC (offset +0000).) |
| 135 |
================================================================== |
| 136 |
2016-06-14 23:46 = 2016-06-14 23:46 |
| 137 |
`) |
| 138 |
} |
| 139 |
|
| 140 |
func getZone(loc *time.Location) (string, int) { |
| 141 |
return time.Now().In(loc).Zone() |
| 142 |
} |
| 143 |
|
| 144 |
func checkZones(quiet bool) { |
| 145 |
if quiet { |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
toZone, toOff := getZone(toLoc) |
| 150 |
fromZone, fromOff := getZone(fromLoc) |
| 151 |
|
| 152 |
if toOff == fromOff { |
| 153 |
fmt.Fprintf(os.Stderr, ` |
| 154 |
================================================================== |
| 155 |
Note: both input and output timezone offsets are the same --- this |
| 156 |
program may not do what you expect it to. |
| 157 |
|
| 158 |
(Converting from %s (offset %+05d) to %s (offset %+05d).) |
| 159 |
================================================================== |
| 160 |
`, fromZone, fromOff, toZone, toOff) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func init() { |
| 165 |
var help, quiet, utc bool |
| 166 |
|
| 167 |
flag.Usage = func() { usage(os.Stderr) } |
| 168 |
flag.StringVar(&format, "f", format, "time format") |
| 169 |
flag.BoolVar(&help, "h", false, "print usage information") |
| 170 |
flag.StringVar(&outFormat, "o", outFormat, "output time format") |
| 171 |
flag.BoolVar(&quiet, "q", false, "suppress zone check warning") |
| 172 |
flag.BoolVar(&fromUnix, "t", false, "input times are Unix timestamps") |
| 173 |
flag.BoolVar(&utc, "u", false, "timestamps are in UTC format") |
| 174 |
flag.StringVar(&tz, "z", tz, "time zone to convert from; if blank, the local timezone is used") |
| 175 |
|
| 176 |
flag.Parse() |
| 177 |
|
| 178 |
if help { |
| 179 |
if !utc { |
| 180 |
checkZones(quiet) |
| 181 |
} |
| 182 |
usage(os.Stdout) |
| 183 |
os.Exit(0) |
| 184 |
} |
| 185 |
|
| 186 |
if utc { |
| 187 |
var err error |
| 188 |
toLoc, err = time.LoadLocation(tz) |
| 189 |
if err != nil { |
| 190 |
fmt.Fprintf(os.Stderr, "Malformed timezone %s: %s\n", tz, err) |
| 191 |
os.Exit(1) |
| 192 |
} |
| 193 |
|
| 194 |
fromLoc = time.UTC |
| 195 |
} else { |
| 196 |
var err error |
| 197 |
fromLoc, err = time.LoadLocation(tz) |
| 198 |
if err != nil { |
| 199 |
fmt.Fprintf(os.Stderr, "Malformed timezone %s: %s\n", tz, err) |
| 200 |
os.Exit(1) |
| 201 |
} |
| 202 |
|
| 203 |
if fromLoc == time.UTC { |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
toLoc = time.UTC |
| 208 |
} |
| 209 |
|
| 210 |
checkZones(quiet) |
| 211 |
} |
| 212 |
|
| 213 |
func showTime(t time.Time) { |
| 214 |
fmt.Printf("%s = %s\n", t.Format(outFormat), |
| 215 |
t.In(toLoc).Format(outFormat)) |
| 216 |
} |
| 217 |
|
| 218 |
func parseTime(in string) (time.Time, error) { |
| 219 |
if !fromUnix { |
| 220 |
return time.ParseInLocation(format, in, fromLoc) |
| 221 |
} |
| 222 |
|
| 223 |
var t time.Time |
| 224 |
n, err := strconv.ParseInt(in, 10, 64) |
| 225 |
if err != nil { |
| 226 |
return t, err |
| 227 |
} |
| 228 |
|
| 229 |
t = time.Unix(n, 0).In(fromLoc) |
| 230 |
return t, nil |
| 231 |
} |
| 232 |
|
| 233 |
func dumpTimes(times []string) bool { |
| 234 |
var errored bool |
| 235 |
|
| 236 |
for _, t := range times { |
| 237 |
u, err := parseTime(t) |
| 238 |
if err != nil { |
| 239 |
errored = true |
| 240 |
fmt.Fprintf(os.Stderr, "Malformed time %s: %s\n", t, err) |
| 241 |
continue |
| 242 |
} |
| 243 |
|
| 244 |
showTime(u) |
| 245 |
} |
| 246 |
|
| 247 |
return errored |
| 248 |
} |
| 249 |
|
| 250 |
func main() { |
| 251 |
var times []string |
| 252 |
n := flag.NArg() |
| 253 |
|
| 254 |
switch n { |
| 255 |
case 0: |
| 256 |
showTime(time.Now()) |
| 257 |
os.Exit(0) |
| 258 |
case 1: |
| 259 |
if flag.Arg(0) == "-" { |
| 260 |
s := bufio.NewScanner(os.Stdin) |
| 261 |
|
| 262 |
for s.Scan() { |
| 263 |
times = append(times, s.Text()) |
| 264 |
} |
| 265 |
} else if flag.Arg(0) == "help" { |
| 266 |
usageExamples() |
| 267 |
} else { |
| 268 |
times = flag.Args() |
| 269 |
} |
| 270 |
default: |
| 271 |
times = flag.Args() |
| 272 |
} |
| 273 |
|
| 274 |
if dumpTimes(times) { |
| 275 |
os.Exit(1) |
| 276 |
} |
| 277 |
} |