Advertisement
ser

Date extraction, Go, V4

ser
Feb 14th, 2012
419
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.         "io/ioutil"
  6.         "os"
  7.         "time"
  8.         "regexp"
  9.         "strings"
  10. )
  11.  
  12. func main() {
  13.         file, _ := os.Open("SearchRequest.xml")
  14.         defer file.Close()
  15.         data, _ := ioutil.ReadAll(file)
  16.         lines := strings.Split(string(data), "\n")
  17.         re, _ := regexp.Compile("<created>(.*)</created>")
  18.         for _, line := range lines {
  19.                 matches := re.FindStringSubmatch(line)
  20.                 if len(matches) > 0 {
  21.                         date := matches[1]
  22.                         t, e := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", string(date))
  23.                         if e != nil {
  24.                                 fmt.Fprintf(os.Stderr, "Unable to parse %s\n", string(date))
  25.                         } else {
  26.                                 fmt.Printf(t.Format("2006/01/02 15:04:05\n"))
  27.                         }
  28.                 }
  29.         }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement