Skip to content

Commit

Permalink
Cleanup in Date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Dec 12, 2023
1 parent d4bf017 commit f03ff69
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 44 deletions.
4 changes: 2 additions & 2 deletions projects/planner/src/app/pipes/duration.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions projects/planner/src/app/shared/ResamplingService.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down Expand Up @@ -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);
}
Expand All @@ -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++;
Expand Down
32 changes: 27 additions & 5 deletions projects/planner/src/app/shared/formaters.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
6 changes: 4 additions & 2 deletions projects/planner/src/app/shared/ndl.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 3 additions & 3 deletions projects/planner/src/app/shared/selectedwaypointService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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));
}
Expand Down
29 changes: 9 additions & 20 deletions projects/scuba-physics/src/lib/Time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f03ff69

Please sign in to comment.