@naturalcycles/js-lib 14.229.0 → 14.230.1

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;
@@ -148,4 +153,4 @@ export declare function localDateOrToday(d?: LocalDateInput | null): LocalDate;
148
153
  /**
149
154
  Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
150
155
  */
151
- export declare function todayIsoDateString(): IsoDateString;
156
+ export declare function todayString(): IsoDateString;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.todayIsoDateString = exports.localDateOrToday = exports.localDateOrUndefined = exports.localDateToday = exports.localDate = exports.localDateRangeIterable = exports.localDateRange = exports.LocalDate = void 0;
3
+ exports.todayString = 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
  }
@@ -499,8 +506,8 @@ exports.localDateOrToday = localDateOrToday;
499
506
  /**
500
507
  Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
501
508
  */
502
- function todayIsoDateString() {
509
+ function todayString() {
503
510
  // It was benchmarked to be faster than by concatenating individual Date components
504
511
  return new Date().toISOString().slice(0, 10);
505
512
  }
506
- exports.todayIsoDateString = todayIsoDateString;
513
+ exports.todayString = todayString;
@@ -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) => {
@@ -534,6 +541,30 @@ function nowUnix() {
534
541
  return Math.floor(Date.now() / 1000);
535
542
  }
536
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;
537
568
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
538
569
  function getWeek(date) {
539
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
  }
@@ -489,7 +496,7 @@ export function localDateOrToday(d) {
489
496
  /**
490
497
  Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
491
498
  */
492
- export function todayIsoDateString() {
499
+ export function todayString() {
493
500
  // It was benchmarked to be faster than by concatenating individual Date components
494
501
  return new Date().toISOString().slice(0, 10);
495
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) => {
@@ -526,6 +533,28 @@ export function localTimeOrNow(d) {
526
533
  export function nowUnix() {
527
534
  return Math.floor(Date.now() / 1000);
528
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
+ }
529
558
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
530
559
  function getWeek(date) {
531
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.229.0",
3
+ "version": "14.230.1",
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
  }
@@ -606,7 +614,7 @@ export function localDateOrToday(d?: LocalDateInput | null): LocalDate {
606
614
  /**
607
615
  Convenience function to return current today's IsoDateString representation, e.g `2024-06-21`
608
616
  */
609
- export function todayIsoDateString(): IsoDateString {
617
+ export function todayString(): IsoDateString {
610
618
  // It was benchmarked to be faster than by concatenating individual Date components
611
619
  return new Date().toISOString().slice(0, 10)
612
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) => {
@@ -642,6 +652,30 @@ export function nowUnix(): UnixTimestampNumber {
642
652
  return Math.floor(Date.now() / 1000)
643
653
  }
644
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
+
645
679
  // based on: https://github.com/date-fns/date-fns/blob/master/src/getISOWeek/index.ts
646
680
  function getWeek(date: Date): number {
647
681
  const diff = startOfWeek(date).getTime() - startOfWeekYear(date).getTime()