You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

181 lines
4.5 KiB

// tools
package tools
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)
// #############################################################################
// correct data-line
// #############################################################################
func CorrectDates(_data string) string {
r := strings.NewReplacer(
" Januar ", ".01.",
" Februar ", ".02.",
" März ", ".03.",
" April ", ".04.",
" Mai ", ".05.",
" Juni ", ".06.",
" Juli ", ".07.",
" August ", ".08.",
" September ", ".09.",
" Oktober ", ".10.",
" November ", ".11.",
" Dezember ", ".12.")
return r.Replace(_data)
}
// #############################################################################
// date / time - functions
// #############################################################################
func GetDate(_date string, _template string) time.Time {
var datetoparse string = _date
loc, _ := time.LoadLocation("Europe/Berlin")
if strings.Contains(datetoparse, " ") {
datetoparse = strings.Split(_date, " ")[0]
}
// führende 0 ergänzen
if len(datetoparse) < 10 {
datetoparse = "0" + datetoparse
}
t, err := time.ParseInLocation(_template, datetoparse, loc)
if err != nil {
panic(err)
}
return t
}
func GetsubstDate(_template string, _date time.Time, _writetemplate string) string {
dateresult := fmt.Sprintf("%d/%d/%d", _date.Year(), _date.Month(), _date.Day())
return strings.Replace(_template, _writetemplate, dateresult, -1)
}
func GetsubstYear(_template string, _date time.Time, _writetemplate string) string {
yearresult := fmt.Sprintf("%d", _date.Year())
return strings.Replace(_template, _writetemplate, yearresult, -1)
}
func GetsubstMonth(_template string, _date time.Time, _writetemplate string) string {
monthresult := fmt.Sprintf("%d", _date.Month())
return strings.Replace(_template, _writetemplate, monthresult, -1)
}
func GetsubstDay(_template string, _date time.Time, _writetemplate string) string {
dayresult := fmt.Sprintf("%d", _date.Day())
return strings.Replace(_template, _writetemplate, dayresult, -1)
}
// #############################################################################
// string helper functions
// #############################################################################
// get string between (tool)
func GetstringBetween(_string string, _start string, _end string) string {
var str = "" + _string
var s = strings.Index(str, _start)
if s == -1 {
return ""
}
s += len(_start)
e := strings.Index(str, _end)
if e == -1 {
return ""
}
return str[s:e]
}
// delete (split) string
func DeleteString(_strSplit string, _delimiter string, _from int64) string {
var list string = ""
icmp := int(_from)
for i, entry := range strings.Split(_strSplit, _delimiter) {
if i >= icmp {
list = list + entry
}
}
return list
}
// #############################################################################
// file helper functions
// #############################################################################
func OpenFile(_filename string) *os.File {
f, err := os.Create(fmt.Sprintf("%s", _filename))
if err != nil {
defer f.Close()
fmt.Println(err)
panic(err)
}
return f
}
func WriteFile(_file *os.File, _value string) {
_, err := _file.WriteString(fmt.Sprintf("%s", _value))
if err != nil {
defer _file.Close()
panic(err)
}
}
// open and read text file
func GetTexfile(_file string) (*os.File, error) {
impfile, err := os.Open(_file)
if err != nil {
log.Fatalf("failed opening file: %s", err)
defer impfile.Close()
}
//
return impfile, err
}
func DeteteFiles(_path string, _name string) {
txtlist, err := FindFilesBySuffix(_path, _name)
if err != nil {
log.Fatalf("failed deleting files: %s", err)
}
// fmt.Println(txtlist)
for _, tv := range txtlist {
err := os.Remove(_path + tv)
if err != nil {
fmt.Printf("### %s%s: %s", _path, tv, err)
}
}
}
func RemoveAll(_path string) {
err := os.RemoveAll(_path)
if err != nil {
fmt.Printf("### %s%s: %s", _path, err)
}
}
// get file list by suffix
func FindFilesBySuffix(_root, _pattern string) ([]string, error) {
var matches []string
err := filepath.Walk(_root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if matched, err := filepath.Match(_pattern, filepath.Base(path)); err != nil {
return err
} else {
if matched {
matches = append(matches, path)
}
}
return nil
})
if err != nil {
return nil, err
}
return matches, nil
}