title: How to do date/time comparison
body: <p>Is there any options in doing date comparison in Go? I have to sort data based on date and time - independently. So I might allow an object that occurs within a range of dates so long as it also occurs within a range of times. In this model, I could not simply just select the oldest date, youngest time/latest date, latest time and Unix() seconds compare them. I'd really appreciate any suggestions.</p>
  
  <p>Ultimately, I wrote a time parsing string compare module to check if a time is within a range. However, this is not faring to well; I've got some gaping issues. I'll post that here just for fun, but I'm hoping there's a better way to time compare.</p>
  
  <pre><code>package main
  
  import (
      "strconv"
      "strings"
  )
  
  func tryIndex(arr []string, index int, def string) string {
      if index &lt;= len(arr)-1 {
          return arr[index]
      }
      return def
  }
  
  /*
   * Takes two strings of format "hh:mm:ss" and compares them.
   * Takes a function to compare individual sections (split by ":").
   * Note: strings can actually be formatted like "h", "hh", "hh:m",
   * "hh:mm", etc. Any missing parts will be added lazily.
   */
  func timeCompare(a, b string, compare func(int, int) (bool, bool)) bool {
      aArr := strings.Split(a, ":")
      bArr := strings.Split(b, ":")
      // Catches margins.
      if (b == a) {
          return true
      }
      for i := range aArr {
          aI, _ := strconv.Atoi(tryIndex(aArr, i, "00"))
          bI, _ := strconv.Atoi(tryIndex(bArr, i, "00"))
          res, flag := compare(aI, bI)
          if res {
              return true
          } else if flag { // Needed to catch case where a &gt; b and a is the lower limit
              return false
          }
      }
      return false
  }
  
  func timeGreaterEqual(a, b int) (bool, bool) {return a &gt; b, a &lt; b}
  func timeLesserEqual(a, b int) (bool, bool) {return a &lt; b, a &gt; b}
  
  /*
   * Returns true for two strings formmated "hh:mm:ss".
   * Note: strings can actually be formatted like "h", "hh", "hh:m",
   * "hh:mm", etc. Any missing parts will be added lazily.
   */
  func withinTime(timeRange, time string) bool {
      rArr := strings.Split(timeRange, "-")
      if timeCompare(rArr[0], rArr[1], timeLesserEqual) {
          afterStart := timeCompare(rArr[0], time, timeLesserEqual)
          beforeEnd := timeCompare(rArr[1], time, timeGreaterEqual)
          return afterStart &amp;&amp; beforeEnd
      }
      // Catch things like `timeRange := "22:00:00-04:59:59"` which will happen
      // with UTC conversions from local time.
      // THIS IS THE BROKEN PART I BELIEVE
      afterStart := timeCompare(rArr[0], time, timeLesserEqual)
      beforeEnd := timeCompare(rArr[1], time, timeGreaterEqual)
      return afterStart || beforeEnd
  }
  </code></pre>
  
  <p>So TLDR, I wrote a withinTimeRange(range, time) function but it's not working totally correctly. (In fact, mostly just the second case, where a time range crosses over days is broken. The original part worked, I just realized I'd need to account for that when making conversions to UTC from local.)</p>
  
  <p>If there's a better (preferably built in) way, I'd love to hear about it!</p>
  
  <p>NOTE:
  Just as an example, I solved this issue in Javascript with this function:</p>
  
  <pre><code>function withinTime(start, end, time) {
      var s = Date.parse("01/01/2011 "+start);
      var e = Date.parse("01/0"+(end=="24:00:00"?"2":"1")+"/2011 "+(end=="24:00:00"?"00:00:00":end));
      var t = Date.parse("01/01/2011 "+time);
      return s &lt;= t &amp;&amp; e &gt;= t;
  }
  </code></pre>
  
  <p>However I really want to do this filter server-side.</p>