Skip to content

Commit

Permalink
correct time
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Ohlendorf committed Sep 29, 2015
1 parent c64af99 commit cababc9
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 64 deletions.
2 changes: 1 addition & 1 deletion archive/2015/index.html
Expand Up @@ -85,7 +85,7 @@ <h1>Entries</h1>
<ul class="posts">

<li>
<span><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Thu, Oct 29, 2015</time></span>
<span><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Tue, Sep 29, 2015</time></span>
</li>

<li>
Expand Down
8 changes: 4 additions & 4 deletions archive/2015/index.xml
Expand Up @@ -7,15 +7,15 @@
<language>en-us</language>
<author>Manuel Ohlendorf</author>

<updated>Thu, 29 Oct 2015 09:34:00 &#43;0200</updated>
<updated>Tue, 29 Sep 2015 09:34:00 CEST</updated>


<item>
<title>The crazy date/time formatting in Go</title>
<link>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Thu, 29 Oct 2015 09:34:00 &#43;0200</pubDate>
<link>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Tue, 29 Sep 2015 09:34:00 CEST</pubDate>
<author>Manuel Ohlendorf</author>
<guid>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</guid>
<guid>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</guid>
<description>&lt;p&gt;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 &lt;a href=&#34;http://www.unicode.org/reports/tr35/#Date_Format_Patterns&#34;&gt;UTS #35&lt;/a&gt;. 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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; EEE, MM/dd hh:mm:ssa yyyy ZZZ
Expand Down
2 changes: 1 addition & 1 deletion archive/index.html
Expand Up @@ -96,7 +96,7 @@ <h2><a href="2011">2011</a> 2</h2>
<h2><a href="2015">2015</a> 3</h2>
<ul>

<li><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></li>
<li><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></li>

<li><a href="http://manuel-ohlendorf.de/blog/2015/09/03/material-design-buttons-with-appcompat/">Material Design Buttons with AppCompat</a></li>

Expand Down
153 changes: 153 additions & 0 deletions blog/2015/09/29/the-crazy-date/time-formatting-in-go/index.html
@@ -0,0 +1,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 &middot; 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>&nbsp;&nbsp;
<a href="https://github.com/mohlendo"><i class="fa fa-github-square"></i></a>&nbsp;&nbsp;
<a href="https://www.xing.com/profile/Manuel_Ohlendorf"><i class="fa fa-xing"></i></a>&nbsp;&nbsp;

<p class="footnote">&copy; 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 &middot; 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&rsquo;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&rsquo;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 (
&quot;fmt&quot;
&quot;time&quot;
)

func main() {
// my birthday
value := &quot;04/19/1979&quot;
// the format of the value
parseFormat := &quot;01/02/2006&quot;

// parse the string into a time object
time, _ := time.Parse(parseFormat, value)

// the german output format
printFormat := &quot;02.01.2006&quot;

// and print it with the desired format
fmt.Println(time.Format(printFormat))
}

// =&gt; &quot;19.04.1979&quot;
</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&rsquo;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>
2 changes: 1 addition & 1 deletion blog/index.html
Expand Up @@ -85,7 +85,7 @@ <h1>Entries</h1>
<ul class="posts">

<li>
<span><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Thu, Oct 29, 2015</time></span>
<span><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Tue, Sep 29, 2015</time></span>
</li>

<li>
Expand Down
8 changes: 4 additions & 4 deletions blog/index.xml
Expand Up @@ -7,15 +7,15 @@
<language>en-us</language>
<author>Manuel Ohlendorf</author>

<updated>Thu, 29 Oct 2015 09:34:00 &#43;0200</updated>
<updated>Tue, 29 Sep 2015 09:34:00 CEST</updated>


<item>
<title>The crazy date/time formatting in Go</title>
<link>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Thu, 29 Oct 2015 09:34:00 &#43;0200</pubDate>
<link>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Tue, 29 Sep 2015 09:34:00 CEST</pubDate>
<author>Manuel Ohlendorf</author>
<guid>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</guid>
<guid>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</guid>
<description>&lt;p&gt;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 &lt;a href=&#34;http://www.unicode.org/reports/tr35/#Date_Format_Patterns&#34;&gt;UTS #35&lt;/a&gt;. 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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; EEE, MM/dd hh:mm:ssa yyyy ZZZ
Expand Down
2 changes: 1 addition & 1 deletion categories/development/index.html
Expand Up @@ -85,7 +85,7 @@ <h1>Entries</h1>
<ul class="posts">

<li>
<span><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Thu, Oct 29, 2015</time></span>
<span><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Tue, Sep 29, 2015</time></span>
</li>

<li>
Expand Down
8 changes: 4 additions & 4 deletions categories/development/index.xml
Expand Up @@ -7,15 +7,15 @@
<language>en-us</language>
<author>Manuel Ohlendorf</author>

<updated>Thu, 29 Oct 2015 09:34:00 &#43;0200</updated>
<updated>Tue, 29 Sep 2015 09:34:00 CEST</updated>


