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
//!
//! Provides 14 functions to return mock date and time data.
//!
//! # Examples
//!
//! ```rust
//! use mockd::datetime;
//!
//!     let data = datetime::month(); // month: 10
//!     let data = datetime::day(); // day: 10
//!     let data = datetime::week_day(); // week_day: 6
//!     let data = datetime::year(); // year: 1986
//!     let data = datetime::hour(); // hour: 10
//!     let data = datetime::minute(); // minute: 10
//!     let data = datetime::second(); // second: 10
//!     let data = datetime::nanosecond(); // nanosecond: 959678991
//!     let data = datetime::timezone(); // timezone: SA Pacific Standard Time
//!     let data = datetime::timezone_full(); // timezone_full: (UTC-04:00) Atlantic Time    (Canada)
//!     let data = datetime::timezone_abv(); // timezone_abv: BST
//!     let data = datetime::timezone_offset(); // timezone_offset: 13
//!     let data = datetime::date_range(
//!                 "2005-04-23T19:30:12Z".to_string(),
//!                 "2019-10-02T19:30:12Z".to_string()); // date_range: 1979-01-06 23:03:10.918301212 UTC
//!     let data = datetime::date(); // date: 1979-01-06 23:03:10.918301212 UTC
//! ```
//!
//! # Feature
//!
//! Requires the "datetime" feature.
//!

use crate::misc;
use chrono::{DateTime, Datelike, NaiveDateTime, Utc};

pub(crate) mod data;

/// Generate a random month.
///
/// Returns a month by number as a string.
/// Values range from 1-12
///
/// # Example
///
/// ```rust
/// let month = mockd::datetime::month();
///
/// println!("Month: {}", month);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn month() -> String {
    misc::random::<i8>(1, 12).to_string()
}

/// Generate a random day.
///
/// A returns a valid number for a day (in any month) as a string.
/// Values range from 1-28.
///
/// # Example
///
/// ```rust
/// let day = mockd::datetime::day();
///
/// println!("Day: {}", day);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn day() -> String {
    misc::random::<i8>(1, 28).to_string()
}

/// Generate a random week day.
///
/// A returns a valid number for a week day as a string.
/// Values range from 0-6.
///
/// # Example
///
/// ```rust
/// let day = mockd::datetime::day();
///
/// println!("Day: {}", day);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn week_day() -> String {
    misc::random::<i8>(0, 6).to_string()
}

/// Generate a random year.
///
/// A returns a valid number for a year as a string.
/// Values range from 1980 to the current year..
///
/// # Example
///
/// ```rust
/// let year = mockd::datetime::year();
///
/// println!("Year: {}", year);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn year() -> String {
    misc::random::<i32>(1980, Utc::now().year()).to_string()
}

/// Generate a random hour.
///
/// A returns a valid number for an hour as a string.
/// Values range from 0-23.
///
/// # Example
///
/// ```rust
/// let hour = mockd::datetime::hour();
///
/// println!("Hour: {}", hour);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn hour() -> String {
    misc::random::<i8>(0, 23).to_string()
}

/// Generate a random minute.
///
/// A returns a valid number for a minute as a string.
/// Values range from 0-59.
///
/// # Example
///
/// ```rust
/// let minute = mockd::datetime::minute();
///
/// println!("Minute: {}", minute);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn minute() -> String {
    misc::random::<i8>(0, 59).to_string()
}

/// Generate a random second.
///
/// A returns a valid number for a second as a string.
/// Values range from 0-59.
///
/// # Example
///
/// ```rust
/// let second = mockd::datetime::second();
///
/// println!("Second: {}", second);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn second() -> String {
    misc::random::<i8>(0, 59).to_string()
}

/// Generate a random nanosecond.
///
/// A returns a valid number for a nanosecond as a string.
/// Values range from 0-999999999.
///
/// # Example
///
/// ```rust
/// let nanosecond = mockd::datetime::nanosecond();
///
/// println!("Nanosecond: {}", nanosecond);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn nanosecond() -> String {
    misc::random::<i64>(0, 999_999_999).to_string()
}

