Documentation
¶
Index ¶
- Constants
- func CalculateTotalTime(entries []JournalEntry) (time.Duration, error)
- func SaveEntries(journalEntries []JournalEntry, filename string) error
- type Break
- type Journal
- type JournalEntry
- func FetchEntriesByMonthDate(journalEntries []JournalEntry, filterDate time.Time) ([]JournalEntry, error)
- func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error)
- func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int)
- func LoadEntries(filename string) ([]JournalEntry, error)
- func NewJournalEntry() *JournalEntry
- type Note
Constants ¶
const SchemaVersion = 1
Variables ¶
This section is empty.
Functions ¶
func CalculateTotalTime ¶
func CalculateTotalTime(entries []JournalEntry) (time.Duration, error)
CalculateTotalTime calculates the total time duration for a slice of JournalEntry. It iterates over each entry in the slice and checks if the end time of the entry is after the start time. If the end time is not after the start time, it returns an error indicating that the entry is invalid. Otherwise, it adds the difference between the end time and the start time to the total duration. After iterating over all entries, it returns the total duration and nil. If there are no entries in the slice, it returns a duration of 0 and nil.
func SaveEntries ¶
func SaveEntries(journalEntries []JournalEntry, filename string) error
SaveEntries encodes the given journal entries into JSON format and writes them to a file with the specified filename.
The function will return an error if the encoding process fails or if the file cannot be created or written to.
The function takes two parameters: - journalEntries: a slice of JournalEntry objects to be saved. - filename: a string representing the name of the file to write to.
Example:
entries := []JournalEntry{...}
err := SaveEntries(entries, "journal.json")
if err != nil {
log.Fatal(err)
}
Types ¶
type Break ¶ added in v0.5.0
type Journal ¶ added in v0.5.0
type Journal struct {
Version int `json:"version"` // schema version for the loaded journal
Entries []JournalEntry `json:"entries"` // journal entries
}
type JournalEntry ¶
type JournalEntry struct {
ID string `json:"id"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Notes []Note `json:"notes,omitempty"`
Breaks []Break `json:"breaks,omitempty"`
}
func FetchEntriesByMonthDate ¶
func FetchEntriesByMonthDate(journalEntries []JournalEntry, filterDate time.Time) ([]JournalEntry, error)
FetchEntriesByMonthDate filters a slice of JournalEntry objects and returns a new slice containing only the entries from the given date's month. The function uses the Year() and Month() functions from time standard library
The function takes a slice of JournalEntry objects and iterates over each entry. It checks the start time of each entry and compares it with the current month. If the entry belongs to the current month, it is added to the new slice.
If no entries are passed, or no entries belong to the current month, the function returns an error along with an empty slice.
Example:
entries := []JournalEntry{...}
currentMonthEntries, err := FetchEntriesByMonthDate(entries)
if err != nil {
log.Fatal(err)
}
fmt.Println(currentMonthEntries) // Prints the entries for the current week
func FetchEntriesByWeekDate ¶
func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error)
FetchEntriesByWeekDate filters a slice of JournalEntry objects and returns a new slice containing only the entries from the given date's week. The function uses the ISO week date system, where weeks start on a Monday and the first week of the year is the one that includes at least four days of the new year.
The function takes a slice of JournalEntry objects and iterates over each entry. It checks the start time of each entry and compares it with the current week. If the entry belongs to the current week, it is added to the new slice.
If no entries are passed, or no entries belong to the current week, the function returns an error along with an empty slice.
Example:
entries := []JournalEntry{...}
currentWeekEntries, err := FetchEntriesByWeekDate(entries)
if err != nil {
log.Fatal(err)
}
fmt.Println(currentWeekEntries) // Prints the entries for the current week
func FetchEntryByID ¶
func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int)
FetchEntryByID searches for a JournalEntry in the provided slice of entries by its ID. It returns a pointer to the found JournalEntry and its index in the slice If the entry is not found, it returns nil and -1.
func LoadEntries ¶
func LoadEntries(filename string) ([]JournalEntry, error)
LoadEntries reads the JSON file with the given filename, unmarshals its contents into a slice of JournalEntry, and returns the slice. If the file does not exists, LoadEntries creates the file and returns an empty slice. If the file cannot be created, LoadEntries returns an error.
func NewJournalEntry ¶
func NewJournalEntry() *JournalEntry
func (*JournalEntry) AddNote ¶
func (j *JournalEntry) AddNote(note Note) error
func (*JournalEntry) EndDay ¶
func (j *JournalEntry) EndDay()
func (*JournalEntry) String ¶
func (j *JournalEntry) String() string
func (*JournalEntry) TotalWorkTime ¶ added in v0.7.0
func (j *JournalEntry) TotalWorkTime() time.Duration