From ca871a80215bd31bc714ac2eae20dbda8eb442aa Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Mon, 17 Jun 2019 14:37:10 -0400 Subject: [PATCH] wip --- package-lock.json | 16 ++++ package.json | 2 + src/app/reports/balancesheet.ts | 4 +- src/app/reports/income.ts | 12 ++- src/app/shared/util.ts | 127 +++++++++++++++++++++++++++++--- src/app/transaction/list.ts | 21 +----- 6 files changed, 143 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3f8ff3b..00e509b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -595,6 +595,14 @@ "@types/jasmine": "*" } }, + "@types/moment-timezone": { + "version": "0.5.12", + "resolved": "https://registry.npmjs.org/@types/moment-timezone/-/moment-timezone-0.5.12.tgz", + "integrity": "sha512-hnHH2+Efg2vExr/dSz+IX860nSiyk9Sk4pJF2EmS11lRpMcNXeB4KBW5xcgw2QPsb9amTXdsVNEe5IoJXiT0uw==", + "requires": { + "moment": ">=2.14.0" + } + }, "@types/node": { "version": "8.9.5", "resolved": "http://registry.npmjs.org/@types/node/-/node-8.9.5.tgz", @@ -6408,6 +6416,14 @@ "minimist": "0.0.8" } }, + "moment-timezone": { + "version": "0.5.25", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.25.tgz", + "integrity": "sha512-DgEaTyN/z0HFaVcVbSyVCUU6HeFdnNC3vE4c9cgu2dgMTvjBUBdBzWfasTBmAW45u5OIMeCJtU8yNjM22DHucw==", + "requires": { + "moment": ">= 2.9.0" + } + }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/package.json b/package.json index d55366f..3befa66 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,10 @@ "@angular/platform-browser-dynamic": "^6.1.0", "@angular/router": "^6.1.0", "@ng-bootstrap/ng-bootstrap": "^3.3.1", + "@types/moment-timezone": "^0.5.12", "bootstrap": "^4.1.3", "core-js": "^2.5.4", + "moment-timezone": "^0.5.25", "rxjs": "~6.2.0", "rxjs-compat": "6.0.0", "zone.js": "~0.8.26" diff --git a/src/app/reports/balancesheet.ts b/src/app/reports/balancesheet.ts index a8e40db..c03713c 100644 --- a/src/app/reports/balancesheet.ts +++ b/src/app/reports/balancesheet.ts @@ -63,7 +63,7 @@ export class BalanceSheetReport { } this.form = fb.group({ - date: [Util.getLocalDateString(this.date), Validators.required], + date: [Util.getLocalDateStringExcl(this.date), Validators.required], priceSource: [this.priceSource, Validators.required] }); } @@ -133,7 +133,7 @@ export class BalanceSheetReport { this.treeSubscription.unsubscribe(); //this.dataService.setLoading(true); this.showOptionsForm = false; - this.date = Util.getDateFromLocalDateString(this.form.value.date); + this.date = Util.getDateFromLocalDateStringExcl(this.form.value.date); this.priceSource = this.form.value.priceSource; let reportData = this.configService.get('reportData'); diff --git a/src/app/reports/income.ts b/src/app/reports/income.ts index cc614b8..2739a56 100644 --- a/src/app/reports/income.ts +++ b/src/app/reports/income.ts @@ -45,10 +45,9 @@ export class IncomeReport { private configService: ConfigService, private sessionService: SessionService) { this.startDate = new Date(); - this.startDate.setDate(1); - this.startDate.setHours(0, 0, 0, 0); - this.endDate = new Date(this.startDate); - this.endDate.setMonth(this.startDate.getMonth() + 1); + Util.setFirstOfMonth(this.startDate); + Util.setBeginOfDay(this.startDate); + this.endDate = Util.getOneMonthLater(this.startDate); let reportData = this.configService.get('reportData'); @@ -65,7 +64,7 @@ export class IncomeReport { this.form = fb.group({ startDate: [Util.getLocalDateString(this.startDate), Validators.required], - endDate: [Util.getLocalDateString(new Date(this.endDate.getTime() - 1)), Validators.required] + endDate: [Util.getLocalDateStringExcl(this.endDate), Validators.required] }); } @@ -92,8 +91,7 @@ export class IncomeReport { //this.dataService.setLoading(true); this.showDateForm = false; this.startDate = Util.getDateFromLocalDateString(this.form.value.startDate); - this.endDate = Util.getDateFromLocalDateString(this.form.value.endDate); - this.endDate.setDate(this.endDate.getDate() + 1); + this.endDate = Util.getDateFromLocalDateStringExcl(this.form.value.endDate); let reportData = this.configService.get('reportData'); diff --git a/src/app/shared/util.ts b/src/app/shared/util.ts index 57ca01e..1dba80f 100644 --- a/src/app/shared/util.ts +++ b/src/app/shared/util.ts @@ -1,8 +1,15 @@ +//import * as moment from 'moment-timezone/builds/moment-timezone-with-data-2012-2022.min'; +import * as moment from 'moment-timezone'; + +const defaultTz = moment.tz.guess(); + export class Util { - static getLocalDateString(input: Date) { - let year = input.getFullYear().toString(); - let month = (input.getMonth() + 1).toString(); - let date = input.getDate().toString(); + static getLocalDateString(input: Date, tz: string = defaultTz) { + let m = moment(input).tz(tz); + + let year = m.format('YYYY'); + let month = m.format('MM'); + let date = m.format('DD'); if(month.length < 2) { month = '0' + month; @@ -15,14 +22,112 @@ export class Util { return year + '-' + month + '-' + date; } - static getDateFromLocalDateString(input: string) { - let parts = input.split('-'); - let date = new Date(); - date.setHours(0, 0, 0, 0); - date.setFullYear(parseInt(parts[0])); - date.setMonth(parseInt(parts[1]) - 1, parseInt(parts[2])); + static getLocalDateStringExcl(input: Date, tz: string = defaultTz) { + let m = moment(input.getTime() - 1).tz(tz); - return date; + let year = m.format('YYYY'); + let month = m.format('MM'); + let date = m.format('DD'); + + if(month.length < 2) { + month = '0' + month; + } + + if(date.length < 2) { + date = '0' + date; + } + + return year + '-' + month + '-' + date; + } + + static getDateFromLocalDateString(input: string, tz: string = defaultTz) { + let parts = input.split('-'); + + let m = moment().tz(tz); + m.hours(0); + m.minutes(0); + m.seconds(0); + m.milliseconds(0); + m.year(parseInt(parts[0])); + m.month(parseInt(parts[1]) - 1); + m.date(parseInt(parts[2])); + + return m.toDate(); + } + + static getDateFromLocalDateStringExcl(input: string, tz: string = defaultTz) { + let parts = input.split('-'); + + let m = moment().tz(tz); + m.hours(0); + m.minutes(0); + m.seconds(0); + m.milliseconds(0); + m.year(parseInt(parts[0])); + m.month(parseInt(parts[1]) - 1); + m.date(parseInt(parts[2]) + 1); + + return m.toDate(); + } + + static setFirstOfMonth(input: Date, tz: string = defaultTz) { + let m = moment(input).tz(tz); + + m.date(1); + input.setTime(m.valueOf()); + } + + static setBeginOfDay(input: Date, tz: string = defaultTz) { + let m = moment(input).tz(tz); + + m.hours(0); + m.minutes(0); + m.seconds(0); + m.milliseconds(0); + input.setTime(m.valueOf()); + } + + static setEndOfDay(input: Date, tz: string = defaultTz) { + let m = moment(input).tz(tz); + + m.hours(23); + m.minutes(59); + m.seconds(59); + m.milliseconds(999); + input.setTime(m.valueOf()); + } + + static getOneMonthLater(input: Date, tz: string = defaultTz): Date { + let m = moment(input).tz(tz); + + m.month(m.month() + 1); + + return m.toDate(); + } + + static computeTransactionDate(formDate: Date, txDate: Date, tz: string = defaultTz): Date { + if(!formDate || !formDate.getTime()) { + return txDate; + } + + let formMoment = moment(formDate).tz(tz); + let txMoment = moment(txDate).tz(tz); + + // make the time be at the very end of the day + formMoment.hours(23); + formMoment.minutes(59); + formMoment.seconds(59); + formMoment.milliseconds(999); + + let sameDay = formMoment.year() === txMoment.year() && + formMoment.month() === txMoment.month() && + formMoment.date() === txMoment.date(); + + if(!sameDay) { + return formMoment.toDate(); + } + + return txDate; } static newGuid() { diff --git a/src/app/transaction/list.ts b/src/app/transaction/list.ts index 0d2db3d..d0fa5d7 100644 --- a/src/app/transaction/list.ts +++ b/src/app/transaction/list.ts @@ -624,7 +624,7 @@ export class TxListPage implements OnInit, AfterViewChecked { let date = item.tx.id ? item.tx.date : new Date(); let formDate = Util.getDateFromLocalDateString(item.form.value.date); - date = this.computeTransactionDate(formDate, date); + date = Util.computeTransactionDate(formDate, date); let tx = new Transaction({ id: item.tx.id, @@ -731,23 +731,6 @@ export class TxListPage implements OnInit, AfterViewChecked { } } - computeTransactionDate(formDate: Date, txDate: Date): Date { - if(formDate.getTime()) { - // make the time be at the very end of the day - formDate.setHours(23, 59, 59, 999); - } - - let sameDay = formDate.getFullYear() === txDate.getFullYear() && - formDate.getMonth() === txDate.getMonth() && - formDate.getDate() === txDate.getDate(); - - if(formDate.getTime() && !sameDay) { - txDate = formDate; - } - - return txDate; - } - deleteTransaction(item) { this.modalService.open(this.confirmDeleteModal).result.then((result) => { this.log.debug('delete'); @@ -846,7 +829,7 @@ export class TxListPage implements OnInit, AfterViewChecked { item.tx = new Transaction( { id: item.tx.id, - date: this.computeTransactionDate(formDate, new Date()), + date: Util.computeTransactionDate(formDate, new Date()), description: tx.description, splits: tx.splits }