1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! Time duration formatting utilities for user-friendly display.
//!
//! Provides formatting functions and types for converting time durations into
//! human-readable string representations used throughout the application.
//!
//! ## Features
//!
//! - **Consistent Formatting**: All time durations use the same "HH:MM" format
//! - **Safety**: Handles negative durations gracefully by treating them as zero
//! - **Performance**: Lightweight formatting with minimal allocations
//! - **Integration**: Works seamlessly with `chrono::Duration` types
//!
//! ## Usage
//!
//! ```rust
//! use kasl::libs::formatter::{format_duration, FormattedEvent};
//! use chrono::Duration;
//!
//! let duration = Duration::hours(2) + Duration::minutes(30);
//! let formatted = format_duration(&duration);
//! assert_eq!(formatted, "02:30");
//! ```
use Duration;
use ;
use ;
/// Represents a formatted time-based event for display purposes.
///
/// This structure holds string representations of event properties, making it
/// suitable for direct use with table-rendering libraries and data export
/// systems. All time values are pre-formatted for consistent display.
///
/// ## Design Rationale
///
/// Rather than storing raw time values and formatting them at display time,
/// this structure pre-formats all values to strings. This approach provides:
///
/// - **Performance**: No repeated formatting calculations
/// - **Consistency**: All instances use identical formatting
/// - **Simplicity**: Direct use in templates and display systems
/// - **Serialization**: Easy JSON/CSV export without custom formatters
///
/// ## Usage Context
///
/// This structure is primarily used for:
/// - Console table display of work intervals
/// - CSV export of time-based data
/// - JSON serialization for API responses
/// - Report generation and data visualization
///
/// ## Field Descriptions
///
/// - `id`: Sequential number for ordering and reference
/// - `start`: Formatted start time (typically "HH:MM")
/// - `end`: Formatted end time (typically "HH:MM")
/// - `duration`: Formatted duration (typically "HH:MM")
///
/// ## Examples
///
/// ```rust
/// use kasl::libs::formatter::FormattedEvent;
///
/// // Work interval representation
/// let interval = FormattedEvent {
/// id: 1,
/// start: "09:00".to_string(),
/// end: "12:00".to_string(),
/// duration: "03:00".to_string(),
/// };
///
/// // Pause representation
/// let pause = FormattedEvent {
/// id: 2,
/// start: "12:00".to_string(),
/// end: "12:30".to_string(),
/// duration: "00:30".to_string(),
/// };
/// ```
/// Formats a chrono::Duration into a standardized "HH:MM" string.
///
/// This function converts a time duration into a human-readable format
/// suitable for display in reports, tables, and user interfaces. It ensures
/// consistent formatting across the entire application.
///
/// ## Formatting Rules
///
/// - **Hours**: Always displayed with at least 2 digits (zero-padded)
/// - **Minutes**: Always displayed with exactly 2 digits (zero-padded)
/// - **Seconds**: Not displayed (rounded to nearest minute)
/// - **Negative**: Treated as zero duration ("00:00")
/// - **Overflow**: Large durations handled gracefully
///
/// ## Algorithm
///
/// 1. Extract total hours from the duration
/// 2. Extract remaining minutes (after removing full hours)
/// 3. Clamp negative values to zero
/// 4. Format with zero-padding
///
/// # Arguments
///
/// * `duration` - A reference to the chrono::Duration to format
///
/// # Returns
///
/// A String in "HH:MM" format representing the duration.
///
/// # Examples
///
/// ```rust
/// use kasl::libs::formatter::format_duration;
/// use chrono::Duration;
///
/// // Standard durations
/// assert_eq!(format_duration(&Duration::hours(8)), "08:00");
/// assert_eq!(format_duration(&Duration::minutes(90)), "01:30");
/// assert_eq!(format_duration(&Duration::minutes(45)), "00:45");
///
/// // Edge cases
/// assert_eq!(format_duration(&Duration::zero()), "00:00");
/// assert_eq!(format_duration(&Duration::hours(-1)), "00:00");
/// assert_eq!(format_duration(&Duration::hours(24)), "24:00");
/// ```
///
/// ## Performance Notes
///
/// This function is designed for frequent use and has minimal overhead:
/// - Single allocation for the result string
/// - Simple arithmetic operations only
/// - No complex parsing or validation
///
/// ## Thread Safety
///
/// This function is pure and thread-safe. It can be called concurrently
/// from multiple threads without synchronization.
/// Returns the current terminal width in columns, or `100` when unknown.
/// Truncates `s` to at most `max_width` display columns, appending `…` when cut.
///
/// Uses Unicode display width so Cyrillic and ASCII share the same budget.