Advertisement
ser

Date extraction, Go, V3

ser
Feb 14th, 2012
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "io/ioutil"
  6.         "os"
  7.         "time"
  8.         "code.google.com/p/sre2/sre2"
  9.         "strings"
  10. )
  11.  
  12. func main() {
  13.         file, _ := os.Open("SearchRequest.xml")
  14.         data, _ := ioutil.ReadAll(file)
  15.         lines := strings.Split(string(data), "\n")
  16.         re := sre2.MustParse("<created>(.*)</created>")
  17.         for _, line := range lines {
  18.                 idxs := re.MatchIndex(string(line))
  19.                 if len(idxs) > 0 {
  20.                         date := line[idxs[2]:idxs[3]]
  21.                         t, e := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", string(date))
  22.                         if e != nil {
  23.                                 fmt.Fprintf(os.Stderr, "Unable to parse %s\n", string(date))
  24.                         } else {
  25.                                 fmt.Printf(t.Format("2006/01/02 15:04:05\n"))
  26.                         }
  27.                 }
  28.         }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement