Split task format from internal storage
This commit is contained in:
22
transaction.go
Normal file → Executable file
22
transaction.go
Normal file → Executable file
@@ -3,6 +3,9 @@ package main
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
@@ -13,6 +16,10 @@ type Transaction struct {
|
||||
Date string
|
||||
}
|
||||
|
||||
func (t *Transaction) Format() string {
|
||||
return fmt.Sprintf("(%s) %v: %s @ %s", cleanDate(t.Date), t.Bank, t.Amount, t.Account)
|
||||
}
|
||||
|
||||
func (t *Transaction) String() string {
|
||||
return fmt.Sprint(*t)
|
||||
}
|
||||
@@ -27,3 +34,18 @@ func NewTransaction(amount, account, date string, bank Bank) *Transaction {
|
||||
t.ID = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprint(t))))
|
||||
return t
|
||||
}
|
||||
|
||||
func cleanDate(date string) string {
|
||||
regexp := regexp.MustCompile(`[A-Z][a-z]{2}, [0-9][0-9]? [A-Z][a-z]{2} 2[0-9]{3}`)
|
||||
matches := regexp.FindAllString(date, -1)
|
||||
if len(matches) < 1 {
|
||||
return date
|
||||
}
|
||||
date = matches[0]
|
||||
time, err := time.Parse(`Mon, 2 Jan 2006`, date)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return date
|
||||
}
|
||||
return time.Format("Mon Jan 2")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user