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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! # `DateTime` Utilities
//!
//! This module provides comprehensive date and time utilities with chrono integration,
//! including serialization support via our serde module and parallel processing capabilities.
//!
//! ## Overview
//!
//! The `datetime` module offers:
//! - **Basic Time Operations**: Current time, formatting, parsing
//! - **Serialization Integration**: JSON serialization for timestamps
//! - **Batch Processing**: Parallel parsing of multiple dates
//! - **Timezone Utilities**: Basic timezone conversion helpers
//!
//! ## Usage Patterns
//!
//! ### Basic `DateTime` Operations
//! ```rust
//! use trash_analyzer::sys::datetime::*;
//!
//! let now = current_utc_time();
//! let formatted = format_datetime(&now);
//! let parsed = parse_datetime(&formatted).unwrap();
//! ```
//!
//! ### Serialization with Serde
//! ```rust
//! use trash_analyzer::sys::datetime::{serialize_timestamp, deserialize_timestamp};
//!
//! let timestamp = current_utc_time();
//! let json = serialize_timestamp(×tamp).unwrap();
//! let restored = deserialize_timestamp(&json).unwrap();
//! ```
//!
//! ### Batch Processing
//! ```rust
//! use trash_analyzer::sys::datetime::batch_parse_dates;
//!
//! let date_strings = vec!["2023-01-01", "2023-01-02", "2023-01-03"];
//! let dates = batch_parse_dates(&date_strings).unwrap();
//! ```
use crateparallel;
use crateserde;
use ;
/// Comprehensive date and time utilities with chrono integration.
///
/// This module provides robust date/time handling with serialization support,
/// parallel processing capabilities, and timezone utilities. Built on the
/// battle-tested chrono library for reliable temporal operations.
///
/// ## Features
///
/// - **Current Time**: UTC time retrieval with high precision
/// - **Formatting & Parsing**: RFC 3339 compliant date/time handling
/// - **Serialization**: JSON serialization for timestamps and dates
/// - **Batch Processing**: Parallel parsing of multiple date strings
/// - **Timezone Support**: Basic timezone offset conversions
/// - **Type Safety**: Compile-time guarantees for date operations
///
/// ## Examples
///
/// ### Time Operations
/// ```rust
/// use trash_utilities::sys::datetime::*;
/// use std::thread;
/// use std::time::Duration;
///
/// let start = current_utc_time();
/// thread::sleep(Duration::from_millis(100));
/// let end = current_utc_time();
///
/// let duration = end.signed_duration_since(start);
/// println!("Elapsed: {} milliseconds", duration.num_milliseconds());
/// ```
///
/// ### Date Formatting and Parsing
/// ```rust
/// use trash_utilities::sys::datetime::*;
///
/// // Format current time
/// let now = current_utc_time();
/// let rfc3339 = format_datetime(&now);
/// println!("RFC 3339: {}", rfc3339);
///
/// // Parse various formats
/// let parsed_datetime = parse_datetime("2023-12-25T00:00:00Z").unwrap();
/// let parsed_date = parse_date("2023-12-25").unwrap();
///
/// println!("Parsed datetime: {}", parsed_datetime);
/// println!("Parsed date: {}", parsed_date);
/// ```
///
/// ### Configuration with Timestamps
/// ```rust
/// use trash_utilities::sys::datetime::*;
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Event {
/// name: String,
/// created_at: DateTime<Utc>,
/// scheduled_for: Option<NaiveDate>,
/// }
///
/// let event = Event {
/// name: "System Backup".to_string(),
/// created_at: current_utc_time(),
/// scheduled_for: Some(parse_date("2024-01-01").unwrap()),
/// };
///
/// // Serialize with timestamp
/// let json = serialize_timestamp(&event.created_at).unwrap();
/// println!("Timestamp JSON: {}", json);
///
/// // Deserialize back
/// let restored: DateTime<Utc> = deserialize_timestamp(&json).unwrap();
/// assert_eq!(event.created_at, restored);
/// ```
///
/// ### Batch Date Processing
/// ```rust
/// use trash_utilities::sys::datetime::batch_parse_dates;
///
/// // Process multiple dates in parallel
/// let date_strings = vec![
/// "2023-01-01",
/// "2023-01-15",
/// "2023-02-01",
/// "2023-02-14",
/// "2023-03-01",
/// ];
///
/// let dates = batch_parse_dates(&date_strings).unwrap();
/// println!("Parsed {} dates", dates.len());
///
/// // Find specific dates
/// let january_dates: Vec<_> = dates.iter()
/// .filter(|d| d.month() == 1)
/// .collect();
/// println!("January dates: {}", january_dates.len());
/// ```
///
/// ### Timezone Conversions
/// ```rust
/// use trash_utilities::sys::datetime::*;
///
/// let utc_time = current_utc_time();
///
/// // Convert to different timezones
/// let est_time = convert_timezone_offset(&utc_time, -5); // EST (UTC-5)
/// let pst_time = convert_timezone_offset(&utc_time, -8); // PST (UTC-8)
/// let jst_time = convert_timezone_offset(&utc_time, 9); // JST (UTC+9)
///
/// println!("UTC: {}", utc_time);
/// println!("EST: {}", est_time);
/// println!("PST: {}", pst_time);
/// println!("JST: {}", jst_time);
/// ```
///
/// ### Error Handling
/// ```rust
/// use trash_utilities::sys::datetime::*;
///
/// // Handle parsing errors gracefully
/// match parse_datetime("invalid-date") {
/// Ok(dt) => println!("Parsed: {}", dt),
/// Err(e) => println!("Parse error: {}", e),
/// }
///
/// match parse_date("2023-13-45") { // Invalid date
/// Ok(date) => println!("Parsed: {}", date),
/// Err(e) => println!("Date parse error: {}", e),
/// }
///
/// // Batch processing with error handling
/// let mixed_dates = vec!["2023-01-01", "invalid", "2023-01-03"];
/// match batch_parse_dates(&mixed_dates) {
/// Ok(dates) => println!("All dates parsed: {}", dates.len()),
/// Err(e) => println!("Batch parse failed: {}", e),
/// }
/// ```
/// Gets the current UTC time.
///
/// This function returns the current time in UTC using `chrono::Utc::now()`.
///
/// # Returns
/// A `DateTime<Utc>` representing the current UTC time.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::current_utc_time;
/// use chrono::{DateTime, Utc};
///
/// let now: DateTime<Utc> = current_utc_time();
/// println!("Current time: {}", now);
/// ```
/// Formats a `DateTime<Utc>` to an ISO 8601 string.
///
/// This function uses RFC 3339 format, which is a profile of ISO 8601.
///
/// # Parameters
/// - `dt`: The `DateTime<Utc>` to format.
///
/// # Returns
/// A `String` containing the formatted date and time.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::{current_utc_time, format_datetime};
/// use chrono::DateTime;
///
/// let now = current_utc_time();
/// let formatted = format_datetime(&now);
/// println!("Formatted time: {}", formatted);
/// ```
/// Parses a date/time string in RFC 3339 format.
///
/// # Parameters
/// - `s`: The string to parse.
///
/// # Returns
/// - `Ok(DateTime<Utc>)` if parsing succeeds.
/// - `Err(chrono::ParseError)` if parsing fails.
///
/// # Errors
/// Returns a `chrono::ParseError` if the string is not a valid RFC 3339 date/time.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::parse_datetime;
///
/// let dt = parse_datetime("2023-01-01T12:00:00Z").unwrap();
/// println!("Parsed datetime: {}", dt);
/// ```
/// Parses a date string in YYYY-MM-DD format.
///
/// # Parameters
/// - `s`: The string to parse.
///
/// # Returns
/// - `Ok(NaiveDate)` if parsing succeeds.
/// - `Err(chrono::ParseError)` if parsing fails.
///
/// # Errors
/// Returns a `chrono::ParseError` if the string is not in YYYY-MM-DD format.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::parse_date;
/// use chrono::NaiveDate;
///
/// let date = parse_date("2023-01-01").unwrap();
/// println!("Parsed date: {}", date);
/// ```
/// Serializes a `DateTime<Utc>` to a JSON string using our serde module.
///
/// # Parameters
/// - `dt`: The `DateTime<Utc>` to serialize.
///
/// # Returns
/// - `Ok(String)` containing the JSON representation.
/// - `Err` if serialization fails.
///
/// # Errors
/// Returns a boxed error if JSON serialization fails.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::{current_utc_time, serialize_timestamp};
///
/// let now = current_utc_time();
/// let json = serialize_timestamp(&now).unwrap();
/// println!("Timestamp JSON: {}", json);
/// ```
/// Deserializes a JSON string to a `DateTime<Utc>` using our serde module.
///
/// # Parameters
/// - `json`: The JSON string to deserialize.
///
/// # Returns
/// - `Ok(DateTime<Utc>)` if deserialization succeeds.
/// - `Err` if deserialization fails.
///
/// # Errors
/// Returns a boxed error if JSON deserialization fails.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::deserialize_timestamp;
///
/// let json = "\"2023-01-01T12:00:00Z\"";
/// let dt = deserialize_timestamp(json).unwrap();
/// println!("Deserialized datetime: {}", dt);
/// ```
/// Parses multiple date strings in parallel using our parallel module.
///
/// This function processes a collection of date strings concurrently for improved performance
/// on large datasets.
///
/// # Parameters
/// - `date_strings`: A slice of date strings in YYYY-MM-DD format.
///
/// # Returns
/// - `Ok(Vec<NaiveDate>)` containing parsed dates in the same order.
/// - `Err` if any date parsing fails (returns the first error encountered).
///
/// # Errors
/// Returns a boxed error if any date string cannot be parsed in YYYY-MM-DD format.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::batch_parse_dates;
///
/// let date_strings = vec!["2023-01-01", "2023-01-02", "2023-01-03"];
/// let dates = batch_parse_dates(&date_strings).unwrap();
/// assert_eq!(dates.len(), 3);
/// ```
/// Converts a UTC datetime to a different timezone offset.
///
/// This is a basic utility for timezone conversion. For more complex timezone handling,
/// consider using the `chrono-tz` crate.
///
/// # Parameters
/// - `dt`: The UTC datetime to convert.
/// - `offset_hours`: The timezone offset in hours (e.g., -5 for EST, +9 for JST).
///
/// # Returns
/// A new `DateTime<Utc>` adjusted by the offset.
///
/// # Examples
/// ```rust
/// use trash_analyzer::sys::datetime::{current_utc_time, convert_timezone_offset};
///
/// let utc = current_utc_time();
/// let est = convert_timezone_offset(&utc, -5); // EST is UTC-5
/// println!("UTC: {}, EST: {}", utc, est);
/// ```