Skip to content

Commit 869512e

Browse files
author
Per-Gunnar Eriksson
committed
Fix reporting
1 parent 94a55a6 commit 869512e

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

src/commands/report.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn execute(
5050
);
5151

5252
// Convert entries to activities
53-
let mut activities = storage::entries_to_activities(&filtered_entries, Some(range.start_date), Some(range.end_date));
53+
let mut activities = storage::entries_to_activities(&filtered_entries, Some(range.start_date), Some(range.end_date), Some(now));
5454

5555
// For today-only reports, ensure we show all activities that have a start or end time today
5656
if is_today_only {

src/storage.rs

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -110,86 +110,70 @@ pub fn append_entry(data_file: &Path, entry: &Entry) -> Result<()> {
110110
Ok(())
111111
}
112112

113-
pub fn entries_to_activities(entries: &[Entry], start_date: Option<NaiveDate>, end_date: Option<NaiveDate>) -> Vec<Activity> {
113+
114+
pub fn entries_to_activities(
115+
entries: &[Entry],
116+
start_date: Option<NaiveDate>,
117+
end_date: Option<NaiveDate>,
118+
now: Option<DateTime<Local>> // Add parameter for current time
119+
) -> Vec<Activity> {
114120
let mut activities = Vec::new();
115121

116-
// We need at least two entries to create an activity
117-
if entries.len() < 2 {
122+
// We need at least one entry to create an activity
123+
if entries.is_empty() {
118124
return activities;
119125
}
120126

121127
// Create activities from consecutive entries, skipping midnight separators and hello entries
122128
for i in 0..entries.len() - 1 {
123-
// Skip if this is a midnight separator entry
124-
if entries[i+1].name.starts_with(MIDNIGHT_SEPARATOR_PREFIX) {
129+
// Skip current entry if it's a hello entry - hello doesn't create duration
130+
if entries[i].name == HELLO_ENTRY_NAME {
125131
continue;
126132
}
127133

128-
// Skip if previous entry was a midnight separator
134+
// Skip current entry if it's a midnight separator
129135
if entries[i].name.starts_with(MIDNIGHT_SEPARATOR_PREFIX) {
130136
continue;
131137
}
132138

133-
// Skip if this is a hello entry - it marks the start of day only
134-
if entries[i+1].name == HELLO_ENTRY_NAME {
135-
continue;
136-
}
137-
138-
// Skip if previous entry was a hello entry - hello doesn't create duration
139-
if entries[i].name == HELLO_ENTRY_NAME {
140-
continue;
141-
}
142-
143139
// Get the start and end times
144140
let start_time = entries[i].datetime;
145141
let end_time = entries[i+1].datetime;
146142

147-
// If the times span multiple days, split them into separate daily activities
148-
if start_time.date_naive() != end_time.date_naive() {
149-
// Create an activity for each day in the range
150-
let mut current_date = start_time.date_naive();
151-
let end_date = end_time.date_naive();
152-
153-
while current_date <= end_date {
154-
// Define the start and end of the activity for this specific day
155-
let day_start = if current_date == start_time.date_naive() {
156-
start_time
157-
} else {
158-
// Start of the day (midnight)
159-
Local.from_utc_datetime(&current_date.and_hms_opt(0, 0, 0).unwrap())
160-
};
161-
162-
let day_end = if current_date == end_date {
163-
end_time
164-
} else {
165-
// End of the day (23:59:59)
166-
Local.from_utc_datetime(&current_date.and_hms_opt(23, 59, 59).unwrap())
167-
};
168-
169-
let activity = Activity::new(
170-
entries[i+1].name.clone(),
171-
day_start,
172-
day_end,
173-
false,
174-
entries[i+1].comment.clone(),
175-
);
176-
177-
activities.push(activity);
143+
// Create activity using the CURRENT entry's name
144+
// This represents what you were doing during this time span
145+
let activity = Activity::new(
146+
entries[i].name.clone(),
147+
start_time,
148+
end_time,
149+
false,
150+
entries[i].comment.clone(),
151+
);
152+
153+
activities.push(activity);
154+
}
155+
156+
// Handle the last entry separately - it represents what you're currently doing
157+
// Only add if we have the current time and it's the same day
158+
if let Some(now_time) = now {
159+
if let Some(last_entry) = entries.last() {
160+
// Skip if it's a hello entry or midnight separator
161+
if last_entry.name != HELLO_ENTRY_NAME &&
162+
!last_entry.name.starts_with(MIDNIGHT_SEPARATOR_PREFIX) {
178163

179-
// Move to the next day
180-
current_date = current_date.succ_opt().unwrap();
164+
// Only add if it's from today
165+
if last_entry.datetime.date_naive() == now_time.date_naive() {
166+
let activity = Activity::new(
167+
last_entry.name.clone(),
168+
last_entry.datetime,
169+
now_time,
170+
true, // Mark as current activity
171+
last_entry.comment.clone(),
172+
);
173+
174+
activities.push(activity);
175+
}
181176
}
182-
} else {
183-
// Regular single-day activity
184-
let activity = Activity::new(
185-
entries[i+1].name.clone(),
186-
start_time,
187-
end_time,
188-
false,
189-
entries[i+1].comment.clone(),
190-
);
191-
192-
activities.push(activity);
193177
}
194178
}
195179

0 commit comments

Comments
 (0)