@synnaxlabs/x 0.52.3 → 0.52.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synnaxlabs/x",
3
- "version": "0.52.3",
3
+ "version": "0.52.4",
4
4
  "type": "module",
5
5
  "description": "Common Utilities for Synnax Labs",
6
6
  "repository": "https://github.com/synnaxlabs/synnax/tree/main/x/ts",
@@ -7,7 +7,7 @@
7
7
  // License, use of this software will be governed by the Apache License, Version 2.0,
8
8
  // included in the file licenses/APL.txt.
9
9
 
10
- import { describe, expect, it, test } from "vitest";
10
+ import { afterEach, beforeEach, describe, expect, it, test } from "vitest";
11
11
 
12
12
  import { binary } from "@/binary";
13
13
  import {
@@ -727,6 +727,122 @@ describe("TimeStamp", () => {
727
727
  const diff = Math.abs(Number(roundTrip.valueOf() - ts.valueOf()));
728
728
  expect(diff).toBeLessThan(1000000000); // Less than 1 second in nanoseconds
729
729
  });
730
+
731
+ describe("timezone sensitive", () => {
732
+ let originalTZ: string | undefined;
733
+ beforeEach(() => {
734
+ originalTZ = process.env.TZ;
735
+ process.env.TZ = "Pacific/Auckland";
736
+ });
737
+ afterEach(() => {
738
+ process.env.TZ = originalTZ;
739
+ });
740
+
741
+ test("localYear", () => {
742
+ // 2022-12-31T23:00:00Z -> Auckland (UTC+13): 2023-01-01T12:00:00
743
+ const ts = new TimeStamp(Date.UTC(2022, 11, 31, 23, 0, 0, 0) * 1e6);
744
+ expect(ts.year).toEqual(2022);
745
+ expect(ts.localYear).toEqual(2023);
746
+ });
747
+
748
+ test("setLocalYear", () => {
749
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 30, 20, 0) * 1e6);
750
+ const updated = ts.setLocalYear(2025);
751
+ expect(updated.localYear).toEqual(2025);
752
+ expect(updated.localMonth).toEqual(ts.localMonth);
753
+ expect(updated.localDay).toEqual(ts.localDay);
754
+ expect(updated.localHour).toEqual(ts.localHour);
755
+ });
756
+
757
+ test("localMonth", () => {
758
+ // 2022-06-30T23:00:00Z -> Auckland (UTC+12): 2022-07-01T11:00:00
759
+ const ts = new TimeStamp(Date.UTC(2022, 5, 30, 23, 0, 0, 0) * 1e6);
760
+ expect(ts.month).toEqual(5); // June (0-indexed)
761
+ expect(ts.localMonth).toEqual(6); // July (0-indexed)
762
+ });
763
+
764
+ test("setLocalMonth", () => {
765
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 30, 0, 0) * 1e6);
766
+ const updated = ts.setLocalMonth(11);
767
+ expect(updated.localMonth).toEqual(11);
768
+ expect(updated.localDay).toEqual(ts.localDay);
769
+ expect(updated.localHour).toEqual(ts.localHour);
770
+ });
771
+
772
+ test("localDay", () => {
773
+ // 2022-06-15T23:00:00Z -> Auckland (UTC+12): 2022-06-16T11:00:00
774
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 23, 0, 0, 0) * 1e6);
775
+ expect(ts.day).toEqual(15);
776
+ expect(ts.localDay).toEqual(16);
777
+ });
778
+
779
+ test("setLocalDay", () => {
780
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 30, 0, 0) * 1e6);
781
+ const updated = ts.setLocalDay(25);
782
+ expect(updated.localDay).toEqual(25);
783
+ expect(updated.localMonth).toEqual(ts.localMonth);
784
+ expect(updated.localHour).toEqual(ts.localHour);
785
+ });
786
+
787
+ test("localMinute", () => {
788
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 30, 0, 0) * 1e6);
789
+ expect(ts.localMinute).toEqual(ts.date().getMinutes());
790
+ });
791
+
792
+ test("setLocalMinute", () => {
793
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 0, 0, 0) * 1e6);
794
+ const updated = ts.setLocalMinute(45);
795
+ expect(updated.localMinute).toEqual(45);
796
+ expect(updated.localHour).toEqual(ts.localHour);
797
+ expect(updated.localSecond).toEqual(ts.localSecond);
798
+ });
799
+
800
+ test("localSecond", () => {
801
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 0, 42, 0) * 1e6);
802
+ expect(ts.localSecond).toEqual(ts.date().getSeconds());
803
+ });
804
+
805
+ test("setLocalSecond", () => {
806
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 0, 0, 0) * 1e6);
807
+ const updated = ts.setLocalSecond(30);
808
+ expect(updated.localSecond).toEqual(30);
809
+ expect(updated.localMinute).toEqual(ts.localMinute);
810
+ expect(updated.localHour).toEqual(ts.localHour);
811
+ });
812
+
813
+ test("localMillisecond", () => {
814
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 0, 0, 500) * 1e6);
815
+ expect(ts.localMillisecond).toEqual(ts.date().getMilliseconds());
816
+ });
817
+
818
+ test("setLocalMillisecond", () => {
819
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 0, 0, 0) * 1e6);
820
+ const updated = ts.setLocalMillisecond(750);
821
+ expect(updated.localMillisecond).toEqual(750);
822
+ expect(updated.localSecond).toEqual(ts.localSecond);
823
+ expect(updated.localHour).toEqual(ts.localHour);
824
+ });
825
+
826
+ test("local getters and setters round trip", () => {
827
+ const ts = new TimeStamp(Date.UTC(2022, 5, 15, 10, 30, 20, 500) * 1e6);
828
+ const roundTrip = ts
829
+ .setLocalYear(ts.localYear)
830
+ .setLocalMonth(ts.localMonth)
831
+ .setLocalDay(ts.localDay)
832
+ .setLocalHour(ts.localHour)
833
+ .setLocalMinute(ts.localMinute)
834
+ .setLocalSecond(ts.localSecond)
835
+ .setLocalMillisecond(ts.localMillisecond);
836
+
837
+ expect(roundTrip.localYear).toEqual(ts.localYear);
838
+ expect(roundTrip.localMonth).toEqual(ts.localMonth);
839
+ expect(roundTrip.localDay).toEqual(ts.localDay);
840
+ expect(roundTrip.localHour).toEqual(ts.localHour);
841
+ expect(roundTrip.localMinute).toEqual(ts.localMinute);
842
+ expect(roundTrip.localSecond).toEqual(ts.localSecond);
843
+ expect(roundTrip.localMillisecond).toEqual(ts.localMillisecond);
844
+ });
845
+ });
730
846
  });
