|
| 1 | +// Copyright © 2023-2024 DateTime (DTT) library. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +// See LICENSE-APACHE.md and LICENSE-MIT.md in the repository root for full license information. |
| 4 | + |
1 | 5 | #[cfg(test)]
|
2 | 6 | mod tests {
|
| 7 | + use dtt::is_valid; |
3 | 8 | use dtt::DateTime;
|
4 | 9 | use dtt::*;
|
5 | 10 |
|
@@ -276,4 +281,66 @@ mod tests {
|
276 | 281 | // Pass an input that cannot be parsed as an i32 to trigger the Err(_) branch.
|
277 | 282 | assert!(!integer("not_a_number"));
|
278 | 283 | }
|
| 284 | + |
| 285 | + #[test] |
| 286 | + fn test_dtt_now() { |
| 287 | + use dtt::dtt_now; |
| 288 | + let now = dtt_now!(); |
| 289 | + println!("Current date and time: {}", now.iso_8601); |
| 290 | + } |
| 291 | + |
| 292 | + #[test] |
| 293 | + fn test_dtt_parse() { |
| 294 | + let input = "2022-01-01T12:00:00+01:00"; |
| 295 | + match dtt_parse!(input) { |
| 296 | + Ok(date) => { |
| 297 | + assert_eq!(date.year, 2022); |
| 298 | + assert_eq!(date.month.parse::<u8>().unwrap(), 1); |
| 299 | + assert_eq!(date.day, 1); |
| 300 | + assert_eq!(date.hour, 12); |
| 301 | + assert_eq!(date.minute, 0); |
| 302 | + assert_eq!(date.second, 0); |
| 303 | + } |
| 304 | + Err(err) => panic!("Parsing failed: {}", err), |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + #[test] |
| 309 | + fn test_dtt_add_days() { |
| 310 | + let dt = DateTime::parse("2023-01-01T12:00:00+00:00").unwrap(); |
| 311 | + let future_date = dtt_add_days!(dt, 5); |
| 312 | + assert_eq!(future_date.day, 6); |
| 313 | + } |
| 314 | + |
| 315 | + #[test] |
| 316 | + fn test_dtt_diff_hours() { |
| 317 | + let dt1 = "2023-01-01T12:00:00+00:00"; |
| 318 | + let dt2 = "2023-01-02T12:00:00+00:00"; |
| 319 | + let hours_difference = dtt_diff_hours!(dt1, dt2); |
| 320 | + assert!(hours_difference > 23.to_string()); |
| 321 | + } |
| 322 | + |
| 323 | + #[test] |
| 324 | + fn test_dtt_diff_minutes() { |
| 325 | + let dt1 = "2023-01-01T12:00:00+00:00"; |
| 326 | + let dt2 = "2023-01-02T12:00:00+00:00"; |
| 327 | + let minutes_difference = dtt_diff_minutes!(dt1, dt2); |
| 328 | + assert!(minutes_difference > 60.to_string()); |
| 329 | + } |
| 330 | + |
| 331 | + #[test] |
| 332 | + fn test_dtt_diff_seconds() { |
| 333 | + let dt1 = "1640995200"; // Unix timestamp for 2023-01-01T12:00:00Z |
| 334 | + let dt2 = "1640995230"; // Unix timestamp for 2023-01-01T12:00:30Z |
| 335 | + let seconds_difference = dtt_diff_seconds!(dt1, dt2); |
| 336 | + assert_eq!(seconds_difference, "30"); |
| 337 | + } |
| 338 | + |
| 339 | + #[test] |
| 340 | + fn test_dtt_diff_days() { |
| 341 | + let dt1 = "1640995200"; // Unix timestamp for 2023-01-01T00:00:00Z |
| 342 | + let dt2 = "1641081600"; // Unix timestamp for 2023-01-02T00:00:00Z |
| 343 | + let days_difference = dtt_diff_days!(dt1, dt2); |
| 344 | + assert_eq!(days_difference, 1); |
| 345 | + } |
279 | 346 | }
|
0 commit comments