time_duration_api 0.1.9

This crate provides a time and duration manipulation API for Rust projects.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
pub mod time_utils {
    use chrono::{DateTime, FixedOffset, NaiveDateTime, Utc};
    use serde::{Deserialize, Serialize};
    use std::{
        cmp::{Ordering, PartialOrd},
        fmt,
        ops::{Add, Div, Mul, Sub},
        str::FromStr,
        time::{Duration, SystemTime},
    };

    // Custom Error Type
    #[derive(Debug, Clone)]
    pub enum TimeError {
        InvalidTime,
        InvalidTimeFormat(String),
        InvalidTimezoneFormat(String),
        ParseError(String), // Generic parsing error
    }

    impl std::error::Error for TimeError {}

    impl fmt::Display for TimeError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                TimeError::InvalidTime => write!(f, "Invalid time"),
                TimeError::InvalidTimeFormat(msg) => write!(f, "Invalid time format: {}", msg),
                TimeError::InvalidTimezoneFormat(msg) => {
                    write!(f, "Invalid timezone format: {}", msg)
                }
                TimeError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            }
        }
    }

    // Custom Result Type
    pub type Result<T> = std::result::Result<T, TimeError>;

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Time {
        timestamp: SystemTime,
        #[serde(skip)]
        cached_utc_datetime: Option<DateTime<Utc>>, // Cache the Utc DateTime
    }

    impl Time {
        /// Creates a new Time instance with the current system time.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut now = Time::now();
        /// println!("Current time: {}", now.format("%Y-%m-%d %H:%M:%S").unwrap());
        /// ```
        pub fn now() -> Self {
            Time {
                timestamp: SystemTime::now(),
                cached_utc_datetime: None,
            }
        }

        /// Formats the time with the given format string.
        ///
        /// Returns a formatted time string or an error if time is invalid.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut time = Time::now();
        /// let formatted_time = time.format("%Y-%m-%d %H:%M:%S").unwrap();
        /// println!("Formatted time: {}", formatted_time);
        /// ```
        pub fn format(&mut self, format: &str) -> Result<String> {
            let datetime = self.get_utc_datetime()?; // Use the cached or generate DateTime
            Ok(datetime.format(format).to_string())
        }

        // Helper function to get cached or generate DateTime<Utc>
        fn get_utc_datetime(&mut self) -> Result<DateTime<Utc>> {
            if let Some(cached) = self.cached_utc_datetime {
                return Ok(cached);
            }

            let duration = self
                .timestamp
                .duration_since(SystemTime::UNIX_EPOCH)
                .map_err(|_| TimeError::InvalidTime)?;

            let datetime = DateTime::<Utc>::from_naive_utc_and_offset(
                NaiveDateTime::from_timestamp_opt(
                    duration.as_secs() as i64,
                    duration.subsec_nanos(),
                )
                .ok_or(TimeError::InvalidTime)?,
                Utc,
            );

            self.cached_utc_datetime = Some(datetime); // Cache the DateTime
            Ok(datetime)
        }
        /// Formats the time with a given format string and timezone.
        ///
        /// Returns a formatted time string or an error if time or timezone is invalid.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut time = Time::now();
        /// let formatted_time = time.format_with_timezone("%Y-%m-%d %H:%M:%S", "+05:30").unwrap();
        /// println!("Formatted time in IST: {}", formatted_time);
        /// ```
        pub fn format_with_timezone(&mut self, format: &str, timezone: &str) -> Result<String> {
            let datetime = self.get_utc_datetime()?;
            let tz: FixedOffset = timezone
                .parse()
                .map_err(|_| TimeError::InvalidTimezoneFormat(timezone.to_string()))?;
            Ok(datetime.with_timezone(&tz).format(format).to_string())
        }

        /// Gets the timestamp in seconds.
        ///
        /// Returns the timestamp as a u64 or an error if time is invalid.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut time = Time::now();
        /// let timestamp = time.timestamp().unwrap();
        /// println!("Timestamp: {}", timestamp);
        /// ```
        pub fn timestamp(&self) -> Result<u64> {
            self.timestamp
                .duration_since(SystemTime::UNIX_EPOCH)
                .map(|duration| duration.as_secs())
                .map_err(|_| TimeError::InvalidTime)
        }

        /// Adds a custom duration to the current time.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::{Time, CustomDuration};
        /// let mut time = Time::now();
        /// let mut duration = CustomDuration::from_secs(3600); // 1 hour
        /// let mut future_time = time.add_duration(&duration);
        /// println!("Future time: {}", future_time.format("%Y-%m-%d %H:%M:%S").unwrap());
        /// ```
        pub fn add_duration(&self, duration: &CustomDuration) -> Self {
            Time {
                timestamp: self.timestamp + duration.duration,
                cached_utc_datetime: None,
            }
        }

        /// Subtracts a custom duration from the current time.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::{Time, CustomDuration};
        /// let mut time = Time::now();
        /// let mut duration = CustomDuration::from_secs(3600); // 1 hour
        /// let mut past_time = time.sub_duration(&duration);
        /// println!("Past time: {}", past_time.format("%Y-%m-%d %H:%M:%S").unwrap());
        /// ```
        pub fn sub_duration(&self, duration: &CustomDuration) -> Self {
            Time {
                timestamp: self.timestamp - duration.duration,
                cached_utc_datetime: None,
            }
        }

        /// Converts the time to a specific timezone.
        ///
        /// Returns the time string in the new timezone or an error.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut time = Time::now();
        /// let ist_time = time.to_timezone("+05:30").unwrap();
        /// println!("Time in IST: {}", ist_time);
        /// ```
        pub fn to_timezone(&mut self, timezone: &str) -> Result<String> {
            let datetime = self.get_utc_datetime()?;
            let tz: FixedOffset = timezone
                .parse()
                .map_err(|_| TimeError::InvalidTimezoneFormat(timezone.to_string()))?;
            Ok(datetime.with_timezone(&tz).to_string())
        }

        /// Creates a Time instance from a formatted time string.
        ///
        /// Returns the time or an error if the format is invalid.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::Time;
        /// let mut time = Time::from_str("2023-09-20 10:30:00+00:00", "%Y-%m-%d %H:%M:%S%:z").unwrap();
        /// println!("Parsed time: {}", time.format("%Y-%m-%d %H:%M:%S").unwrap());
        /// ```
        pub fn from_str(time_str: &str, format: &str) -> Result<Self> {
             match DateTime::parse_from_str(time_str, format) {
                Ok(dt) => Ok(Time {
                    timestamp: SystemTime::from(dt),
                    cached_utc_datetime: None,
                }),
                Err(e) => Err(TimeError::InvalidTimeFormat(format!(
                    "Failed to parse '{}' with format '{}': {}",
                    time_str, format, e
                ))),
            }
        }
    }

    impl fmt::Display for Time {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self.timestamp.duration_since(SystemTime::UNIX_EPOCH) {
                Ok(duration) => {
                    let datetime = DateTime::<Utc>::from_naive_utc_and_offset(
                        NaiveDateTime::from_timestamp_opt(
                            duration.as_secs() as i64,
                            duration.subsec_nanos(),
                        )
                        .unwrap(),
                        Utc,
                    );
                    write!(f, "{}", datetime.format("%Y-%m-%d %H:%M:%S"))
                }
                Err(_) => write!(f, "Invalid Time"),
            }
        }
    }

    #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
    pub struct CustomDuration {
        duration: Duration,
    }

    impl CustomDuration {
        /// Creates a CustomDuration from a number of seconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_secs(60);
        /// println!("Duration: {}", duration.format_human_readable());
        /// ```
        pub fn from_secs(secs: u64) -> Self {
            CustomDuration {
                duration: Duration::from_secs(secs),
            }
        }

        /// Creates a CustomDuration from a number of milliseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_millis(1000);
        /// println!("Duration: {}", duration.format_human_readable());
        /// ```
        pub fn from_millis(millis: u64) -> Self {
            CustomDuration {
                duration: Duration::from_millis(millis),
            }
        }

        /// Creates a CustomDuration from a number of microseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_micros(1000000);
        /// println!("Duration: {}", duration.format_human_readable());
        /// ```
        pub fn from_micros(micros: u64) -> Self {
            CustomDuration {
                duration: Duration::from_micros(micros),
            }
        }

        /// Creates a CustomDuration from a number of nanoseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_nanos(1000000000);
        /// println!("Duration: {}", duration.format_human_readable());
        /// ```
        pub fn from_nanos(nanos: u64) -> Self {
            CustomDuration {
                duration: Duration::from_nanos(nanos),
            }
        }

        /// Creates a CustomDuration from a human-readable string (e.g., "1h 30m").
        ///
        /// Returns the duration or an error if the string is invalid.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_str("2h 30m").unwrap();
        /// println!("Duration: {}", duration.format_human_readable());
        /// ```
        pub fn from_str(duration_str: &str) -> Result<Self> {
            humantime::parse_duration(duration_str)
                .map(|dur| CustomDuration { duration: dur })
                .map_err(|e| TimeError::ParseError(e.to_string()))
        }

        /// Adds two CustomDuration instances.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let dur1 = CustomDuration::from_secs(60);
        /// let dur2 = CustomDuration::from_secs(120);
        /// let sum = dur1.add(&dur2);
        /// println!("Sum: {}", sum.format_human_readable());
        /// ```
        pub fn add(&self, other: &CustomDuration) -> CustomDuration {
            CustomDuration {
                duration: self.duration + other.duration,
            }
        }

        /// Subtracts one CustomDuration from another.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let dur1 = CustomDuration::from_secs(180);
        /// let dur2 = CustomDuration::from_secs(60);
        /// let diff = dur1.sub(&dur2);
        /// println!("Difference: {}", diff.format_human_readable());
        /// ```
        pub fn sub(&self, other: &CustomDuration) -> CustomDuration {
            CustomDuration {
                duration: self.duration - other.duration,
            }
        }

        /// Multiplies a CustomDuration by a scalar value.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_secs(60);
        /// let multiplied = duration.mul(3);
        /// println!("Multiplied duration: {}", multiplied.format_human_readable());
        /// ```
        pub fn mul(&self, scalar: u32) -> CustomDuration {
            CustomDuration {
                duration: self.duration * scalar,
            }
        }

        /// Divides a CustomDuration by a scalar value.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_secs(120);
        /// let divided = duration.div(2);
        /// println!("Divided duration: {}", divided.format_human_readable());
        /// ```
        pub fn div(&self, divisor: u32) -> CustomDuration {
            CustomDuration {
                duration: self.duration / divisor,
            }
        }
        /// Rounds the duration to the nearest second.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_millis(1500);
        /// let rounded = duration.round_secs();
        /// println!("Rounded duration: {}", rounded.format_human_readable());
        /// ```
        pub fn round_secs(&self) -> CustomDuration {
            CustomDuration {
                duration: Duration::from_secs(self.duration.as_secs()),
            }
        }

        /// Returns the duration as a number of seconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_secs(60);
        /// println!("Seconds: {}", duration.as_secs());
        /// ```
        pub fn as_secs(&self) -> u64 {
            self.duration.as_secs()
        }

        /// Returns the duration as a number of milliseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_millis(1000);
        /// println!("Milliseconds: {}", duration.as_millis());
        /// ```
        pub fn as_millis(&self) -> u128 {
            self.duration.as_millis()
        }

        /// Returns the duration as a number of microseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_micros(1000000);
        /// println!("Microseconds: {}", duration.as_micros());
        /// ```
        pub fn as_micros(&self) -> u128 {
            self.duration.as_micros()
        }

        /// Returns the duration as a number of nanoseconds.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_nanos(1000000000);
        /// println!("Nanoseconds: {}", duration.as_nanos());
        /// ```
        pub fn as_nanos(&self) -> u128 {
            self.duration.as_nanos()
        }

        /// Formats the duration into a human-readable string.
        ///
        /// # Example
        ///
        /// ```
        /// use time_duration_api::time_utils::CustomDuration;
        /// let duration = CustomDuration::from_secs(3661);
        /// println!("Human readable: {}", duration.format_human_readable());
        /// ```
        pub fn format_human_readable(&self) -> String {
            humantime::format_duration(self.duration).to_string()
        }
    }

    impl Add for CustomDuration {
        type Output = Self;

        fn add(self, other: Self) -> Self {
            CustomDuration {
                duration: self.duration + other.duration,
            }
        }
    }

    impl Sub for CustomDuration {
        type Output = Self;

        fn sub(self, other: Self) -> Self {
            CustomDuration {
                duration: self.duration - other.duration,
            }
        }
    }

    impl Mul<u32> for CustomDuration {
        type Output = Self;

        fn mul(self, scalar: u32) -> Self {
            CustomDuration {
                duration: self.duration * scalar,
            }
        }
    }

    impl Div<u32> for CustomDuration {
        type Output = Self;

        fn div(self, divisor: u32) -> Self {
            CustomDuration {
                duration: self.duration / divisor,
            }
        }
    }

    impl PartialEq for CustomDuration {
        fn eq(&self, other: &Self) -> bool {
            self.duration == other.duration
        }
    }

    impl PartialOrd for CustomDuration {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            self.duration.partial_cmp(&other.duration)
        }
    }

    impl fmt::Display for CustomDuration {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", humantime::format_duration(self.duration))
        }
    }

     impl FromStr for Time {
        type Err = TimeError;
    
        fn from_str(s: &str) -> Result<Self> {
            // Define formats with and without timezone
           let formats_with_tz = [
                "%Y-%m-%d %H:%M:%S%z",
                "%Y-%m-%dT%H:%M:%S%z",
                "%Y-%m-%d %H:%M:%S.%f%z",
                "%Y-%m-%dT%H:%M:%S.%f%z",
            ];
             let _ = [
                "%Y-%m-%d %H:%M:%S",
                "%Y-%m-%dT%H:%M:%S",
                "%Y-%m-%d %H:%M:%S.%f",
                "%Y-%m-%dT%H:%M:%S.%f",
            ];
    
             for format in formats_with_tz {
                 if let Ok(dt) = DateTime::parse_from_str(s, format) {
                    return Ok(Time {
                        timestamp: SystemTime::from(dt),
                        cached_utc_datetime: None,
                    });
                }
           }
              // Attempt to parse with common formats without offset,
            if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
               return  Ok(Time {
                   timestamp: SystemTime::from(dt),
                    cached_utc_datetime: None,
                });
            }

            
             for format in  formats_with_tz {
                match DateTime::parse_from_str(s, format) {
                    Ok(dt) => {
                      return Ok(Time {
                         timestamp: SystemTime::from(dt),
                         cached_utc_datetime: None,
                       });
                     }
                Err(_)=> {}
           }
    
        }
    
       Err(TimeError::ParseError(format!("Invalid time string: {}",s)))

    }
    
    }
}