731
847
 
732
848
  describe("remainder", () => {
@@ -398,13 +398,18 @@ export class TimeStamp
398
398
  return Number(this.valueOf());
399
399
  }
400
400
 
401
- /** @returns the integer year that the timestamp corresponds to. */
401
+ /** @returns the integer year that the timestamp corresponds to in UTC. */
402
402
  get year(): number {
403
403
  return this.date().getUTCFullYear();
404
404
  }
405
405
 
406
+ /** @returns the integer year that the timestamp corresponds to in local time. */
407
+ get localYear(): number {
408
+ return this.date().getFullYear();
409
+ }
410
+
406
411
  /**
407
- * @returns a copy of the timestamp with the year changed.
412
+ * @returns a copy of the timestamp with the year changed in UTC.
408
413
  * @param year the value to set the year to.
409
414
  */
410
415
  setYear(year: number): TimeStamp {
@@ -413,13 +418,30 @@ export class TimeStamp
413
418
  return new TimeStamp(d);
414
419
  }
415
420
 
416
- /** @returns the integer month that the timestamp corresponds to with its year. */
421
+ /**
422
+ * @returns a copy of the timestamp with the year changed in local time.
423
+ * @param year the value to set the year to.
424
+ */
425
+ setLocalYear(year: number): TimeStamp {
426
+ const d = this.date();
427
+ d.setFullYear(year);
428
+ return new TimeStamp(d);
429
+ }
430
+
431
+ /** @returns the integer month that the timestamp corresponds to within its year in
432
+ * UTC. */
417
433
  get month(): number {
418
434
  return this.date().getUTCMonth();
419
435
  }
420
436
 
437
+ /** @returns the integer month that the timestamp corresponds to within its year in
438
+ * local time. */
439
+ get localMonth(): number {
440
+ return this.date().getMonth();
441
+ }
442
+
421
443
  /**
422
- * @returns a copy of the timestamp with the month changed.
444
+ * @returns a copy of the timestamp with the month changed in UTC.
423
445
  * @param month the value to set the month to.
424
446
  */
425
447
  setMonth(month: number): TimeStamp {
@@ -428,13 +450,30 @@ export class TimeStamp
428
450
  return new TimeStamp(d);
429
451
  }
430
452
 
431
- /** @returns the integer day that the timestamp corresponds to within its month. */
453
+ /**
454
+ * @returns a copy of the timestamp with the month changed in local time.
455
+ * @param month the value to set the month to.
456
+ */
457
+ setLocalMonth(month: number): TimeStamp {
458
+ const d = this.date();
459
+ d.setMonth(month);
460
+ return new TimeStamp(d);
461
+ }
462
+
463
+ /** @returns the integer day that the timestamp corresponds to within its month in
464
+ * UTC. */
432
465
  get day(): number {
433
466
  return this.date().getUTCDate();
434
467
  }
435
468
 
469
+ /** @returns the integer day that the timestamp corresponds to within its month in
470
+ * local time. */
471
+ get localDay(): number {
472
+ return this.date().getDate();
473
+ }
474
+
436
475
  /**
437
- * @returns a copy of the timestamp with the day changed.
476
+ * @returns a copy of the timestamp with the day changed in UTC.
438
477
  * @param day the value the set the day to.
439
478
  */
440
479
  setDay(day: number): TimeStamp {
@@ -443,6 +482,16 @@ export class TimeStamp
443
482
  return new TimeStamp(d);
444
483
  }
445
484
 
485
+ /**
486
+ * @returns a copy of the timestamp with the day changed in local time.
487
+ * @param day the value to set the day to.
488
+ */
489
+ setLocalDay(day: number): TimeStamp {
490
+ const d = this.date();
491
+ d.setDate(day);
492
+ return new TimeStamp(d);
493
+ }
494
+
446
495
  /**
447
496
  * @returns the integer hour that the timestamp corresponds to within its day.
448
497
  */
@@ -451,7 +500,8 @@ export class TimeStamp
451
500
  }
452
501
 
453
502
  /**
454
- * @returns the integer hour that the timestamp corresponds to within its day in local time.
503
+ * @returns the integer hour that the timestamp corresponds to within its day in local
504
+ * time.
455
505
  */
456
506
  get localHour(): number {
457
507
  return this.date().getHours();
@@ -477,13 +527,20 @@ export class TimeStamp
477
527
  return new TimeStamp(d);
478
528
  }
479
529
 
480
- /** @returns the integer minute that the timestamp corresponds to within its hour. */
530
+ /** @returns the integer minute that the timestamp corresponds to within its hour in
531
+ * UTC. */
481
532
  get minute(): number {
482
533
  return this.date().getUTCMinutes();
483
534
  }
484
535
 
536
+ /** @returns the integer minute that the timestamp corresponds to within its hour in
537
+ * local time. */
538
+ get localMinute(): number {
539
+ return this.date().getMinutes();
540
+ }
541
+
485
542
  /**
486
- * @returns a copy of the timestamp with the minute changed.
543
+ * @returns a copy of the timestamp with the minute changed in UTC.
487
544
  * @param minute the value to set the minute to.
488
545
  */
489
546
  setMinute(minute: number): TimeStamp {
@@ -492,16 +549,34 @@ export class TimeStamp
492
549
  return new TimeStamp(d);
493
550
  }
494
551
 
552
+ /**
553
+ * @returns a copy of the timestamp with the minute changed in local time.
554
+ * @param minute the value to set the minute to.
555
+ */
556
+ setLocalMinute(minute: number): TimeStamp {
557
+ const d = this.date();
558
+ d.setMinutes(minute);
559
+ return new TimeStamp(d);
560
+ }
561
+
495
562
  /**
496
563
  * @returns the integer second that the timestamp corresponds to within its
497
- * minute.
564
+ * minute in UTC.
498
565
  */
499
566
  get second(): number {
500
567
  return this.date().getUTCSeconds();
501
568
  }
502
569
 
503
570
  /**
504
- * @returns a copy of the timestamp with the second changed.
571
+ * @returns the integer second that the timestamp corresponds to within its minute in
572
+ * local time.
573
+ */
574
+ get localSecond(): number {
575
+ return this.date().getSeconds();
576
+ }
577
+
578
+ /**
579
+ * @returns a copy of the timestamp with the second changed in UTC.
505
580
  * @param second the value to set the second to.
506
581
  */
507
582
  setSecond(second: number): TimeStamp {
@@ -510,16 +585,34 @@ export class TimeStamp
510
585
  return new TimeStamp(d);
511
586
  }
512
587
 
588
+ /**
589
+ * @returns a copy of the timestamp with the second changed in local time.
590
+ * @param second the value to set the second to.
591
+ */
592
+ setLocalSecond(second: number): TimeStamp {
593
+ const d = this.date();
594
+ d.setSeconds(second);
595
+ return new TimeStamp(d);
596
+ }
597
+
513
598
  /**
514
599
  * @returns the integer millisecond that the timestamp corresponds to within its
515
- * second.
600
+ * second in UTC.
516
601
  */
517
602
  get millisecond(): number {
518
603
  return this.date().getUTCMilliseconds();
519
604
  }
520
605
 
521
606
  /**
522
- * @returns a copy of the timestamp with the milliseconds changed.
607
+ * @returns the integer millisecond that the timestamp corresponds to within its
608
+ * second in local time.
609
+ */
610
+ get localMillisecond(): number {
611
+ return this.date().getMilliseconds();
612
+ }
613
+
614
+ /**
615
+ * @returns a copy of the timestamp with the milliseconds changed in UTC.
523
616
  * @param millisecond the value to set the millisecond to.
524
617
  */
525
618
  setMillisecond(millisecond: number): TimeStamp {
@@ -528,6 +621,16 @@ export class TimeStamp
528
621
  return new TimeStamp(d);
529
622
  }
530
623
 
624
+ /**
625
+ * @returns a copy of the timestamp with the milliseconds changed in local time.
626
+ * @param millisecond the value to set the millisecond to.
627
+ */
628
+ setLocalMillisecond(millisecond: number): TimeStamp {
629
+ const d = this.date();
630
+ d.setMilliseconds(millisecond);
631
+ return new TimeStamp(d);
632
+ }
633
+
531
634
  /**
532
635
  * Returns a string representation of the TimeStamp.
533
636
  *