Advertisement
ser

Date extraction, Go, V5

ser
Feb 14th, 2012
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.93 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "os"
  6.         "time"
  7.         "regexp"
  8.         "io"
  9.         "bufio"
  10. )
  11.  
  12. func main() {
  13.         file, _ := os.Open("SearchRequest.xml")
  14.         defer file.Close()
  15.         reader := bufio.NewReader(file)
  16.         re, _ := regexp.Compile("<created>(.*)</created>")
  17.         for line, _, e := reader.ReadLine(); e != io.EOF; line, _, e = reader.ReadLine() {
  18.                 matches := re.FindStringSubmatch(string(line))
  19.                 if len(matches) > 0 {
  20.                         date := matches[1]
  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