/
index.html
153 lines (105 loc) · 6.04 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="theme-color" content="#515151">
<meta name="generator" content="Hugo 0.14" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title> The crazy date/time formatting in Go · Manuel Ohlendorf </title>
<link href='http://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700|Abril+Fatface">
<link rel="stylesheet" href="http://manuel-ohlendorf.de/css/poole.css">
<link rel="stylesheet" href="http://manuel-ohlendorf.de//css/syntax.css">
<link rel="stylesheet" href="http://manuel-ohlendorf.de//css/hyde.css">
<link rel="stylesheet" href="http://manuel-ohlendorf.de//css/styles.css">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="/favicon.ico">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1937462-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<link href="" rel="alternate" type="application/rss+xml" title="Manuel Ohlendorf" />
</head>
<body class="theme-dark">
<div class="sidebar">
<div class="container">
<div class="sidebar-about">
<h1 class="brand"><a href="/">Manuel Ohlendorf</a></h1>
<p class="lead">
Things from and about me
</p>
</div>
<ul class="sidebar-nav">
<li><a href="http://manuel-ohlendorf.de/blog">Blog</a></li>
<li><a href="/cv/"> Curriculum Vitae </a></li>
<li><a href="http://manuel-ohlendorf.de/archive">Archive</a></li>
</ul>
<a href="https://twitter.com/mohlendo"><i class="fa fa-twitter-square"></i></a>
<a href="https://github.com/mohlendo"><i class="fa fa-github-square"></i></a>
<a href="https://www.xing.com/profile/Manuel_Ohlendorf"><i class="fa fa-xing"></i></a>
<p class="footnote">© 2015 Manuel Ohlendorf. All rights reserved.</p>
</div>
</div>
<div class="content container">
<div class="post">
<h1>The crazy date/time formatting in Go</h1>
<div class="tags">
<ul>
<li><a href="/tags/go">Go</a> </li>
<li><a href="/tags/time">Time</a> </li>
</ul>
</div>
<span class="post-date">Tue, Sep 29, 2015 · 2 minute read</span>
<p>Formatting and parsing date/time strings is a very common use-case in programming. A lot of the programming languages I worked with take the same approach for that: Using the Unicode time pattern syntax <a href="http://www.unicode.org/reports/tr35/#Date_Format_Patterns">UTS #35</a>. For me it is always hard to remember the details, especially when it comes to the upper and lower case codes. A typical format string in Java for an english date would be</p>
<pre><code> EEE, MM/dd hh:mm:ssa yyyy ZZZ
</code></pre>
<p>Fairly simple, but you have to remember that the month part is always upper case, the minute part is always lower case, 12-hour time is the lower case <code>hh</code>, <code>a</code> is the am/pm marker, etc. There a several more pattern letters if you want to get more detailed on the time format.</p>
<p>Go takes a completely new road to date/time formatting. Instead of codes it uses a standard time for describing the desired date/time format. The same date format as above written in Go is:</p>
<pre><code> Mon, 01/02 03:04:05PM 2006 -0700
</code></pre>
<p>As you can see this is a special time counting from one for the month part to six for the year part and seven for the time zone (which is MST). So there a only three things to recall: it starts with the month, the hour part is PM (15 o’clock in 24-hour time) and the day of the week is Monday - instead of all all those abstract time pattern letters.</p>
<p>Now let’s parse and format a simple date string from an english date format to the german date format:</p>
<pre><code class="language-Go">package main
import (
"fmt"
"time"
)
func main() {
// my birthday
value := "04/19/1979"
// the format of the value
parseFormat := "01/02/2006"
// parse the string into a time object
time, _ := time.Parse(parseFormat, value)
// the german output format
printFormat := "02.01.2006"
// and print it with the desired format
fmt.Println(time.Format(printFormat))
}
// => "19.04.1979"
</code></pre>
<p>I like this new approach of Go. Writing the standard time down the way yours looks may be strange in the first place, but it’s easy to recall, and it also happens to match the form of your time string, syntactically.</p>
</div>
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'manuelohlendorf';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</div>
</body>
</html>