busroot-sdk 0.0.8-dev.2930158956 → 0.0.8-dev.2936968167

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.
Files changed (2) hide show
  1. package/build/index.js +30 -13
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -567,6 +567,8 @@ var require_times = __commonJS({
567
567
  exports2.fromSpecificHourString = fromSpecificHourString;
568
568
  exports2.plantWeekNumber = plantWeekNumber;
569
569
  exports2.plantWeekStart = plantWeekStart;
570
+ exports2.plantDayStart = plantDayStart;
571
+ exports2.plantProductionDate = plantProductionDate;
570
572
  exports2.addPlantDays = addPlantDays;
571
573
  exports2.getTimeRangeFromRangeCode = getTimeRangeFromRangeCode;
572
574
  exports2.getCoverage = getCoverage;
@@ -995,9 +997,24 @@ var require_times = __commonJS({
995
997
  return start;
996
998
  }
997
999
  var DAYS_PER_WEEK = 7;
1000
+ function plantDayStart(date, dayStartHour) {
1001
+ const start = date.clone().hour(dayStartHour);
1002
+ const timezone = date.tz();
1003
+ if (timezone && (0, moment_timezone_1.default)(start.valueOf()).tz(timezone).hour() !== dayStartHour) {
1004
+ return (0, moment_timezone_1.default)(start.valueOf()).tz(timezone).add(1, "hour");
1005
+ }
1006
+ return start;
1007
+ }
1008
+ function plantProductionDate(instant, plant) {
1009
+ const date = (0, moment_timezone_1.default)(instant).tz(plant.timezone).startOf("day");
1010
+ if (instant < plantDayStart(date, plant.dayStartHour).valueOf()) {
1011
+ date.subtract(1, "day");
1012
+ }
1013
+ return date;
1014
+ }
998
1015
  function plantDayRange(date, days, dayStartHour) {
999
- const from = date.clone().add(dayStartHour, "hour").valueOf();
1000
- const to = date.clone().add(days, "days").add(dayStartHour, "hour").valueOf();
1016
+ const from = plantDayStart(date, dayStartHour).valueOf();
1017
+ const to = plantDayStart(date.clone().add(days, "days"), dayStartHour).valueOf();
1001
1018
  return timestampsToTimeRange({ from, to });
1002
1019
  }
1003
1020
  function addPlantDays(instant, days, timezone) {
@@ -1005,10 +1022,11 @@ var require_times = __commonJS({
1005
1022
  }
1006
1023
  function floorToPlantWeekStart(instant, plant) {
1007
1024
  const weekStartDay = plant.weekStartDay ?? 0;
1008
- return (0, moment_timezone_1.default)(instant).tz(plant.timezone).subtract(weekStartDay, "days").subtract(plant.dayStartHour, "hours").startOf("isoWeek").add(weekStartDay, "days").add(plant.dayStartHour, "hours").valueOf();
1025
+ const weekStart = plantProductionDate(instant, plant).subtract(weekStartDay, "days").startOf("isoWeek").add(weekStartDay, "days");
1026
+ return plantDayStart(weekStart, plant.dayStartHour).valueOf();
1009
1027
  }
1010
1028
  function floorToPlantDayStart(instant, plant) {
1011
- return (0, moment_timezone_1.default)(instant).tz(plant.timezone).subtract(plant.dayStartHour, "hours").startOf("day").add(plant.dayStartHour, "hours").valueOf();
1029
+ return plantDayStart(plantProductionDate(instant, plant), plant.dayStartHour).valueOf();
1012
1030
  }
1013
1031
  var CUSTOM_RANGE_MINUTE_STEPS = {
1014
1032
  "5m": 5,
@@ -1063,19 +1081,18 @@ var require_times = __commonJS({
1063
1081
  return timestampsToTimeRange({ from: day.valueOf(), to: day.add(1, "hours").valueOf() });
1064
1082
  },
1065
1083
  // Every range below ends where the next one of its kind starts, built on the plant's local calendar
1066
- // (add whole days/months, then dayStartHour) and only then read as UTC. Adding a fixed n×24h instead
1067
- // would make a range that contains a daylight-saving change an hour too long or short, leaving gaps
1068
- // and overlaps between consecutive ranges.
1084
+ // (add whole days/months, then set the hour to dayStartHour) and only then read as UTC. Adding a fixed
1085
+ // n×24h instead would make a range that contains a daylight-saving change an hour too long or short,
1086
+ // leaving gaps and overlaps between consecutive ranges.
1069
1087
  //
1070
- // TODAY/YESTERDAY: subtract dayStartHour before startOf("day") to find the
1071
- // correct production day boundary, then re-add it. This handles the case where
1072
- // e.g. it's 02:00 but the production day started at 06:00 the previous calendar day.
1088
+ // TODAY/YESTERDAY: the production date containing `now` - e.g. at 02:00 with a 06:00 day start,
1089
+ // the production day that started the previous calendar day.
1073
1090
  today: () => {
1074
- const date = (0, moment_timezone_1.default)(now).tz(plant.timezone).add(-plant.dayStartHour, "hour").startOf("day");
1091
+ const date = plantProductionDate(now ?? Date.now(), plant);
1075
1092
  return plantDayRange(date, 1, plant.dayStartHour);
1076
1093
  },
1077
1094
  yesterday: () => {
1078
- const date = (0, moment_timezone_1.default)(now).tz(plant.timezone).add(-plant.dayStartHour, "hour").startOf("day").add(-1, "day");
1095
+ const date = plantProductionDate(now ?? Date.now(), plant).subtract(1, "day");
1079
1096
  return plantDayRange(date, 1, plant.dayStartHour);
1080
1097
  },
1081
1098
  day: () => {
@@ -1087,7 +1104,7 @@ var require_times = __commonJS({
1087
1104
  return plantDayRange(week, DAYS_PER_WEEK, plant.dayStartHour);
1088
1105
  },
1089
1106
  month: () => {
1090
- const month = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}}`, "YYYY_MM", plant.timezone).startOf("month");
1107
+ const month = moment_timezone_1.default.tz(`${parseInt(parts[1])}_${parseInt(parts[2])}`, "YYYY_MM", plant.timezone).startOf("month");
1091
1108
  return plantDayRange(month, month.daysInMonth(), plant.dayStartHour);
1092
1109
  },
1093
1110
  // QUARTER: 13 ISO weeks (14 for Q4 in 53-week years). Start at the first ISO week of the quarter,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "busroot-sdk",
3
- "version": "0.0.8-dev.2930158956",
3
+ "version": "0.0.8-dev.2936968167",
4
4
  "description": "An SDK for accessing Busroot from output.industries",
5
5
  "homepage": "https://www.output.industries",
6
6
  "main": "./build/index.js",