-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
106 lines (92 loc) · 2.2 KB
/
utils.go
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
package ledger
import (
"fmt"
"log"
"strconv"
"time"
)
const (
DATE_TIME string = "20060102T150405"
DATE_ONLY string = "2006-01-02"
)
/**
* @brief: Check if any errors occured, log the error if there are any
*
* @arg: e - Error
**/
func CheckErr(e error) {
if nil != e {
log.Fatal(e)
}
}
/**
* @brief: Converts a string to an uint64.
*
* @arg: str - String that is to be converted to uint64
*
* @return: uint64 val of string
**/
func StrToUint(str string) uint64 {
u, err := strconv.ParseUint(str, 10, 64)
CheckErr(err)
return u
}
/**
* @brief: Converts a string to an float64.
*
* @arg: str - String that is to be converted to float64
*
* @return: float64 val of string
**/
func StrToFloat(str string) float64 {
f, err := strconv.ParseFloat(str, 64)
CheckErr(err)
return f
}
/***
* @brief: Convert a number (int, float, etc) to a string
*
* @arg: number - Number that is to be converted
*
* @return: string value of the number
***/
func NumToStr(number interface{}) string {
return fmt.Sprintf("%v", number)
}
/**
* @brief: A ternary functions that checks for the condition and
* return a value. Type cast the function to get the
* data type that is expecting
*
* @arg: condition - Condition to check for validity
* @arg: valid - Val being returned if the condition is true
* @arg: invalid - Val being returned if the condition is false
*
* @return: Depending on the condition, an interface value is returned.
**/
func ternary(condition bool, valid, invalid interface{}) interface{} {
if condition {
return valid
}
return invalid
}
/**
* @brief: Get the current date and time
*
* @agr: The format of which to put the date
*
* @return: The date and/or time in the format of which the user has entered
**/
func GetDate(format string) string {
currTime := time.Now()
return currTime.Format(format)
}
func FormatDate(date string) string{
/* Date */
fmtDate := date[:4] + "/" + date[4:6] + "/" + date[6:8]
/* Time */
if len(date) == 15 {
fmtDate += " " + date[9:11] + ":" + date[11:13] + ":" + date[13:15]
}
return fmtDate
}