|
367 | 367 | (set _year (+ _year (floordiv _month 12))) |
368 | 368 |
|
369 | 369 | (mut _result (+ (* (- _year 1970) 365) (@ _cumulativeDays (mod _month 12)))) |
370 | | - (set _result (+ |
371 | | - _result |
372 | | - (floordiv (- _year 1968) 4) |
373 | | - (* -1 (floordiv (- _year 1900) 100)) |
374 | | - (floordiv (- _year 1600) 400))) |
375 | | - (if (and |
376 | | - (= 0 (mod _year 4)) |
377 | | - (or (!= 0 (mod _year 100)) (= 0 (mod _year 400))) |
378 | | - (< (mod _month 12) 2)) |
| 370 | + (set _result (+ _result (floordiv (- _year 1968) 4) (* -1 (floordiv (- _year 1900) 100)) (floordiv (- _year 1600) 400))) |
| 371 | + (if (and (= 0 (mod _year 4)) (or (!= 0 (mod _year 100)) (= 0 (mod _year 400))) (< (mod _month 12) 2)) |
379 | 372 | (set _result (- _result 1))) |
380 | | - (let _tzOffset (if (nil? _tz) 0 (* -1 (utcOffsetMinutes _tz)))) |
381 | | - (+ (* (+ (* (+ (* (+ _result _day -1) 24) _hour (if _dst? -1 0)) 60) _minute _tzOffset) 60) _second (/ _millisecond 1000)) })) |
| 373 | + (let _tzOffset |
| 374 | + (if (nil? _tz) |
| 375 | + 0 |
| 376 | + (* -1 (utcOffsetMinutes _tz)))) |
| 377 | + (+ |
| 378 | + (* |
| 379 | + (+ |
| 380 | + (* |
| 381 | + (+ |
| 382 | + (* (+ _result _day -1) 24) |
| 383 | + _hour |
| 384 | + (if _dst? |
| 385 | + -1 |
| 386 | + 0)) |
| 387 | + 60) |
| 388 | + _minute |
| 389 | + _tzOffset) |
| 390 | + 60) |
| 391 | + _second |
| 392 | + (/ _millisecond 1000)) })) |
382 | 393 |
|
383 | 394 | # @brief Precomputed timestamp of 1/1/0000 at 00H 00M 00.000s |
384 | 395 | # @author https://github.com/SuperFola |
|
416 | 427 | # (print (datetime:toUTCTimestamp (datetime:asUTCDate t)) # 1779908523.389 |
417 | 428 | # =end |
418 | 429 | # @author https://github.com/SuperFola |
419 | | -(let toUTCTimestamp (fun (_date) |
420 | | - (makeUTCTimestamp |
| 430 | +(let toUTCTimestamp (fun (_date) (makeUTCTimestamp |
421 | 431 | (dict:get _date "year") |
422 | 432 | (dict:get _date "month") |
423 | 433 | (dict:get _date "day") |
|
448 | 458 | # # {millisecond: 913, second: 24, minute: 15, hour: 17, day: 26, month: 5, year: 2026, week_day: 2, year_day: 145, is_dst: false} |
449 | 459 | # =end |
450 | 460 | # @author https://github.com/SuperFola |
451 | | -(let asUTCDate (fun (_time) |
452 | | - (builtin__time:asUTCDate _time))) |
| 461 | +(let asUTCDate (fun (_time) (builtin__time:asUTCDate _time))) |
453 | 462 |
|
454 | 463 | # @brief Add a number of seconds to a timestamp |
455 | 464 | # @param _time Number, timestamp |
|
544 | 553 | # @author https://github.com/SuperFola |
545 | 554 | (let atStartOfDay (fun (_time) { |
546 | 555 | (let _date (asUTCDate _time)) |
547 | | - (dict:update! _date (dict "millisecond" 0 "second" 0 "minute" 0 "hour" 0)) |
| 556 | + (dict:update! |
| 557 | + _date |
| 558 | + (dict |
| 559 | + "millisecond" 0 |
| 560 | + "second" 0 |
| 561 | + "minute" 0 |
| 562 | + "hour" 0)) |
548 | 563 | (toUTCTimestamp _date) })) |
549 | 564 |
|
550 | 565 | # @brief Put a given timestamp at the end of the day, at 23H 59M 59.999s |
551 | 566 | # @param _time Number, timestamp |
552 | 567 | # @author https://github.com/SuperFola |
553 | 568 | (let atEndOfDay (fun (_time) { |
554 | 569 | (let _date (asUTCDate _time)) |
555 | | - (dict:update! _date (dict "millisecond" 999 "second" 59 "minute" 59 "hour" 23)) |
| 570 | + (dict:update! |
| 571 | + _date |
| 572 | + (dict |
| 573 | + "millisecond" 999 |
| 574 | + "second" 59 |
| 575 | + "minute" 59 |
| 576 | + "hour" 23)) |
556 | 577 | (toUTCTimestamp _date) })) |
557 | 578 |
|
558 | 579 | # @brief Compute today's date, at 00H 00M 00s |
|
609 | 630 | # @brief Create a delta from a number of seconds, as a Dict with the same shape as `datetime:asUTCDate` |
610 | 631 | # @param _time Number, number of seconds |
611 | 632 | # @author https://github.com/SuperFola |
612 | | -(let asDelta (fun (_time) |
613 | | - (delta 0 _time))) |
| 633 | +(let asDelta (fun (_time) (delta 0 _time))) |
614 | 634 |
|
615 | 635 | # @brief Convert a delta or a date to a number of seconds |
616 | 636 | # @param _date Dict, with the same shape as `datetime:asUTCDate` |
|
630 | 650 | # @param _time Number, timestamp |
631 | 651 | # @param _delta Dict, with the same shape as `datetime:asUTCDate` |
632 | 652 | # @author https://github.com/SuperFola |
633 | | -(let plusDelta (fun (_time _delta) |
634 | | - (+ _time (asSeconds _delta)))) |
| 653 | +(let plusDelta (fun (_time _delta) (+ _time (asSeconds _delta)))) |
635 | 654 |
|
636 | 655 | # @brief Subtract a delta to a timestamp |
637 | 656 | # @param _time Number, timestamp |
638 | 657 | # @param _delta Dict, with the same shape as `datetime:asUTCDate` |
639 | 658 | # @author https://github.com/SuperFola |
640 | | -(let minusDelta (fun (_time _delta) |
641 | | - (- _time (asSeconds _delta)))) |
| 659 | +(let minusDelta (fun (_time _delta) (- _time (asSeconds _delta)))) |
642 | 660 |
|
643 | 661 | # @brief Return the year component of a timestamp |
644 | 662 | # @param _time Number, timestamp |
|
689 | 707 | # @param _year Number, year |
690 | 708 | # @author https://github.com/SuperFola |
691 | 709 | (let leapYear? (fun (_year) { |
692 | | - (or |
693 | | - (and (= 0 (mod _year 4)) (!= 0 (mod _year 100))) |
694 | | - (= 0 (mod _year 400))) })) |
| 710 | + (or (and (= 0 (mod _year 4)) (!= 0 (mod _year 100))) (= 0 (mod _year 400))) })) |
695 | 711 |
|
696 | 712 | # @brief Return the number of days in the month a timestamp is at |
697 | 713 | # @param _time Number, timestamp |
|
723 | 739 | (assert (not (nil? _minutes)) "Unknown timezone") |
724 | 740 | (let _hoursToUtc (abs (floordiv _minutes 60))) |
725 | 741 | (let _minToUtc (mod _minutes 60)) |
726 | | - (format "{}{:02}:{:02}" (if (< _minutes 0) "-" "+") _hoursToUtc _minToUtc) })) |
| 742 | + (format |
| 743 | + "{}{:02}:{:02}" |
| 744 | + (if (< _minutes 0) |
| 745 | + "-" |
| 746 | + "+") |
| 747 | + _hoursToUtc |
| 748 | + _minToUtc) })) |
727 | 749 |
|
728 | 750 | # @brief Convert a timestamp to its ISO8601 representation: 2007-04-05T12:34:56.789Z |
729 | 751 | # @param _time Number, timestamp |
|
0 commit comments