Custom date format as string conversion

I (as an unexperienced go programmer) wrote a program (“stolen” from the web) that reads generic database tables and exports this data to a csv file. The program scans database record into a bytes slice then simply assigns a string slice using the string(raw) code.

In case of datetime database field the resulting string value is the default “2006-01-02T15:04:05-0700” go format, so the question is how can i change the resulting format?

Thanks in advance,
Stefano

What outut do you want instead? Note that what you have is ISO 8601, a very common and portable format for dates and datetimes.

Use func (Time) Format:

package main

import (
	"fmt"
	"time"
)

func main() {
	t, _ := time.Parse(time.UnixDate, "Sat Mar  7 11:06:39 PST 2015")
	fmt.Println(t)
	s := t.Format("02.01.2006, 15:04:05")
	fmt.Println(s)
}

Output:

2015-03-07 11:06:39 +0000 PST
07.03.2015, 11:06:39

https://play.golang.com/p/6dN_hTAEIbS

I would like to choose the format i want even at runtime/execution time. I know that the format is ISO but unfortunately as far as i can understand this format is not recognized by Excel and 99% of customers loads csv files directly into excel just double clicking on the file.
Regatrding the time.Parse is ok but the problem here is that i don’t know that i’m managing a date, the string([raw_value]) does this work for me very good but only with the ISO date format.

What is the type of raw_value?

OK,
what i did (getting code from internet) is reading data into a interface slice pointing to a byte slice then converting with string(…) so rawdata is a byte:

// create a slice of interfaces to point to data columns
readCols := make([]interface{}, len(colNames))
// create a slice of strings that will be written on file
writeCols := make([]string, len(colNames))
// create a slice of byte to read data in
rawResult := make([][]byte, len(colNames))

// let the interface point to the bytes
for i, _ := range writeCols {
	readCols[i] = &rawResult[i]
}

for rows.Next() {

	// reads a row into the interface slice so data will go into byte slice
	err = rows.Scan(readCols...)
	if err != nil {
		fmt.Println("Failed to scan row", err)
		return
	}

	// i have to copy bytes into strings since the string conversion gives error on null db values
	for i, raw := range rawResult {
		if raw == nil  {
			writeCols[i] = ""
		} else {
			writeCols[i] = string(raw)
		}
	}

	// write the string row
	writer.Write(writeCols)
}

Here you have a string. So you can apply Time.Parse to it if you are sure that it contains a timestamp. How do you know? Is there any hint that a column contains a date or a datetime? Maybe the column name can be used?

BTW: Do you want to build a tool that has no knowledge about the table structure at all? How can you expect it to recognize dates and datetimes?

Maybe using the export tool of your database would be a better idea. These tools usually are able to write CSV and to format columns.

OK,
you are right in the meaning that the conversion is not made by the string(raw) code, i have to know the column type from the database.

BTW: Yes, i want to build a tool without hardcoding table specs, many tools can do this but they are slow, i can be much faster for huge tables and i wanted to test golang :slight_smile: .

Thanks a lot.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.