/// Generate a random timezone name.
///
/// A returns a timezone name.
/// Sample values:
/// * Mountain Standard Time
/// * Central Standard Time (Mexico)
///
/// # Example
///
/// ```rust
/// let timezone = mockd::datetime::timezone();
///
/// println!("Timezone: {}", timezone);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn timezone() -> String {
    misc::random_data(data::TEXT).to_string()
}

/// Generate a random full timezone description.
///
/// A returns a full timezone description.
/// Sample values:
/// * (UTC-07:00) Mountain Time (US & Canada)
/// * (UTC-06:00) Guadalajara, Mexico City, Monterrey
/// * (UTC-04:00) Georgetown, La Paz, Manaus, San Juan
///
/// # Example
///
/// ```rust
/// let timezone_full = mockd::datetime::timezone_full();
///
/// println!("Full timezone description: {}", timezone_full);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn timezone_full() -> String {
    misc::random_data(data::FULL).to_string()
}

/// Generate a random timezone abbreviation.
///
/// A returns a timezone abbreviation.
/// Sample values:
/// * MDT
/// * CCST
/// * GDT
///
/// # Example
///
/// ```rust
/// let timezone_abr = mockd::datetime::timezone_abv();
///
/// println!("Timezone abbreviation: {}", timezone_abr);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn timezone_abv() -> String {
    misc::random_data(data::ABR).to_string()
}

/// Generate a random timezone offset.
///
/// A returns a timezone offset as a string.
/// Sample values:
/// * -8
/// * -2.5
/// * 3
/// * 9.5
///
/// # Example
///
/// ```rust
/// let timezone_offset = mockd::datetime::timezone_offset();
///
/// println!("Timezone offset: {}", timezone_offset);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn timezone_offset() -> String {
    misc::random_data(data::OFFSET).to_string()
}

/// Generate a random date from within a range.
///
/// # Example
///
/// ```rust
/// let random_date = mockd::datetime::date_range(
///                     "2005-04-23T19:30:12Z".to_string(),
///                     "2019-10-12T19:30:12Z".to_string());
///
/// println!("Random date from range: {}",random_date);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn date_range(min: String, max: String) -> DateTime<Utc> {
    // RFC3339
    let min_nano = DateTime::parse_from_rfc3339(&min)
        .unwrap()
        .timestamp_nanos();
    let max_nano = DateTime::parse_from_rfc3339(&max)
        .unwrap()
        .timestamp_nanos();
    let ns = misc::random(min_nano, max_nano - 10_000_000_000);
    let secs = (ns / 1_000_000_000) as i64;
    let mut nsecs = (ns - (secs * 1_000_000_000)) as u32;

    // This case will cause the `NaiveDateTime::from_timestamp` function to panic.
    // So we just roll it back to the maximum allowed value.
    if nsecs >= 2_000_000_000 {
        nsecs = 1_999_999_999;
    }

    DateTime::<Utc>::from_utc(
        NaiveDateTime::from_timestamp_opt(secs, nsecs as u32).unwrap(),
        Utc,
    )
}

/// Generate a random date.
///
/// # Example
///
/// ```rust
/// let random_date = mockd::datetime::date();
///
/// println!("Random date from range: {}",random_date);
/// ```
///
/// # Feature
///
/// Requires the "datetime" feature.
///
pub fn date() -> DateTime<Utc> {
    date_range(
        "1970-01-01T16:39:57-08:00".to_string(),
        Utc::now().to_rfc3339(),
    )
}

#[cfg(test)]
mod tests {
    use crate::datetime;
    use crate::testify::exec_mes;

    #[test]
    fn timezone() {
        exec_mes("datetime::timezone", datetime::timezone);
    }

    #[test]
    fn timezone_full() {
        exec_mes("datetime::timezone_full", datetime::timezone_full);
    }

    #[test]
    fn timezone_abv() {
        exec_mes("datetime::timezone_abv", datetime::timezone_abv);
    }

    #[test]
    fn timezone_offset() {
        exec_mes("datetime::timezone_offset", datetime::timezone_offset);
    }

    #[test]
    fn date() {
        let data1 = datetime::date();
        let data2 = datetime::date();
        assert_ne!(data1, data2);
    }
}