diff --git a/projects/planner/src/app/pipes/duration.pipe.ts b/projects/planner/src/app/pipes/duration.pipe.ts index 6161d9cb..e623bdd4 100644 --- a/projects/planner/src/app/pipes/duration.pipe.ts +++ b/projects/planner/src/app/pipes/duration.pipe.ts @@ -10,10 +10,10 @@ export class DurationPipe implements PipeTransform { constructor(private datePipe: DatePipe) {} transform(value: number, maxValue: number): unknown { - const duration = Time.toDate(value); + const duration = DateFormats.toDate(value); const format = DateFormats.selectTimeFormat(maxValue); const minutesPart = this.datePipe.transform(duration, format) || ''; - let hours = DateFormats.hoursDuration(value); + let hours = Time.toHours(value); hours = Math.trunc(hours); if(hours >= 1) { diff --git a/projects/planner/src/app/profilechart/profilechart.component.ts b/projects/planner/src/app/profilechart/profilechart.component.ts index dabb9d05..33b40288 100644 --- a/projects/planner/src/app/profilechart/profilechart.component.ts +++ b/projects/planner/src/app/profilechart/profilechart.component.ts @@ -5,13 +5,12 @@ import { DiveResults } from '../shared/diveresults'; import { faChartArea } from '@fortawesome/free-solid-svg-icons'; import * as Plotly from 'plotly.js-basic-dist'; import { takeUntil } from 'rxjs'; -import { Time } from 'scuba-physics'; import { DateFormats } from '../shared/formaters'; import { UnitConversion } from '../shared/UnitConversion'; import { SelectedWaypoint } from '../shared/selectedwaypointService'; import { Streamed } from '../shared/streamed'; import { ResamplingService } from '../shared/ResamplingService'; -import {DiveSchedules} from '../shared/dive.schedules'; +import { DiveSchedules } from '../shared/dive.schedules'; @Component({ selector: 'app-profilechart', @@ -124,8 +123,8 @@ export class ProfileChartComponent extends Streamed implements OnInit { }; if (wayPoint) { - this.cursor1.x0 = Time.toDate(wayPoint.startTime); - this.cursor1.x1 = Time.toDate(wayPoint.endTime); + this.cursor1.x0 = DateFormats.toDate(wayPoint.startTime); + this.cursor1.x1 = DateFormats.toDate(wayPoint.endTime); this.cursor1.y0 = wayPoint.startDepth; this.cursor1.y1 = wayPoint.endDepth; update.shapes.push(this.cursor1); diff --git a/projects/planner/src/app/shared/ResamplingService.ts b/projects/planner/src/app/shared/ResamplingService.ts index ae3336ca..37773571 100644 --- a/projects/planner/src/app/shared/ResamplingService.ts +++ b/projects/planner/src/app/shared/ResamplingService.ts @@ -1,7 +1,8 @@ import { Injectable } from '@angular/core'; -import { WayPoint } from '../shared/models'; -import { Event, EventType, Time, StandardGases, Precision, Ceiling } from 'scuba-physics'; -import { UnitConversion } from '../shared/UnitConversion'; +import { WayPoint } from './models'; +import { Event, EventType, StandardGases, Precision, Ceiling } from 'scuba-physics'; +import { UnitConversion } from './UnitConversion'; +import { DateFormats } from './formaters'; export interface AxisValues { xValues: Date[]; @@ -38,7 +39,7 @@ export class ResamplingService { events.forEach((event) => { if (this.isSupportedEvent(event)) { - xValues.push(Time.toDate(event.timeStamp)); + xValues.push(DateFormats.toDate(event.timeStamp)); const convertedDepth = this.convertDepth(event.depth); yValues.push(convertedDepth); const text = this.formatChartEventText(event); @@ -59,7 +60,7 @@ export class ResamplingService { // possible performance optimization = remove all waypoints, where ceiling = 0 and depth didn't change ceilings.forEach((item) => { - xValues.push(Time.toDate(item.time)); + xValues.push(DateFormats.toDate(item.time)); const depth = this.convertDepth(item.depth); yValues.push(depth); }); @@ -87,14 +88,14 @@ export class ResamplingService { private resampleDepthsToSeconds(xValues: Date[], yValues: number[], item: WayPoint) { const speed = (item.endDepthMeters - item.startDepthMeters) / item.duration; for (let timeStamp = item.startTime; timeStamp < item.endTime; timeStamp++) { - xValues.push(Time.toDate(timeStamp)); + xValues.push(DateFormats.toDate(timeStamp)); const depth = item.startDepthMeters + (timeStamp - item.startTime) * speed; const rounded = this.convertDepth(depth); yValues.push(rounded); } // fix end of the dive - xValues.push(Time.toDate(item.endTime)); + xValues.push(DateFormats.toDate(item.endTime)); const endDepth = this.convertDepth(item.endDepthMeters); yValues.push(endDepth); } @@ -111,7 +112,7 @@ export class ResamplingService { waiPoints.forEach(wayPoint => { if (wayPoint.duration > 0) { for (let seconds = 0; seconds < wayPoint.duration; seconds++) { - xValues.push(Time.toDate(totalDuration)); + xValues.push(DateFormats.toDate(totalDuration)); const depth = wayPoint.depthAt(seconds); const cumulativeWeight = depth + totalDuration * cumulativeAverage; totalDuration++; diff --git a/projects/planner/src/app/shared/formaters.ts b/projects/planner/src/app/shared/formaters.ts index 383fb7aa..12d90779 100644 --- a/projects/planner/src/app/shared/formaters.ts +++ b/projects/planner/src/app/shared/formaters.ts @@ -1,6 +1,6 @@ +import { Time } from 'scuba-physics'; + export class DateFormats { - private static readonly secondsPerHour = 3600; - private static readonly secondsPerDay = DateFormats.secondsPerHour * 24; /** * Chart formatting * Not showing days in chart, because it consumes lot of space @@ -26,12 +26,34 @@ export class DateFormats { return 'm:ss'; } - public static hoursDuration(seconds: number): number { - return seconds / DateFormats.secondsPerHour; + + /** + * Converts duration in seconds to Date structure + * + * @param seconds duration in seconds + * + * @returns amount seconds calculated from current duration + */ + public static toDate(seconds: number): Date { + // we don't care about UTC, because we don't handle date, only relative time + return new Date(1970, 1, 1, 0, 0, seconds, 0); + } + + /** + * Extracts number of seconds from date parameter + * + * @param date text containing data value + */ + public static secondsFromDate(date: string): number { + const newValue = new Date(date); + let result = newValue.getSeconds(); + result += newValue.getMinutes() * Time.oneMinute; + result += newValue.getHours() * Math.pow(Time.oneMinute, 2); + return result; } private static hasHoursRuntime(seconds: number): boolean { - const hoursCount = DateFormats.hoursDuration(seconds); + const hoursCount = Time.toHours(seconds); return hoursCount >= 1; } } diff --git a/projects/planner/src/app/shared/ndl.service.ts b/projects/planner/src/app/shared/ndl.service.ts index 351dec79..1a5bfe2d 100644 --- a/projects/planner/src/app/shared/ndl.service.ts +++ b/projects/planner/src/app/shared/ndl.service.ts @@ -1,9 +1,11 @@ import { Injectable } from '@angular/core'; -import { AlgorithmParams, BuhlmannAlgorithm, DepthConverter, Gas, Options } from 'scuba-physics'; +import { + AlgorithmParams, BuhlmannAlgorithm, DepthConverter, Gas, Options, Time +} from 'scuba-physics'; export class NdlLimit { public depth = 0; - public limit = 3600; + public limit = Time.oneHour; } @Injectable() diff --git a/projects/planner/src/app/shared/selectedwaypointService.ts b/projects/planner/src/app/shared/selectedwaypointService.ts index eb45e0e9..e59e4b4a 100644 --- a/projects/planner/src/app/shared/selectedwaypointService.ts +++ b/projects/planner/src/app/shared/selectedwaypointService.ts @@ -1,7 +1,7 @@ import { EventEmitter, Injectable, Input, Output } from '@angular/core'; -import { Time } from 'scuba-physics'; import { WayPoint } from './models'; -import {DiveSchedules} from './dive.schedules'; +import { DiveSchedules } from './dive.schedules'; +import { DateFormats } from './formaters'; @Injectable() export class SelectedWaypoint { @@ -14,7 +14,7 @@ export class SelectedWaypoint { public set selectedTimeStamp(newValue: string) { let found: WayPoint | undefined; if (newValue) { - const newTimeStamp = Time.secondsFromDate(newValue); + const newTimeStamp = DateFormats.secondsFromDate(newValue); const dive = this.schedules.selected.diveResult; found = dive.wayPoints.find(p => p.fits(newTimeStamp)); } diff --git a/projects/scuba-physics/src/lib/Time.ts b/projects/scuba-physics/src/lib/Time.ts index 95b2a6b2..5cdc79c7 100644 --- a/projects/scuba-physics/src/lib/Time.ts +++ b/projects/scuba-physics/src/lib/Time.ts @@ -6,8 +6,11 @@ export class Time { /** One minute is 60 seconds */ public static readonly oneMinute = 60; - /** Maximum deco stop duration one day */ - public static readonly oneDay = 24 * 60 * Time.oneMinute; + /** One hour is 3600 seconds */ + public static readonly oneHour = Time.oneMinute * 60; + + /** Maximum deco stop duration one day (86400 seconds) */ + public static readonly oneDay = Time.oneHour * 24; /** Default duration of the safety stop */ public static readonly safetyStopDuration = Time.oneMinute * 3; @@ -35,27 +38,13 @@ export class Time { } /** - * Converts duration in seconds to Date structure + * Converts duration in seconds to hours * * @param seconds duration in seconds * - * @returns amount seconds calculated from current duration - */ - public static toDate(seconds: number): Date { - // we don't care about UTC, because we don't handle date, only relative time - return new Date(1970, 1, 1, 0, 0, seconds, 0); - } - - /** - * Extracts number of seconds from date parameter - * - * @param date text containing data value + * @returns amount hours calculated from seconds duration */ - public static secondsFromDate(date: string): number { - const newValue = new Date(date); - let result = newValue.getSeconds(); - result += newValue.getMinutes() * Time.oneMinute; - result += newValue.getHours() * Math.pow(Time.oneMinute, 2); - return result; + public static toHours(seconds: number): number { + return seconds / Time.oneHour; } }