Changing date and time formats

In this section, you will learn how to change the format of a string that contains both a date and a time. A very common place for finding such strings is the log files of web servers such as Apache and Nginx. As we do not know how to read a text file line by line, the text will be hard coded in the program, This fact, however, does not change the functionality of the program.

The Go code of timeDate.go will be presented in four parts. The first part of this program is the following expected preamble:

package main 
 
import ( 
    "fmt" 
    "regexp" 
    "time" 
) 

You need to import the regexp standard Go package for supporting regular expressions.

The second code portion of timeDate.go is as follows:

func main() { 
 
    logs := []string{"127.0.0.1 - - [16/Nov/2017:10:49:46 +0200] 
325504", "127.0.0.1 - - [16/Nov/2017:10:16:41 +0200] "GET /CVEN
HTTP/1.1" 200 12531 "-" "Mozilla/5.0 AppleWebKit/537.36", "127.0.0.1 200 9412 - - [12/Nov/2017:06:26:05 +0200] "GET
"http://www.mtsoukalos.eu/taxonomy/term/47" 1507", "[12/Nov/2017:16:27:21 +0300]", "[12/Nov/2017:20:88:21 +0200]", "[12/Nov/2017:20:21 +0200]", }

As you cannot be sure about your data and its format, the sample data used for this program tries to cover many different cases, including incomplete data like [12/Nov/2017:20:21 +0200], where there are no seconds in the time part, and erroneous data such as [12/Nov/2017:20:88:21 +0200], where the value of the minutes is 88.

The third part of timeDate.go contains the following Go code:

    for _, logEntry := range logs { 
        r := regexp.MustCompile('.*[(dd/w+/dddd:dd:dd:dd.*)].*') 
        if r.MatchString(logEntry) { 
            match := r.FindStringSubmatch(logEntry) 

The main benefit that you receive from such a difficult-to-read regular expression in this particular program is that it allows your code to find out whether you have a date and time string somewhere in your line or not. After you get that string, you will feed time.Parse() with it and let that do the rest of the job!

The last part of the program is shown in the following Go code:

            dt, err := time.Parse("02/Jan/2006:15:04:05 -0700", 
match[1]) if err == nil { newFormat := dt.Format(time.RFC850) fmt.Println(newFormat) } else { fmt.Println("Not a valid date time format!") } } else { fmt.Println("Not a match!") } } }

Once you find a string that matches the regular expression, you parse it using time.Parse() to make sure that it is a valid date/time string. If yes, timeDate.go will print the date and time according to the RFC850 format.

If you execute timeDate.go, you will get the following output:

$ go run timeDate.go
Thursday, 16-Nov-17 10:49:46 EET
Thursday, 16-Nov-17 10:16:41 EET
Sunday, 12-Nov-17 06:26:05 EET
Sunday, 12-Nov-17 16:27:21 +0300
Not a valid date time format!
Not a match!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.148.210