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
//! [Fakedate](https://gitlab.com/ufoot/fakedate) generates pseudo-random dates and times.
//!
//! It is written in [Rust](https://www.rust-lang.org/)
//! and is mostly a toy project to ramp up on the language.
//! It might however be useful. Use at your own risk.
//!
//! ![Fakedate icon](https://gitlab.com/ufoot/fakedate/raw/master/fakedate.png)

pub mod options;

extern crate chrono;
extern crate rand;

use chrono::naive::*;
use chrono::*;
use rand::Rng;

/// Generate a which is during last week-end.
///
/// ```
/// use chrono::offset;
/// use fakedate::week_end_date;
///
/// let _date = week_end_date(offset::Local::now());
/// ```
pub fn week_end_date(now: chrono::DateTime<Local>) -> chrono::DateTime<Local> {
    let naive_now = now.naive_local();
    let delta_days = match naive_now.date().weekday() {
        Weekday::Sat => 0,
        Weekday::Sun => 1,
        Weekday::Mon => 2,
        Weekday::Tue => 3,
        Weekday::Wed => 4,
        Weekday::Thu => 5,
        Weekday::Fri => 6,
    };
    let naive_d0 = NaiveDateTime::from_timestamp(naive_now.timestamp() - delta_days * 3600 * 24, 0)
        .date()
        .and_time(NaiveTime::from_hms(0, 0, 0));
    let mut delta = naive_now - naive_d0;
    delta = delta / 5;
    delta = delta
        + if delta < Duration::hours(15) {
            Duration::hours(7)
        } else {
            Duration::hours(14)
        };
    let new_now = naive_d0 + delta;
    let new_date = new_now.date();
    let new_time = new_now.time();
    offset::Local
        .from_local_date(&new_date)
        .and_time(new_time)
        .unwrap()
}

/// Generate a random date for a given year.
///
/// ```
/// use fakedate::random_year_date;
///
/// let _date = random_year_date(Some(2020));
/// ```
pub fn random_year_date(year: Option<i32>) -> chrono::DateTime<Local> {
    let mut rng = rand::thread_rng();
    let y = year.unwrap_or(offset::Local::now().year());
    let o = rng.gen_range(0, 365 as u32);
    let h = rng.gen_range(0, 24 as u32);
    let m = rng.gen_range(0, 60 as u32);
    let s = rng.gen_range(0, 60 as u32);
    let date = NaiveDate::from_yo(y, o);
    let time = NaiveTime::from_hms(h, m, s);
    offset::Local.from_local_date(&date).and_time(time).unwrap()
}

pub fn generate(cfg: &options::Config) -> String {
    let d = match cfg.week_end {
        true => week_end_date(offset::Local::now()),
        false => random_year_date(cfg.year),
    };
    d.to_rfc2822()
}

/// Generate a date with the given options, output it to stdout.
///
/// ```
/// use fakedate::{options, run};
///
/// run(&options::Config::parse());
/// ```
pub fn run(cfg: &options::Config) {
    let txt = generate(cfg);
    if cfg.newline {
        println!("{}", txt)
    } else {
        print!("{}", txt)
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use std::collections::HashMap;

    #[test]
    fn test_random_year_date() {
        assert_eq!(2020, random_year_date(Some(2020)).year());
        assert_eq!(offset::Local::now().year(), random_year_date(None).year());
    }

    #[test]
    fn test_week_end_date() {
        struct TestDate {
            week_num: u32,
            week_day: Weekday,
            h: u32,
            m: u32,
            s: u32,
        };
        struct TestCase {
            input: TestDate,
            expected_output: TestDate,
        }
        let mut test_cases: HashMap<&str, TestCase> = HashMap::new();

        test_cases.insert(
            "a basic saturday",
            TestCase {
                input: {
                    TestDate {
                        week_num: 5,
                        week_day: Weekday::Sat,
                        h: 10,
                        m: 10,
                        s: 10,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sat,
                    h: 9,
                    m: 2,
                    s: 2,
                },
            },
        );

        test_cases.insert(
            "an early saturday",
            TestCase {
                input: {
                    TestDate {
                        week_num: 5,
                        week_day: Weekday::Sat,
                        h: 2,
                        m: 3,
                        s: 4,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sat,
                    h: 7,
                    m: 24,
                    s: 36,
                },
            },
        );

        test_cases.insert(
            "a late saturday",
            TestCase {
                input: {
                    TestDate {
                        week_num: 5,
                        week_day: Weekday::Sat,
                        h: 22,
                        m: 30,
                        s: 0,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sat,
                    h: 11,
                    m: 30,
                    s: 00,
                },
            },
        );

        test_cases.insert(
            "a sunday noon",
            TestCase {
                input: {
                    TestDate {
                        week_num: 5,
                        week_day: Weekday::Sun,
                        h: 12,
                        m: 0,
                        s: 0,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sat,
                    h: 14,
                    m: 12,
                    s: 00,
                },
            },
        );

        test_cases.insert(
            "a monday noon",
            TestCase {
                input: {
                    TestDate {
                        week_num: 6,
                        week_day: Weekday::Mon,
                        h: 12,
                        m: 0,
                        s: 0,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sat,
                    h: 19,
                    m: 00,
                    s: 00,
                },
            },
        );

        test_cases.insert(
            "a tuesday aft",
            TestCase {
                input: {
                    TestDate {
                        week_num: 6,
                        week_day: Weekday::Tue,
                        h: 16,
                        m: 30,
                        s: 5,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sun,
                    h: 7,
                    m: 42,
                    s: 1,
                },
            },
        );

        test_cases.insert(
            "a wed morning",
            TestCase {
                input: {
                    TestDate {
                        week_num: 6,
                        week_day: Weekday::Wed,
                        h: 9,
                        m: 17,
                        s: 45,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sun,
                    h: 11,
                    m: 3,
                    s: 33,
                },
            },
        );

        test_cases.insert(
            "a thu morning",
            TestCase {
                input: {
                    TestDate {
                        week_num: 6,
                        week_day: Weekday::Thu,
                        h: 9,
                        m: 17,
                        s: 45,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sun,
                    h: 15,
                    m: 51,
                    s: 33,
                },
            },
        );

        test_cases.insert(
            "a fri evening",
            TestCase {
                input: {
                    TestDate {
                        week_num: 6,
                        week_day: Weekday::Fri,
                        h: 23,
                        m: 55,
                        s: 15,
                    }
                },
                expected_output: TestDate {
                    week_num: 5,
                    week_day: Weekday::Sun,
                    h: 23,
                    m: 35,
                    s: 3,
                },
            },
        );

        for (key, test_case) in test_cases.into_iter() {
            let input_date = offset::Local
                .from_local_date(&NaiveDate::from_isoywd(
                    2020,
                    test_case.input.week_num,
                    test_case.input.week_day,
                ))
                .unwrap()
                .and_hms(test_case.input.h, test_case.input.m, test_case.input.s);
            let expected_output_date = offset::Local
                .from_local_date(&NaiveDate::from_isoywd(
                    2020,
                    test_case.expected_output.week_num,
                    test_case.expected_output.week_day,
                ))
                .unwrap()
                .and_hms(
                    test_case.expected_output.h,
                    test_case.expected_output.m,
                    test_case.expected_output.s,
                );
            let output_date = week_end_date(input_date);
            assert_eq!(
                format!("{}: {}", key, expected_output_date.to_rfc2822()),
                format!("{}: {}", key, output_date.to_rfc2822())
            );
        }
    }
}