52 lines
980 B
Go
Executable File
52 lines
980 B
Go
Executable File
package main
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
type Transaction struct {
|
|
ID string
|
|
Bank Bank
|
|
Amount string
|
|
Account string
|
|
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)
|
|
}
|
|
|
|
func NewTransaction(amount, account, date string, bank Bank) *Transaction {
|
|
t := &Transaction{
|
|
Amount: amount,
|
|
Account: account,
|
|
Bank: bank,
|
|
Date: date,
|
|
}
|
|
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")
|
|
}
|