<item>
<title>The crazy date/time formatting in Go</title>
<link>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Thu, 29 Oct 2015 09:34:00 &#43;0200</pubDate>
<link>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Tue, 29 Sep 2015 09:34:00 CEST</pubDate>
<author>Manuel Ohlendorf</author>
<guid>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</guid>
<guid>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</guid>
<description>&lt;p&gt;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 &lt;a href=&#34;http://www.unicode.org/reports/tr35/#Date_Format_Patterns&#34;&gt;UTS #35&lt;/a&gt;. 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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; EEE, MM/dd hh:mm:ssa yyyy ZZZ
Expand Down
2 changes: 1 addition & 1 deletion categories/index.html
Expand Up @@ -88,7 +88,7 @@ <h2><a href="categories/development">development</a> 5</h2>

<li><a href="http://manuel-ohlendorf.de/blog/2015/09/03/material-design-buttons-with-appcompat/">Material Design Buttons with AppCompat</a></li>

<li><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></li>
<li><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></li>

</ul>

Expand Down
6 changes: 3 additions & 3 deletions index.html
Expand Up @@ -77,13 +77,13 @@ <h1 class="brand"><a href="/">Manuel Ohlendorf</a></h1>


<div class="summary">
<h1><a href="/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></h1>
<span class="post-date">Thu, Oct 29, 2015 &middot; 2 minute read</span>
<h1><a href="/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a></h1>
<span class="post-date">Tue, Sep 29, 2015 &middot; 2 minute read</span>
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 UTS #35. 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 EEE, MM/dd hh:mm:ssa yyyy ZZZ 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 hh, a is the am/pm marker, etc.
</div>

<div class="read-more-link">
<a href="/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">Read More…</a>
<a href="/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">Read More…</a>
</div>


Expand Down
8 changes: 4 additions & 4 deletions index.xml
Expand Up @@ -7,15 +7,15 @@
<language>en-us</language>
<author>Manuel Ohlendorf</author>

<updated>Thu, 29 Oct 2015 09:34:00 &#43;0200</updated>
<updated>Tue, 29 Sep 2015 09:34:00 CEST</updated>


<item>
<title>The crazy date/time formatting in Go</title>
<link>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Thu, 29 Oct 2015 09:34:00 &#43;0200</pubDate>
<link>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Tue, 29 Sep 2015 09:34:00 CEST</pubDate>
<author>Manuel Ohlendorf</author>
<guid>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</guid>
<guid>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</guid>
<description>&lt;p&gt;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 &lt;a href=&#34;http://www.unicode.org/reports/tr35/#Date_Format_Patterns&#34;&gt;UTS #35&lt;/a&gt;. 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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; EEE, MM/dd hh:mm:ssa yyyy ZZZ
Expand Down
6 changes: 3 additions & 3 deletions sitemap.xml
Expand Up @@ -3,13 +3,13 @@

<url>
<loc>http://manuel-ohlendorf.de/</loc>
<lastmod>2015-10-29T09:34:00+02:00</lastmod>
<lastmod>2015-09-29T09:34:00+02:00</lastmod>
<priority>0</priority>
</url>

<url>
<loc>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</loc>
<lastmod>2015-10-29T09:34:00+02:00</lastmod>
<loc>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</loc>
<lastmod>2015-09-29T09:34:00+02:00</lastmod>
</url>

<url>
Expand Down
2 changes: 1 addition & 1 deletion tags/go/index.html
Expand Up @@ -85,7 +85,7 @@ <h1>Entries</h1>
<ul class="posts">

<li>
<span><a href="http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Thu, Oct 29, 2015</time></span>
<span><a href="http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/">The crazy date/time formatting in Go</a> <time class="pull-right post-list">Tue, Sep 29, 2015</time></span>
</li>

</ul>
Expand Down
8 changes: 4 additions & 4 deletions tags/go/index.xml
Expand Up @@ -7,15 +7,15 @@
<language>en-us</language>
<author>Manuel Ohlendorf</author>

<updated>Thu, 29 Oct 2015 09:34:00 &#43;0200</updated>
<updated>Tue, 29 Sep 2015 09:34:00 CEST</updated>


<item>
<title>The crazy date/time formatting in Go</title>
<link>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Thu, 29 Oct 2015 09:34:00 &#43;0200</pubDate>
<link>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</link>
<pubDate>Tue, 29 Sep 2015 09:34:00 CEST</pubDate>
<author>Manuel Ohlendorf</author>
<guid>http://manuel-ohlendorf.de/blog/2015/10/29/the-crazy-date/time-formatting-in-go/</guid>
<guid>http://manuel-ohlendorf.de/blog/2015/09/29/the-crazy-date/time-formatting-in-go/</guid>
<description>&lt;p&gt;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 &lt;a href=&#34;http://www.unicode.org/reports/tr35/#Date_Format_Patterns&#34;&gt;UTS #35&lt;/a&gt;. 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&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; EEE, MM/dd hh:mm:ssa yyyy ZZZ
Expand Down

0 comments on commit cababc9

Please sign in to comment.