This repository was archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
108 lines (99 loc) · 2.58 KB
/
Copy pathutils.go
File metadata and controls
108 lines (99 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package incomes
import (
"errors"
"reflect"
"strconv"
"strings"
"time"
"github.com/eraclitux/stracer"
)
// ParseDate converts a string date in the format
// dd-MM-yyyy in a time.Time.
func ParseDateFromBackend(d string) (time.Time, error) {
// How reference date "Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)" will be
// formatted our way?
// http://stackoverflow.com/a/6590606
format := "02-01-2006"
date, err := time.Parse(format, d)
if err != nil {
return time.Now(), err
}
return date, nil
}
// ParseDate converts a string date in the format
// dd/mm/yyyy in a time.Time.
func ParseDate(d string) (time.Time, error) {
// How reference date "Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)" will be
// formatted our way?
// http://stackoverflow.com/a/6590606
format := "02/01/2006"
date, err := time.Parse(format, d)
if err != nil {
return time.Now(), err
}
return date, nil
}
func deNotate(line string) string {
s := strings.Replace(line, `.`, ``, -1)
return strings.Replace(s, `,`, `.`, -1)
}
// ParseFloat extends string.ParseFloat returnig 0.0
// in case of empty string.
// FIXME return error
func ParseFloat(s string) float32 {
s = deNotate(s)
value, err := strconv.ParseFloat(s, 32)
if err != nil {
// Empty string is zero for us not an error.
if e, ok := err.(*strconv.NumError); ok && e.Num == "" {
return 0
}
//panic("error converting to float: " + s)
stracer.Traceln("error converting to float", s, err)
return float32(-1)
}
return float32(value)
}
// Atoi extends strconv.Atoi returning 0 in case of empty string.
func Atoi(s string) (int, error) {
n, err := strconv.Atoi(s)
if err != nil {
// Empty string is zero for us not an error.
if e, ok := err.(*strconv.NumError); ok && e.Num == "" {
return 0, nil
} else {
return 0, err
}
}
return n, nil
}
// ExtractDistrict takes a string in the format "City (dst)"
// and returns city and district as two strings.
func ExtractDistrict(s string) (city, district string) {
if !strings.Contains(s, "(") {
city = s
district = s
return
}
a := strings.Split(s, " (")
city = a[0]
district = strings.TrimRight(a[1], ")")
return
}
// LowerStruct changes string field to lower case
// for the passed struct.
func LowerStruct(strPtr interface{}) error {
str := reflect.ValueOf(strPtr)
if str.Kind() != reflect.Ptr {
return errors.New("pointer to struct expected")
}
v := str.Elem()
for i := 0; i < v.NumField(); i++ {
fieldValue := v.Field(i)
if fieldValue.CanSet() && fieldValue.Kind() == reflect.String {
s := fieldValue.String()
fieldValue.SetString(strings.ToLower(s))
}
}
return nil
}