decova-dotnet
Version:
This package provides fundumentals that a .net developer may miss while working with Typescript, whether they are missing functinalities or funcionalities provided in a non-elegant design in javascript. Bad naming, bad design of optional parameters, non-c
129 lines • 4.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeSpan = void 0;
const Conv_1 = require("./Conv");
const List_1 = require("../Array/List");
class TimeSpan {
constructor(_ticks) {
this._ticks = _ticks;
}
static parse(text) {
text = text.trim();
let parts = new List_1.List(text.split(/\s*\D\s*/));
if ((parts.Count < 1 || parts.Count > 5) || parts.Any(p => /\D/.test(p))) {
throw new Error("Invalid TimeSpan format. Valid formats should satisfy the following two conditions: (1) it should be composed of from 1 to 5 parts, starting from days through to milliseconds. (2) Parts separators could be any single non-digit characters while extra spaces arround are not significant (It's not required to have a consistent use of the single separator).");
}
let numParts = parts.Select(sp => new Number(sp).valueOf()).Array;
let day = numParts[0];
let hour = (numParts.length > 1) ? numParts[1] : 0;
let minute = (numParts.length > 2) ? numParts[2] : 0;
let sec = (numParts.length > 3) ? numParts[3] : 0;
let milliSec = (numParts.length > 4) ? numParts[4] : 0;
let ticks = (day * Conv_1.Conv.DayTicks) + (hour * Conv_1.Conv.HourTicks) + (minute * Conv_1.Conv.MinuteTicks) + (sec * Conv_1.Conv.SecTicks) + (milliSec);
return new TimeSpan(ticks);
}
get ticks() {
return this._ticks;
this.valueOf = () => this._ticks;
}
get totalSeconds() {
return this.ticks / Conv_1.Conv.SecTicks;
}
get totalMinutes() {
return this.ticks / Conv_1.Conv.MinuteTicks;
}
get totalHours() {
return this.ticks / Conv_1.Conv.HourTicks;
}
get totalDays() {
return this.ticks / Conv_1.Conv.DayTicks;
}
get days() {
return Math.floor(this.ticks / Conv_1.Conv.DayTicks);
}
get hours() {
let ticks = this.ticks % Conv_1.Conv.DayTicks;
return Math.floor(ticks);
}
get minutes() {
let ticks = this.ticks % Conv_1.Conv.HourTicks;
return Math.floor(ticks);
}
get seconds() {
let ticks = this.ticks % Conv_1.Conv.MinuteTicks;
return Math.floor(ticks);
}
get milliseconds() {
let ticks = this.ticks % Conv_1.Conv.SecTicks;
return Math.floor(ticks);
}
static fromMilliSeconds(milliSec) {
return new TimeSpan(milliSec);
}
addMilliSeconds(milliSec) {
return new TimeSpan(this.ticks + milliSec);
}
static fromSeconds(seconds) {
return new TimeSpan(seconds * 1000);
}
addSeconds(seconds) {
return new TimeSpan(this.ticks + seconds * 1000);
}
static fromMinutes(minutes) {
return new TimeSpan(minutes * 60 * 1000);
}
addMinutes(minutes) {
return new TimeSpan(this.ticks + minutes * 60 * 1000);
}
static fromHours(hours) {
return new TimeSpan(hours * 60 * 60 * 1000);
}
addHours(hours) {
return new TimeSpan(this.ticks + hours * 60 * 60 * 1000);
}
static fromDays(days) {
return new TimeSpan(days * 24 * 60 * 60 * 1000);
}
addDays(days) {
return new TimeSpan(this.ticks + days * 24 * 60 * 60 * 1000);
}
static fromWeeks(weeks) {
return new TimeSpan(weeks * 7 * 24 * 60 * 60 * 1000);
}
addWeeks(weeks) {
return new TimeSpan(this.ticks + weeks * 7 * 24 * 60 * 60 * 1000);
}
absolute() {
return new TimeSpan(Math.abs(this.ticks));
}
negate() {
return new TimeSpan(-this.ticks);
}
multiply(factor) {
return new TimeSpan(this.ticks * factor);
}
subtract(span) {
return new TimeSpan(this.ticks - span.ticks);
}
compareTo(another) {
if (this.ticks > another.ticks)
return 1;
if (this.ticks < another.ticks)
return -1;
return 0;
}
add(span) {
return new TimeSpan(this.ticks + span.ticks);
}
static get zero() {
return new TimeSpan(0);
}
toString() {
return `${this.days} - ${this.hours}:${this.minutes}:${this.seconds}.${this.milliseconds}`;
}
get _() {
return this.toString();
}
}
exports.TimeSpan = TimeSpan;
//# sourceMappingURL=TimeSpan.js.map