Skip to content

Instantly share code, notes, and snippets.

@caelifer
Created October 28, 2015 20:46
Show Gist options
  • Save caelifer/4f7fccbabfa35ecb4b8c to your computer and use it in GitHub Desktop.
Save caelifer/4f7fccbabfa35ecb4b8c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"regexp"
"strings"
"time"
)
var (
timeStampRx = regexp.MustCompile(`^\[(?:[-$0-9])+\]([12]?[0-9]/[1-3]?[0-9]/[0-9]{2} [12]?[0-9]:[0-9]{2}) ([AaPp][Mm]);@`)
dateFormat = "1/2/06 3:04PM"
)
func main() {
alltests := strings.Split(`[$-2409]10/2/15 1:24 pm;@
[$-009]9/28/15 2:00 pm;@
[$-5909]10/9/15 1:59 pm;@
[$-4009]10/7/15 4:40 pm;@
[$-5009]1/30/15 10:50 pm;@
[$-1209]11/12/13 4:12 pm;@
[$-1909]10/7/15 5:19 pm;@
[$-3609]10/7/15 5:36 pm;@
[$-3809]1/13/15 2:38 pm;@
[$-4409]1/13/15 4:44 pm;@
[$-3309]11/7/13 8:33 pm;@
[$-1409]10/1/15 2:14 pm;@
[$-509]10/5/15 3:05 pm;@
[$-2609]10/14/15 1:26 pm;@
[$-909]10/9/15 5:09 pm;@
[$-3809]2/6/14 6:38 pm;@
[$-5909]11/5/12 12:59 pm;@
[$-2109]10/2/15 2:21 pm;@
[$-1009]9/15/15 8:10 pm;@
[$-509]10/13/15 5:05 pm;@
[$-109]10/13/15 8:01 pm;@`, "\n")
for _, tst := range alltests {
res := timeStampRx.FindAllStringSubmatch(tst, -1)[0][1:3]
res[1] = strings.ToUpper(res[1])
t0 := strings.Join(res, "")
t1, err := time.ParseInLocation(dateFormat, t0, time.Local)
if err != nil {
log.Println("[WARN] Failed to parse time:", err)
}
fmt.Printf("%q \t %q\n", t0, t1)
}
}
@caelifer
Copy link
Author

Live code - http://play.golang.org/p/HYJSJWxoI2

Output:

"10/2/15 1:24PM"     "2015-10-02 13:24:00 +0000 UTC"
"9/28/15 2:00PM"     "2015-09-28 14:00:00 +0000 UTC"
"10/9/15 1:59PM"     "2015-10-09 13:59:00 +0000 UTC"
"10/7/15 4:40PM"     "2015-10-07 16:40:00 +0000 UTC"
"1/30/15 10:50PM"    "2015-01-30 22:50:00 +0000 UTC"
"11/12/13 4:12PM"    "2013-11-12 16:12:00 +0000 UTC"
"10/7/15 5:19PM"     "2015-10-07 17:19:00 +0000 UTC"
"10/7/15 5:36PM"     "2015-10-07 17:36:00 +0000 UTC"
"1/13/15 2:38PM"     "2015-01-13 14:38:00 +0000 UTC"
"1/13/15 4:44PM"     "2015-01-13 16:44:00 +0000 UTC"
"11/7/13 8:33PM"     "2013-11-07 20:33:00 +0000 UTC"
"10/1/15 2:14PM"     "2015-10-01 14:14:00 +0000 UTC"
"10/5/15 3:05PM"     "2015-10-05 15:05:00 +0000 UTC"
"10/14/15 1:26PM"    "2015-10-14 13:26:00 +0000 UTC"
"10/9/15 5:09PM"     "2015-10-09 17:09:00 +0000 UTC"
"2/6/14 6:38PM"      "2014-02-06 18:38:00 +0000 UTC"
"11/5/12 12:59PM"    "2012-11-05 12:59:00 +0000 UTC"
"10/2/15 2:21PM"     "2015-10-02 14:21:00 +0000 UTC"
"9/15/15 8:10PM"     "2015-09-15 20:10:00 +0000 UTC"
"10/13/15 5:05PM"    "2015-10-13 17:05:00 +0000 UTC"
"10/13/15 8:01PM"    "2015-10-13 20:01:00 +0000 UTC"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment