@naturalcycles/js-lib 14.228.0 → 14.230.0

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.
@@ -97,6 +97,11 @@ export declare class LocalDate {
97
97
  minus(num: number, unit: LocalDateUnit, mutate?: boolean): LocalDate;
98
98
  startOf(unit: LocalDateUnitStrict): LocalDate;
99
99
  endOf(unit: LocalDateUnitStrict): LocalDate;
100
+ /**
101
+ * Returns how many days are in the current month.
102
+ * E.g 31 for January.
103
+ */
104
+ daysInMonth(): number;
100
105
  static getYearLength(year: number): number;
101
106
  static getMonthLength(year: number, month: number): number;
102
107
  static isLeapYear(year: number): boolean;
@@ -145,3 +150,7 @@ export declare function localDateOrUndefined(d?: LocalDateInput | null): LocalDa
145
150
  * Creates a LocalDate from the input, unless it's falsy - then returns LocalDate.today.
146
151
  */
147
152
  export declare function localDateOrToday(d?: LocalDateInput | null): LocalDate;
153
+ /**
154
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
155
+ */
156
+ export declare function todayIsoDateString(): IsoDateString;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
3
+ exports.todayIsoDateString = exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
4
4
  const assert_1 = require("../error/assert");
5
5
  const iterable2_1 = require("../iter/iterable2");
6
6
  const localTime_1 = require("./localTime");
@@ -361,6 +361,13 @@ class LocalDate {
361
361
  // year
362
362
  return LocalDate.create(this.$year, 12, 31);
363
363
  }
364
+ /**
365
+ * Returns how many days are in the current month.
366
+ * E.g 31 for January.
367
+ */
368
+ daysInMonth() {
369
+ return LocalDate.getMonthLength(this.$year, this.$month);
370
+ }
364
371
  static getYearLength(year) {
365
372
  return this.isLeapYear(year) ? 366 : 365;
366
373
  }
@@ -496,3 +503,11 @@ function localDateOrToday(d) {
496
503
  return d ? LocalDate.of(d) : LocalDate.today();
497
504
  }
498
505
  exports.localDateOrToday = localDateOrToday;
506
+ /**
507
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
508
+ */
509
+ function todayIsoDateString() {
510
+ // It was benchmarked to be faster than by concatenating individual Date components
511
+ return new Date().toISOString().slice(0, 10);
512
+ }
513
+ exports.todayIsoDateString = todayIsoDateString;
@@ -1,4 +1,4 @@
1
- import type { Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
1
+ import type { Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, NumberOfHours, NumberOfMinutes, SortDirection, UnixTimestampMillisNumber, UnixTimestampNumber } from '../types';
2
2
  import { LocalDate } from './localDate';
3
3
  export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second';
4
4
  export declare enum ISODayOfWeek {
@@ -75,6 +75,11 @@ export declare class LocalTime {
75
75
  diff(other: LocalTimeInput, unit: LocalTimeUnit): number;
76
76
  startOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
77
77
  endOf(unit: LocalTimeUnit, mutate?: boolean): LocalTime;
78
+ /**
79
+ * Returns how many days are in the current month.
80
+ * E.g 31 for January.
81
+ */
82
+ daysInMonth(): number;
78
83
  static sort(items: LocalTime[], mutate?: boolean, dir?: SortDirection): LocalTime[];
79
84
  static earliestOrUndefined(items: LocalTimeInput[]): LocalTime | undefined;
80
85
  static earliest(items: LocalTimeInput[]): LocalTime;
@@ -173,3 +178,21 @@ export declare function localTimeOrNow(d?: LocalTimeInput | null): LocalTime;
173
178
  Like Date.now(), but in seconds.
174
179
  */
175
180
  export declare function nowUnix(): UnixTimestampNumber;
181
+ /**
182
+ * UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
183
+ * to the local time to get UTC time.
184
+ *
185
+ * E.g utcOffset for CEST is -120,
186
+ * which means that you need to add -120 minutes to the local time to get UTC time.
187
+ *
188
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
189
+ */
190
+ export declare function getUTCOffsetMinutes(): NumberOfMinutes;
191
+ /**
192
+ * Same as getUTCOffsetMinutes, but rounded to hours.
193
+ *
194
+ * E.g for CEST it is -2.
195
+ *
196
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
197
+ */
198
+ export declare function getUTCOffsetHours(): NumberOfHours;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nowUnix = exports.localTimeOrNow = exports.localTimeOrUndefined = exports.localTimeNow = exports.localTime = exports.LocalTime = exports.ISODayOfWeek = void 0;
3
+ exports.getUTCOffsetHours = exports.getUTCOffsetMinutes = exports.nowUnix = exports.localTimeOrNow = exports.localTimeOrUndefined = exports.localTimeNow = exports.localTime = exports.LocalTime = exports.ISODayOfWeek = void 0;
4
4
  const assert_1 = require("../error/assert");
5
5
  const time_util_1 = require("../time/time.util");
6
6
  const localDate_1 = require("./localDate");
@@ -302,6 +302,13 @@ class LocalTime {
302
302
  }
303
303
  return mutate ? this : new LocalTime(d);
304
304
  }
305
+ /**
306
+ * Returns how many days are in the current month.
307
+ * E.g 31 for January.
308
+ */
309
+ daysInMonth() {
310
+ return localDate_1.LocalDate.getMonthLength(this.$date.getFullYear(), this.$date.getMonth() + 1);
311
+ }
305
312
  static sort(items, mutate = false, dir = 'asc') {
306
313
  const mod = dir === 'desc' ? -1 : 1;
307
314
  return (mutate ? items : [...items]).sort((a, b) => {
@@ -443,25 +450,8 @@ class LocalTime {
443
450
  return localDate_1.LocalDate.fromDate(this.$date);
444
451
  }
445
452
  toPretty(seconds = true) {
446
- const { year, month, day, hour, minute, second } = this.components();
447
- return ([
448
- String(year).padStart(4, '0'),
449
- String(month).padStart(2, '0'),
450
- String(day).padStart(2, '0'),
451
- ].join('-') +
452
- ' ' +
453
- [
454
- String(hour).padStart(2, '0'),
455
- String(minute).padStart(2, '0'),
456
- seconds && String(second).padStart(2, '0'),
457
- ]
458
- .filter(Boolean)
459
- .join(':'));
460
- // return this.$date
461
- // .toISOString()
462
- // .slice(0, seconds ? 19 : 16)
463
- // .split('T')
464
- // .join(' ')
453
+ const s = this.$date.toISOString();
454
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16);
465
455
  }
466
456
  /**
467
457
  * Returns e.g: `1984-06-21T17:56:21`
@@ -473,27 +463,13 @@ class LocalTime {
473
463
  * Returns e.g: `1984-06-21`, only the date part of DateTime
474
464
  */
475
465
  toISODate() {
476
- const { year, month, day } = this.components();
477
- return [
478
- String(year).padStart(4, '0'),
479
- String(month).padStart(2, '0'),
480
- String(day).padStart(2, '0'),
481
- ].join('-');
482
- // return this.$date.toISOString().slice(0, 10)
466
+ return this.$date.toISOString().slice(0, 10);
483
467
  }
484
468
  /**
485
469
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
486
470
  */
487
471
  toISOTime(seconds = true) {
488
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
489
- const { hour, minute, second } = this.components();
490
- return [
491
- String(hour).padStart(2, '0'),
492
- String(minute).padStart(2, '0'),
493
- seconds && String(second).padStart(2, '0'),
494
- ]
495
- .filter(Boolean)
496
- .join(':');
472
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16);
497
473
  }
498
474
  /**
499
475
  * Returns e.g: `19840621_1705`
@@ -565,6 +541,30 @@ function nowUnix() {
565
541
  return Math.floor(Date.now() / 1000);
566
542
  }
567
543
  exports.nowUnix = nowUnix;
544
+ /**
545
+ * UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
546
+ * to the local time to get UTC time.
547
+ *
548
+ * E.g utcOffset for CEST is -120,
549
+ * which means that you need to add -120 minutes to the local time to get UTC time.
550
+ *
551
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
552
+ */
553
+ function getUTCOffsetMinutes() {
554
+ return -new Date().getTimezoneOffset() || 0;
555
+ }
556
+ exports.getUTCOffsetMinutes = getUTCOffsetMinutes;
557
+ /**
558
+ * Same as getUTCOffsetMinutes, but rounded to hours.
559
+ *
560
+ * E.g for CEST it is -2.
561
+ *
562
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
563
+ */
564
+ function getUTCOffsetHours() {
565
+ return Math.round(getUTCOffsetMinutes() / 60);
566
+ }
567
+ exports.getUTCOffsetHours = getUTCOffsetHours;
568
568
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
569
569
  function getWeek(date) {
570
570
  const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime();
@@ -358,6 +358,13 @@ export class LocalDate {
358
358
  // year
359
359
  return LocalDate.create(this.$year, 12, 31);
360
360
  }
361
+ /**
362
+ * Returns how many days are in the current month.
363
+ * E.g 31 for January.
364
+ */
365
+ daysInMonth() {
366
+ return LocalDate.getMonthLength(this.$year, this.$month);
367
+ }
361
368
  static getYearLength(year) {
362
369
  return this.isLeapYear(year) ? 366 : 365;
363
370
  }
@@ -486,3 +493,10 @@ export function localDateOrUndefined(d) {
486
493
  export function localDateOrToday(d) {
487
494
  return d ? LocalDate.of(d) : LocalDate.today();
488
495
  }
496
+ /**
497
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
498
+ */
499
+ export function todayIsoDateString() {
500
+ // It was benchmarked to be faster than by concatenating individual Date components
501
+ return new Date().toISOString().slice(0, 10);
502
+ }
@@ -300,6 +300,13 @@ export class LocalTime {
300
300
  }
301
301
  return mutate ? this : new LocalTime(d);
302
302
  }
303
+ /**
304
+ * Returns how many days are in the current month.
305
+ * E.g 31 for January.
306
+ */
307
+ daysInMonth() {
308
+ return LocalDate.getMonthLength(this.$date.getFullYear(), this.$date.getMonth() + 1);
309
+ }
303
310
  static sort(items, mutate = false, dir = 'asc') {
304
311
  const mod = dir === 'desc' ? -1 : 1;
305
312
  return (mutate ? items : [...items]).sort((a, b) => {
@@ -441,25 +448,8 @@ export class LocalTime {
441
448
  return LocalDate.fromDate(this.$date);
442
449
  }
443
450
  toPretty(seconds = true) {
444
- const { year, month, day, hour, minute, second } = this.components();
445
- return ([
446
- String(year).padStart(4, '0'),
447
- String(month).padStart(2, '0'),
448
- String(day).padStart(2, '0'),
449
- ].join('-') +
450
- ' ' +
451
- [
452
- String(hour).padStart(2, '0'),
453
- String(minute).padStart(2, '0'),
454
- seconds && String(second).padStart(2, '0'),
455
- ]
456
- .filter(Boolean)
457
- .join(':'));
458
- // return this.$date
459
- // .toISOString()
460
- // .slice(0, seconds ? 19 : 16)
461
- // .split('T')
462
- // .join(' ')
451
+ const s = this.$date.toISOString();
452
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16);
463
453
  }
464
454
  /**
465
455
  * Returns e.g: `1984-06-21T17:56:21`
@@ -471,27 +461,13 @@ export class LocalTime {
471
461
  * Returns e.g: `1984-06-21`, only the date part of DateTime
472
462
  */
473
463
  toISODate() {
474
- const { year, month, day } = this.components();
475
- return [
476
- String(year).padStart(4, '0'),
477
- String(month).padStart(2, '0'),
478
- String(day).padStart(2, '0'),
479
- ].join('-');
480
- // return this.$date.toISOString().slice(0, 10)
464
+ return this.$date.toISOString().slice(0, 10);
481
465
  }
482
466
  /**
483
467
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
484
468
  */
485
469
  toISOTime(seconds = true) {
486
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
487
- const { hour, minute, second } = this.components();
488
- return [
489
- String(hour).padStart(2, '0'),
490
- String(minute).padStart(2, '0'),
491
- seconds && String(second).padStart(2, '0'),
492
- ]
493
- .filter(Boolean)
494
- .join(':');
470
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16);
495
471
  }
496
472
  /**
497
473
  * Returns e.g: `19840621_1705`
@@ -557,6 +533,28 @@ export function localTimeOrNow(d) {
557
533
  export function nowUnix() {
558
534
  return Math.floor(Date.now() / 1000);
559
535
  }
536
+ /**
537
+ * UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
538
+ * to the local time to get UTC time.
539
+ *
540
+ * E.g utcOffset for CEST is -120,
541
+ * which means that you need to add -120 minutes to the local time to get UTC time.
542
+ *
543
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
544
+ */
545
+ export function getUTCOffsetMinutes() {
546
+ return -new Date().getTimezoneOffset() || 0;
547
+ }
548
+ /**
549
+ * Same as getUTCOffsetMinutes, but rounded to hours.
550
+ *
551
+ * E.g for CEST it is -2.
552
+ *
553
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
554
+ */
555
+ export function getUTCOffsetHours() {
556
+ return Math.round(getUTCOffsetMinutes() / 60);
557
+ }
560
558
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
561
559
  function getWeek(date) {
562
560
  const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.228.0",
3
+ "version": "14.230.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -438,6 +438,14 @@ export class LocalDate {
438
438
  return LocalDate.create(this.$year, 12, 31)
439
439
  }
440
440
 
441
+ /**
442
+ * Returns how many days are in the current month.
443
+ * E.g 31 for January.
444
+ */
445
+ daysInMonth(): number {
446
+ return LocalDate.getMonthLength(this.$year, this.$month)
447
+ }
448
+
441
449
  static getYearLength(year: number): number {
442
450
  return this.isLeapYear(year) ? 366 : 365
443
451
  }
@@ -602,3 +610,11 @@ export function localDateOrUndefined(d?: LocalDateInput | null): LocalDate | und
602
610
  export function localDateOrToday(d?: LocalDateInput | null): LocalDate {
603
611
  return d ? LocalDate.of(d) : LocalDate.today()
604
612
  }
613
+
614
+ /**
615
+ Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
616
+ */
617
+ export function todayIsoDateString(): IsoDateString {
618
+ // It was benchmarked to be faster than by concatenating individual Date components
619
+ return new Date().toISOString().slice(0, 10)
620
+ }
@@ -5,6 +5,8 @@ import type {
5
5
  IsoDateString,
6
6
  IsoDateTimeString,
7
7
  MonthId,
8
+ NumberOfHours,
9
+ NumberOfMinutes,
8
10
  SortDirection,
9
11
  UnixTimestampMillisNumber,
10
12
  UnixTimestampNumber,
@@ -376,6 +378,14 @@ export class LocalTime {
376
378
  return mutate ? this : new LocalTime(d)
377
379
  }
378
380
 
381
+ /**
382
+ * Returns how many days are in the current month.
383
+ * E.g 31 for January.
384
+ */
385
+ daysInMonth(): number {
386
+ return LocalDate.getMonthLength(this.$date.getFullYear(), this.$date.getMonth() + 1)
387
+ }
388
+
379
389
  static sort(items: LocalTime[], mutate = false, dir: SortDirection = 'asc'): LocalTime[] {
380
390
  const mod = dir === 'desc' ? -1 : 1
381
391
  return (mutate ? items : [...items]).sort((a, b) => {
@@ -541,29 +551,8 @@ export class LocalTime {
541
551
  }
542
552
 
543
553
  toPretty(seconds = true): IsoDateTimeString {
544
- const { year, month, day, hour, minute, second } = this.components()
545
-
546
- return (
547
- [
548
- String(year).padStart(4, '0'),
549
- String(month).padStart(2, '0'),
550
- String(day).padStart(2, '0'),
551
- ].join('-') +
552
- ' ' +
553
- [
554
- String(hour).padStart(2, '0'),
555
- String(minute).padStart(2, '0'),
556
- seconds && String(second).padStart(2, '0'),
557
- ]
558
- .filter(Boolean)
559
- .join(':')
560
- )
561
-
562
- // return this.$date
563
- // .toISOString()
564
- // .slice(0, seconds ? 19 : 16)
565
- // .split('T')
566
- // .join(' ')
554
+ const s = this.$date.toISOString()
555
+ return s.slice(0, 10) + ' ' + s.slice(11, seconds ? 19 : 16)
567
556
  }
568
557
 
569
558
  /**
@@ -577,31 +566,14 @@ export class LocalTime {
577
566
  * Returns e.g: `1984-06-21`, only the date part of DateTime
578
567
  */
579
568
  toISODate(): IsoDateString {
580
- const { year, month, day } = this.components()
581
-
582
- return [
583
- String(year).padStart(4, '0'),
584
- String(month).padStart(2, '0'),
585
- String(day).padStart(2, '0'),
586
- ].join('-')
587
-
588
- // return this.$date.toISOString().slice(0, 10)
569
+ return this.$date.toISOString().slice(0, 10)
589
570
  }
590
571
 
591
572
  /**
592
573
  * Returns e.g: `17:03:15` (or `17:03` with seconds=false)
593
574
  */
594
575
  toISOTime(seconds = true): string {
595
- // return this.$date.toISOString().slice(11, seconds ? 19 : 16)
596
- const { hour, minute, second } = this.components()
597
-
598
- return [
599
- String(hour).padStart(2, '0'),
600
- String(minute).padStart(2, '0'),
601
- seconds && String(second).padStart(2, '0'),
602
- ]
603
- .filter(Boolean)
604
- .join(':')
576
+ return this.$date.toISOString().slice(11, seconds ? 19 : 16)
605
577
  }
606
578
 
607
579
  /**
@@ -680,6 +652,30 @@ export function nowUnix(): UnixTimestampNumber {
680
652
  return Math.floor(Date.now() / 1000)
681
653
  }
682
654
 
655
+ /**
656
+ * UTC offset is the opposite of "timezone offset" - it's the number of minutes to add
657
+ * to the local time to get UTC time.
658
+ *
659
+ * E.g utcOffset for CEST is -120,
660
+ * which means that you need to add -120 minutes to the local time to get UTC time.
661
+ *
662
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
663
+ */
664
+ export function getUTCOffsetMinutes(): NumberOfMinutes {
665
+ return -new Date().getTimezoneOffset() || 0
666
+ }
667
+
668
+ /**
669
+ * Same as getUTCOffsetMinutes, but rounded to hours.
670
+ *
671
+ * E.g for CEST it is -2.
672
+ *
673
+ * Instead of -0 it returns 0, for the peace of mind and less weird test/snapshot differences.
674
+ */
675
+ export function getUTCOffsetHours(): NumberOfHours {
676
+ return Math.round(getUTCOffsetMinutes() / 60)
677
+ }
678
+
683
679
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
684
680
  function getWeek(date: Date): number {
685
681
  const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime()