format.go 1.04 KB
package webutility

import (
	"fmt"
	"time"
)

// UnixToDate converts given Unix time to local time in format and returns result:
// YYYY-MM-DD hh:mm:ss +zzzz UTC
func UnixToDate(unix int64) time.Time {
	return time.Unix(unix, 0)
}

// DateToUnix converts given date in Unix timestamp.
func DateToUnix(date interface{}) int64 {
	if date != nil {
		t, ok := date.(time.Time)
		if !ok {
			return 0
		}
		return t.Unix()

	}
	return 0
}

// EqualQuotes encapsulates given string in SQL 'equal' statement and returns result.
// Example: "hello" -> " = 'hello'"
func EqualQuotes(stmt string) string {
	if stmt != "" {
		stmt = fmt.Sprintf(" = '%s'", stmt)
	}
	return stmt
}

// EqualString ...
func EqualString(stmt string) string {
	if stmt != "" {
		stmt = fmt.Sprintf(" = %s", stmt)
	}
	return stmt
}

// LikeQuotes encapsulates given string in SQL 'like' statement and returns result.
// Example: "hello" -> " LIKE UPPER('%hello%')"
func LikeQuotes(stmt string) string {
	if stmt != "" {
		stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%")
	}
	return stmt
}