Advertisement
ser

Date extraction, Go, V2

ser
Feb 14th, 2012
445
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"
  6.         "os"
  7.         "time"
  8.         "code.google.com/p/sre2/sre2"
  9.         "bufio"
  10. )
  11.  
  12. func main() {
  13.         file, _ := os.Open("SearchRequest.xml")
  14.         reader := bufio.NewReader(file)
  15.         re := sre2.MustParse("<created>(.*)</created>")
  16.         for line, _, e := reader.ReadLine(); e != io.EOF; line, _, e = reader.ReadLine() {
  17.                 idxs := re.MatchIndex(string(line))
  18.                 if len(idxs) > 0 {
  19.                         date := line[idxs[2]:idxs[3]]
  20.                         t, e := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", string(date))
  21.                         if e != nil {
  22.                                 fmt.Fprintf(os.Stderr, "Unable to parse %s\n", string(date))
  23.                         } else {
  24.                                 fmt.Printf(t.Format("2006/01/02 15:04:05\n"))
  25.                         }
  26.                 }
  27.         }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement