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
//! Glue between mruby FFI and `Time` Rust implementation.

use spinoso_time::strftime::{
    Error::{FormattedStringTooLarge, InvalidFormatString, WriteZero},
    ASCTIME_FORMAT_STRING,
};

use crate::convert::implicitly_convert_to_int;
use crate::convert::to_str;
use crate::extn::core::string::{Encoding, String};
use crate::extn::core::time::{args::Args, subsec::Subsec, Offset, Time};
use crate::extn::prelude::*;

// Constructor
pub fn now(interp: &mut Artichoke) -> Result<Value, Error> {
    let now = Time::now()?;
    let result = Time::alloc_value(now, interp)?;
    Ok(result)
}

pub fn at(
    interp: &mut Artichoke,
    seconds: Value,
    first: Option<Value>,
    second: Option<Value>,
    third: Option<Value>,
) -> Result<Value, Error> {
    // Coerce the params to the correct place. Specifically:
    // - the options hash might not always be provided as the last argument.
    // - subseconds can be provided with an optional symbol for the type of subsec.
    //
    // ```console
    // [3.1.2] > Time.at(1)
    // => 1970-01-01 01:00:01 +0100
    // [3.1.2] > Time.at(1, 1)
    // => 1970-01-01 01:00:01.000001 +0100
    // [3.1.2] > Time.at(1, 1, :nsec)
    // => 1970-01-01 01:00:01.000000001 +0100
    // [3.1.2] > Time.at(1, in: "A")
    // => 1970-01-01 01:00:01 +0100
    // [3.1.2] > Time.at(1, 1, in: "A")
    // => 1970-01-01 01:00:01.000001 +0100
    // [3.1.2] > Time.at(1, 1, :nsec)
    // => 1970-01-01 01:00:01.000000001 +0100
    // [3.1.2] > Time.at(1, 1, :nsec, in: "A")
    // => 1970-01-01 01:00:01.000000001 +0100
    // ```

    let mut subsec = first;
    let mut subsec_unit = second;
    let mut options = third;

    // Re-position the options hash under the `options` if it exists. Calling
    // `Time.at` without the optional parameters will end up placing the
    // options hash in the incorrect parameter position.
    //
    // ```console
    // Time.at(0, in: "A")
    // #          ^--first
    // Time.at(0, 1, in: "A")
    // #             ^-- second
    // Time.at(0, 1, :nsec, in: "A")
    // #                    ^-- third
    // ```
    //
    // The below logic:
    // - ensures the third parameter is a Ruby::Hash if provided.
    // - if third param is not options, check the second parameter, if it is a
    //   Ruby::Hash then assume this is the options hash, and clear out the
    //   second parameter.
    // - if second param is not options, check the first param, if it is a
    //   Ruby::Hash then assume this is the options hash, and clear out the
    //   first parameter.
    if let Some(third_param) = third {
        if third_param.ruby_type() != Ruby::Hash {
            return Err(ArgumentError::with_message("invalid offset options").into());
        }
    } else {
        options = if let Some(second_param) = second {
            if second_param.ruby_type() == Ruby::Hash {
                subsec_unit = None;
                Some(second_param)
            } else if let Some(first_param) = first {
                if first_param.ruby_type() == Ruby::Hash {
                    subsec = None;
                    Some(first_param)
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        }
    }

    let subsec: Subsec = interp.try_convert_mut((subsec, subsec_unit))?;
    let (subsec_secs, subsec_nanos) = subsec.to_tuple();

    let seconds = implicitly_convert_to_int(interp, seconds)?
        .checked_add(subsec_secs)
        .ok_or(ArgumentError::with_message("Time too large"))?;

    let offset: Offset = if let Some(options) = options {
        let offset: Option<Offset> = interp.try_convert_mut(options)?;
        offset.unwrap_or_else(Offset::local)
    } else {
        Offset::local()
    };

    let time = Time::with_timespec_and_offset(seconds, subsec_nanos, offset)?;

    Time::alloc_value(time, interp)
}

pub fn mkutc(interp: &mut Artichoke, args: &mut [Value]) -> Result<Value, Error> {
    let args: Args = interp.try_convert_mut(args)?;

    let time = Time::utc(
        args.year,
        args.month,
        args.day,
        args.hour,
        args.minute,
        args.second,
        args.nanoseconds,
    )?;

    Time::alloc_value(time, interp)
}

pub fn mktime(interp: &mut Artichoke, args: &mut [Value]) -> Result<Value, Error> {
    let args: Args = interp.try_convert_mut(args)?;

    let time = Time::local(
        args.year,
        args.month,
        args.day,
        args.hour,
        args.minute,
        args.second,
        args.nanoseconds,
    )?;

    Time::alloc_value(time, interp)
}

// Core

pub fn to_int(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let timestamp = time.to_int();
    Ok(interp.convert(timestamp))
}

pub fn to_float(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let duration = time.to_float();
    Ok(interp.convert_mut(duration))
}

pub fn to_rational(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    // Requires `Rational`
    Err(NotImplementedError::new().into())
}

pub fn cmp(interp: &mut Artichoke, mut time: Value, mut other: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    if let Ok(other) = unsafe { Time::unbox_from_value(&mut other, interp) } {
        let cmp = time.cmp(&other);
        Ok(interp.convert(cmp as i32))
    } else {
        let mut message = b"comparison of Time with ".to_vec();
        message.extend_from_slice(interp.inspect_type_name_for_value(other).as_bytes());
        message.extend_from_slice(b" failed");
        Err(ArgumentError::from(message).into())
    }
}

pub fn eql(interp: &mut Artichoke, mut time: Value, mut other: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    if let Ok(other) = unsafe { Time::unbox_from_value(&mut other, interp) } {
        let cmp = time.eq(&other);
        Ok(interp.convert(cmp))
    } else {
        Ok(interp.convert(false))
    }
}

pub fn hash(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    Err(NotImplementedError::new().into())
}

pub fn initialize<T>(interp: &mut Artichoke, time: Value, args: T) -> Result<Value, Error>
where
    T: IntoIterator<Item = Value>,
{
    let _ = interp;
    let _ = time;
    let _ignored_while_unimplemented = args.into_iter();
    Err(NotImplementedError::new().into())
}

pub fn initialize_copy(interp: &mut Artichoke, time: Value, mut from: Value) -> Result<Value, Error> {
    let from = unsafe { Time::unbox_from_value(&mut from, interp)? };
    let result = *from;
    Time::box_into_value(result, time, interp)
}

// Mutators and converters

pub fn mutate_to_local(interp: &mut Artichoke, time: Value, offset: Option<Value>) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    let _ = offset;
    Err(NotImplementedError::new().into())
}

pub fn mutate_to_utc(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let mut obj = unsafe { Time::unbox_from_value(&mut time, interp)? };
    obj.set_utc()?;
    Ok(time)
}

pub fn as_local(interp: &mut Artichoke, time: Value, offset: Option<Value>) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    let _ = offset;
    Err(NotImplementedError::new().into())
}

pub fn as_utc(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let utc = time.to_utc()?;
    Time::alloc_value(utc, interp)
}

// Inspect

pub fn asctime(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    strftime_with_encoding(interp, time, ASCTIME_FORMAT_STRING.as_bytes(), Encoding::Utf8)
}

pub fn to_string(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    // %z will always display a +/-HHMM value, however it's expected that UTC
    // is shown if it is UTC Time.
    let format = if unsafe { Time::unbox_from_value(&mut time, interp)? }.is_utc() {
        "%Y-%m-%d %H:%M:%S UTC"
    } else {
        "%Y-%m-%d %H:%M:%S %z"
    };

    strftime_with_encoding(interp, time, format.as_bytes(), Encoding::Utf8)
}

pub fn to_array(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    // Need to implement `Convert` for timezone offset.
    let _ = interp;
    let _ = time;
    Err(NotImplementedError::new().into())
}

// Math

pub fn plus(interp: &mut Artichoke, mut time: Value, mut other: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    if unsafe { Time::unbox_from_value(&mut other, interp) }.is_ok() {
        // ```console
        // [3.1.2] > Time.now + Time.now
        // (irb):15:in `+': time + time? (TypeError)
        //         from (irb):15:in `<main>'
        //         from /usr/local/var/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        //         from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `load'
        //         from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `<main>'
        // ```
        Err(TypeError::with_message("time + time?").into())
    } else if let Ok(other) = other.try_convert_into::<f64>(interp) {
        let result = time.checked_add_f64(other)?;

        Time::alloc_value(result, interp)
    } else if let Ok(other) = implicitly_convert_to_int(interp, other) {
        let result = time.checked_add_i64(other)?;

        Time::alloc_value(result, interp)
    } else {
        Err(TypeError::with_message("can't convert into an exact number").into())
    }
}

pub fn minus(interp: &mut Artichoke, mut time: Value, mut other: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    if let Ok(other) = unsafe { Time::unbox_from_value(&mut other, interp) } {
        let result: Value = interp.convert_mut(time.to_float() - other.to_float());
        Ok(result)
    } else if let Ok(other) = implicitly_convert_to_int(interp, other) {
        let result = time.checked_sub_i64(other)?;

        Time::alloc_value(result, interp)
    } else if let Ok(other) = other.try_convert_into::<f64>(interp) {
        let result = time.checked_sub_f64(other)?;

        Time::alloc_value(result, interp)
    } else {
        Err(TypeError::with_message("can't convert into an exact number").into())
    }
}

// Coarse math

pub fn succ(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    interp.warn(b"warning: Time#succ is obsolete; use time + 1")?;
    plus(interp, time, interp.convert(1))
}

pub fn round(interp: &mut Artichoke, time: Value, num_digits: Option<Value>) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    let _ = num_digits;
    Err(NotImplementedError::new().into())
}

// Datetime

pub fn second(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let second = time.second();
    let result = interp.convert(second);
    Ok(result)
}

pub fn minute(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let minute = time.minute();
    let result = interp.convert(minute);
    Ok(result)
}

pub fn hour(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let hour = time.hour();
    let result = interp.convert(hour);
    Ok(result)
}

pub fn day(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let day = time.day();
    let result = interp.convert(day);
    Ok(result)
}

pub fn month(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let month = time.month();
    let result = interp.convert(month);
    Ok(result)
}

pub fn year(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let year = time.year();
    let result = interp.convert(year);
    Ok(result)
}

pub fn weekday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let weekday = time.day_of_week();
    let result = interp.convert(weekday);
    Ok(result)
}

pub fn year_day(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let year_day = time.day_of_year();
    let result = interp.convert(year_day);
    Ok(result)
}

pub fn is_dst(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_dst = time.is_dst();
    Ok(interp.convert(is_dst))
}

pub fn timezone(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    Err(NotImplementedError::new().into())
}

pub fn utc_offset(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    Err(NotImplementedError::new().into())
}

// Timezone mode

pub fn is_utc(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_utc = time.is_utc();
    Ok(interp.convert(is_utc))
}

// Day of week

pub fn is_sunday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_sunday = time.is_sunday();
    let result = interp.convert(is_sunday);
    Ok(result)
}

pub fn is_monday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_monday = time.is_monday();
    let result = interp.convert(is_monday);
    Ok(result)
}

pub fn is_tuesday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_tuesday = time.is_tuesday();
    let result = interp.convert(is_tuesday);
    Ok(result)
}

pub fn is_wednesday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_wednesday = time.is_wednesday();
    let result = interp.convert(is_wednesday);
    Ok(result)
}

pub fn is_thursday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_thursday = time.is_thursday();
    let result = interp.convert(is_thursday);
    Ok(result)
}

pub fn is_friday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_friday = time.is_friday();
    let result = interp.convert(is_friday);
    Ok(result)
}

pub fn is_saturday(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let is_saturday = time.is_saturday();
    let result = interp.convert(is_saturday);
    Ok(result)
}

// Unix time value

pub fn microsecond(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let microsecond = time.microseconds();
    let result = interp.convert(microsecond);
    Ok(result)
}

pub fn nanosecond(interp: &mut Artichoke, mut time: Value) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };
    let nanosecond = time.nanoseconds();
    let result = interp.convert(nanosecond);
    Ok(result)
}

pub fn subsec(interp: &mut Artichoke, time: Value) -> Result<Value, Error> {
    let _ = interp;
    let _ = time;
    // Requires `Rational`
    Err(NotImplementedError::new().into())
}

// Time format
fn strftime_with_encoding(
    interp: &mut Artichoke,
    mut time: Value,
    format: &[u8],
    encoding: Encoding,
) -> Result<Value, Error> {
    let time = unsafe { Time::unbox_from_value(&mut time, interp)? };

    let bytes: Vec<u8> = time.strftime(format).map_err(|e| {
        // InvalidFormatString is the only true ArgumentError, where as the
        // rest that can be thrown from strftime are runtime failures.
        //
        // ```console
        // [3.1.2]> Time.now.strftime("%")
        // (irb):1:in `strftime': invalid format: % (ArgumentError)
        //      from (irb):1:in `<main>'
        // 	    from /home/ben/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        // 	    from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `load'
        // 	    from /home/ben/.rbenv/versions/3.1.2/bin/irb:25:in `<main>'
        // ```
        //
        // Note: The errors which are re-thrown as RuntimeError include (but is
        // not limited to: `InvalidTime`, `FmtError(Error)`,
        // `OutOfMemory(TryReserveError)`
        #[allow(clippy::match_same_arms)]
        match e {
            InvalidFormatString => {
                let mut message = b"invalid format: ".to_vec();
                message.extend_from_slice(format);
                Error::from(ArgumentError::from(message))
            }
            FormattedStringTooLarge => {
                // TODO: This should be an `Errno::ERANGE` not an ArgumentError
                //
                // ```console
                // [3.1.2] > Time.now.strftime "%4718593m"
                // (irb):28:in `strftime': Result too large - %4718593m (Errno::ERANGE)
                //      from (irb):28:in `<main>'
                //      from /usr/local/var/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
                //      from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `load'
                //      from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `<main>'
                // ```
                let mut message = b"Result too large - ".to_vec();
                message.extend_from_slice(format);
                Error::from(ArgumentError::from(message))
            }
            WriteZero => {
                // TODO: This should be an `Errno::ERANGE` not an ArgumentError
                //
                // ```console
                // [3.1.2] > Time.now.strftime "%2147483647m"
                // (irb):28:in `strftime': Result too large - %2147483647m (Errno::ERANGE)
                //      from (irb):29:in `<main>'
                //      from /usr/local/var/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
                //      from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `load'
                //      from /usr/local/var/rbenv/versions/3.1.2/bin/irb:25:in `<main>'
                // ```
                let mut message = b"Result too large - ".to_vec();
                message.extend_from_slice(format);
                Error::from(ArgumentError::from(message))
            }
            _ => Error::from(RuntimeError::with_message("Unexpected failure")),
        }
    })?;

    let result = String::with_bytes_and_encoding(bytes, encoding);

    String::alloc_value(result, interp)
}

pub fn strftime(interp: &mut Artichoke, time: Value, format: Value) -> Result<Value, Error> {
    let mut format = to_str(interp, format)?;

    let format = unsafe { String::unbox_from_value(&mut format, interp)? };

    strftime_with_encoding(interp, time, &format, format.encoding())
}