Split task format from internal storage

This commit is contained in:
bel
2020-04-03 00:18:39 +00:00
parent eafd022d7d
commit 9f3f3dc08f
8 changed files with 32 additions and 1 deletions

22
transaction.go Normal file → Executable file
View 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")
}