separate out date functions

This commit is contained in:
Patrick Nagurny
2019-06-27 16:37:46 -04:00
parent 5b720b79c4
commit 714a1b187b
12 changed files with 220 additions and 216 deletions

View File

@@ -7,7 +7,7 @@ import { SessionService } from '../core/session.service';
import { Transaction, Split } from '../shared/transaction';
import { Org } from '../shared/org';
import { Account, AccountTree } from '../shared/account';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { TxListPage } from '../transaction/list';
import { IncomeReport } from '../reports/income';
import { Observable } from 'rxjs/Observable';
@@ -51,7 +51,7 @@ export class DashboardPage implements OnInit {
this.org = this.orgService.getCurrentOrg();
this.log.debug('org', this.org);
let periodStart = Util.getPeriodStart(this.org.timezone);
let periodStart = DateUtil.getPeriodStart(this.org.timezone);
let tree$ = this.accountService.getAccountTreeWithPeriodBalance(periodStart);

View File

@@ -14,6 +14,7 @@ import { Org } from '../shared/org';
import { Invite } from '../shared/invite';
import { AppError } from '../shared/error';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
@Component({
selector: 'app-org',
@@ -42,8 +43,8 @@ export class OrgPage {
private fb: FormBuilder
) {
this.timezones = Util.getTimezones();
this.defaultTz = Util.getDefaultTimezone();
this.timezones = DateUtil.getTimezones();
this.defaultTz = DateUtil.getDefaultTimezone();
console.log('defaultTz', this.defaultTz);
this.invites = null;

View File

@@ -11,6 +11,7 @@ import {
AbstractControl
} from '@angular/forms';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { PriceService } from '../core/price.service';
import { Price } from '../shared/price';
import { SessionService } from '../core/session.service';
@@ -36,7 +37,7 @@ export class PriceModal {
private fb: FormBuilder
) {
this.org = this.sessionService.getOrg();
let dateString = Util.getLocalDateString(new Date(), this.org.timezone);
let dateString = DateUtil.getLocalDateString(new Date(), this.org.timezone);
this.form = fb.group({
'id': [null],
@@ -52,7 +53,7 @@ export class PriceModal {
this.form.patchValue({
id: data.id,
currency: data.currency,
date: Util.getLocalDateString(data.date, this.org.timezone),
date: DateUtil.getLocalDateString(data.date, this.org.timezone),
price: data.price
});
}
@@ -61,11 +62,11 @@ export class PriceModal {
this.error = null;
let date = this.form.value.id ? this.originalDate : new Date();
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
if(formDate.getTime() && !Util.isSameDay(date, formDate, this.org.timezone)) {
if(formDate.getTime() && !DateUtil.isSameDay(date, formDate, this.org.timezone)) {
// make the time be at the very end of the day
Util.setEndOfDay(formDate, this.org.timezone);
DateUtil.setEndOfDay(formDate, this.org.timezone);
date = formDate;
}

View File

@@ -13,7 +13,6 @@ import { PriceService } from '../core/price.service';
import { Price } from '../shared/price';
import { Org } from '../shared/org';
import { AppError } from '../shared/error';
import { Util } from '../shared/util';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';

View File

@@ -17,6 +17,7 @@ import { Transaction } from '../shared/transaction';
import { Org } from '../shared/org';
import { AppError } from '../shared/error';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { ReconcileModal } from './reconcile-modal';
import { Reconciliation } from './reconciliation';
@@ -87,8 +88,8 @@ export class ReconcilePage {
let value = this.newReconcile.getRawValue();
let rec = new Reconciliation();
rec.startDate = Util.getDateFromLocalDateString(value.startDate, this.org.timezone);
rec.endDate = Util.getDateFromLocalDateString(value.endDate, this.org.timezone);
rec.startDate = DateUtil.getDateFromLocalDateString(value.startDate, this.org.timezone);
rec.endDate = DateUtil.getDateFromLocalDateString(value.endDate, this.org.timezone);
rec.startBalance = Math.round(parseFloat(value.startBalance) * Math.pow(10, this.account.precision));
rec.endBalance = Math.round(parseFloat(value.endBalance) * Math.pow(10, this.account.precision));
@@ -106,7 +107,7 @@ export class ReconcilePage {
this.newReconcile.patchValue(
{
startDate: Util.getLocalDateString(rec.endDate, this.org.timezone),
startDate: DateUtil.getLocalDateString(rec.endDate, this.org.timezone),
startBalance: rec.endBalance / Math.pow(10, this.account.precision),
endBalance: 0,
endDate: ''
@@ -203,7 +204,7 @@ export class ReconcilePage {
if(!dates.length) {
if(firstStartDate) {
this.newReconcile.patchValue({startDate: Util.getLocalDateString(firstStartDate, this.org.timezone)});
this.newReconcile.patchValue({startDate: DateUtil.getLocalDateString(firstStartDate, this.org.timezone)});
}
return;
}
@@ -228,7 +229,7 @@ export class ReconcilePage {
this.newReconcile.patchValue(
{
startDate: Util.getLocalDateString(lastRec.endDate, this.org.timezone),
startDate: DateUtil.getLocalDateString(lastRec.endDate, this.org.timezone),
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
}
);
@@ -272,7 +273,7 @@ export class ReconcilePage {
if(lastRec) {
this.newReconcile.patchValue(
{
startDate: Util.getLocalDateString(lastRec.endDate, this.org.timezone),
startDate: DateUtil.getLocalDateString(lastRec.endDate, this.org.timezone),
startBalance: lastRec.endBalance / Math.pow(10, this.account.precision)
}
);

View File

@@ -17,7 +17,7 @@ import {
ValidationErrors
} from '@angular/forms';
import { AppError } from '../shared/error';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
@Component({
selector: 'app-balancesheet',
@@ -65,7 +65,7 @@ export class BalanceSheetReport {
this.org = this.orgService.getCurrentOrg();
this.form = fb.group({
date: [Util.getLocalDateStringExcl(this.date, this.org.timezone), Validators.required],
date: [DateUtil.getLocalDateStringExcl(this.date, this.org.timezone), Validators.required],
priceSource: [this.priceSource, Validators.required]
});
}
@@ -134,7 +134,7 @@ export class BalanceSheetReport {
this.treeSubscription.unsubscribe();
//this.dataService.setLoading(true);
this.showOptionsForm = false;
this.date = Util.getDateFromLocalDateStringExcl(this.form.value.date, this.org.timezone);
this.date = DateUtil.getDateFromLocalDateStringExcl(this.form.value.date, this.org.timezone);
this.priceSource = this.form.value.priceSource;
let reportData = this.configService.get('reportData');

View File

@@ -18,7 +18,7 @@ import {
ValidationErrors
} from '@angular/forms';
import { AppError } from '../shared/error';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
@Component({
selector: 'app-income',
@@ -51,9 +51,9 @@ export class IncomeReport {
this.org = this.orgService.getCurrentOrg();
this.startDate = new Date();
Util.setFirstOfMonth(this.startDate, this.org.timezone);
Util.setBeginOfDay(this.startDate, this.org.timezone);
this.endDate = Util.getOneMonthLater(this.startDate, this.org.timezone);
DateUtil.setFirstOfMonth(this.startDate, this.org.timezone);
DateUtil.setBeginOfDay(this.startDate, this.org.timezone);
this.endDate = DateUtil.getOneMonthLater(this.startDate, this.org.timezone);
let reportData = this.configService.get('reportData');
@@ -69,8 +69,8 @@ export class IncomeReport {
}
this.form = this.fb.group({
startDate: [Util.getLocalDateString(this.startDate, this.org.timezone), Validators.required],
endDate: [Util.getLocalDateStringExcl(this.endDate, this.org.timezone), Validators.required]
startDate: [DateUtil.getLocalDateString(this.startDate, this.org.timezone), Validators.required],
endDate: [DateUtil.getLocalDateStringExcl(this.endDate, this.org.timezone), Validators.required]
});
this.treeSubscription = this.accountService.getAccountTreeWithPeriodBalance(this.startDate, this.endDate)
@@ -91,8 +91,8 @@ export class IncomeReport {
this.treeSubscription.unsubscribe();
//this.dataService.setLoading(true);
this.showDateForm = false;
this.startDate = Util.getDateFromLocalDateString(this.form.value.startDate, this.org.timezone);
this.endDate = Util.getDateFromLocalDateStringExcl(this.form.value.endDate, this.org.timezone);
this.startDate = DateUtil.getDateFromLocalDateString(this.form.value.startDate, this.org.timezone);
this.endDate = DateUtil.getDateFromLocalDateStringExcl(this.form.value.endDate, this.org.timezone);
let reportData = this.configService.get('reportData');

175
src/app/shared/dateutil.ts Normal file
View File

@@ -0,0 +1,175 @@
//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 DateUtil {
static getLocalDateString(input: Date, tz: string) {
let m = moment(input).tz(tz || defaultTz);
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 getLocalDateStringExcl(input: Date, tz: string) {
let m = moment(input.getTime() - 1).tz(tz || defaultTz);
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) {
let parts = input.split('-');
let m = moment().tz(tz || defaultTz);
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) {
let parts = input.split('-');
let m = moment().tz(tz || defaultTz);
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) {
let m = moment(input).tz(tz || defaultTz);
m.date(1);
input.setTime(m.valueOf());
}
static setBeginOfDay(input: Date, tz: string) {
let m = moment(input).tz(tz || defaultTz);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
input.setTime(m.valueOf());
}
static setEndOfDay(input: Date, tz: string) {
let m = moment(input).tz(tz || defaultTz);
m.hours(23);
m.minutes(59);
m.seconds(59);
m.milliseconds(999);
input.setTime(m.valueOf());
}
static getOneMonthLater(input: Date, tz: string): Date {
let m = moment(input).tz(tz || defaultTz);
m.month(m.month() + 1);
return m.toDate();
}
static computeTransactionDate(formDate: Date, txDate: Date, tz: string): Date {
if(!formDate || !formDate.getTime()) {
return txDate;
}
let formMoment = moment(formDate).tz(tz || defaultTz);
let txMoment = moment(txDate).tz(tz || defaultTz);
// 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 txDate;
}
if(formDate < txDate) {
// make time end of day for past dates
formMoment.hours(23);
formMoment.minutes(59);
formMoment.seconds(59);
formMoment.milliseconds(999);
} else {
// make time beginning of day for future dates
formMoment.hours(0);
formMoment.minutes(0);
formMoment.seconds(0);
formMoment.milliseconds(0);
}
return formMoment.toDate();
}
static isSameDay(date1: Date, date2: Date, tz: string) {
let m1 = moment(date1).tz(tz || defaultTz);
let m2 = moment(date2).tz(tz || defaultTz);
return m1.year() === m2.year() &&
m1.month() === m2.month() &&
m1.date() === m2.date();
}
static getPeriodStart(tz: string): Date {
let m = moment().tz(tz || defaultTz);
m.date(1);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
return m.toDate();
}
static getTimezones(): string[] {
let timezones = [''];
return timezones.concat(moment.tz.names());
}
static getDefaultTimezone(): string {
return defaultTz;
}
}

View File

@@ -1,178 +1,4 @@
//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, tz: string) {
let m = moment(input).tz(tz || defaultTz);
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 getLocalDateStringExcl(input: Date, tz: string) {
let m = moment(input.getTime() - 1).tz(tz || defaultTz);
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) {
let parts = input.split('-');
let m = moment().tz(tz || defaultTz);
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) {
let parts = input.split('-');
let m = moment().tz(tz || defaultTz);
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) {
let m = moment(input).tz(tz || defaultTz);
m.date(1);
input.setTime(m.valueOf());
}
static setBeginOfDay(input: Date, tz: string) {
let m = moment(input).tz(tz || defaultTz);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
input.setTime(m.valueOf());
}
static setEndOfDay(input: Date, tz: string) {
let m = moment(input).tz(tz || defaultTz);
m.hours(23);
m.minutes(59);
m.seconds(59);
m.milliseconds(999);
input.setTime(m.valueOf());
}
static getOneMonthLater(input: Date, tz: string): Date {
let m = moment(input).tz(tz || defaultTz);
m.month(m.month() + 1);
return m.toDate();
}
static computeTransactionDate(formDate: Date, txDate: Date, tz: string): Date {
if(!formDate || !formDate.getTime()) {
return txDate;
}
let formMoment = moment(formDate).tz(tz || defaultTz);
let txMoment = moment(txDate).tz(tz || defaultTz);
// 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 txDate;
}
if(formDate < txDate) {
// make time end of day for past dates
formMoment.hours(23);
formMoment.minutes(59);
formMoment.seconds(59);
formMoment.milliseconds(999);
} else {
// make time beginning of day for future dates
formMoment.hours(0);
formMoment.minutes(0);
formMoment.seconds(0);
formMoment.milliseconds(0);
}
return formMoment.toDate();
}
static isSameDay(date1: Date, date2: Date, tz: string) {
let m1 = moment(date1).tz(tz || defaultTz);
let m2 = moment(date2).tz(tz || defaultTz);
return m1.year() === m2.year() &&
m1.month() === m2.month() &&
m1.date() === m2.date();
}
static getPeriodStart(tz: string): Date {
let m = moment().tz(tz || defaultTz);
m.date(1);
m.hours(0);
m.minutes(0);
m.seconds(0);
m.milliseconds(0);
return m.toDate();
}
static getTimezones(): string[] {
let timezones = [''];
return timezones.concat(moment.tz.names());
}
static getDefaultTimezone(): string {
return defaultTz;
}
static newGuid() {
let arr = new Uint8Array(16);
window.crypto.getRandomValues(arr);

View File

@@ -15,6 +15,7 @@ import {
AbstractControl
} from '@angular/forms';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { OrgService } from '../core/org.service';
import { TransactionService } from '../core/transaction.service';
@@ -50,7 +51,7 @@ export class AdvancedEdit {
this.org = this.orgService.getCurrentOrg();
let dateString = Util.getLocalDateString(item.tx.date, this.org.timezone);
let dateString = DateUtil.getLocalDateString(item.tx.date, this.org.timezone);
this.form = new FormGroup({
date: new FormControl(dateString),
@@ -110,9 +111,9 @@ export class AdvancedEdit {
this.error = null;
let date = this.item.tx.id ? this.item.tx.date : new Date();
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
let tx = new Transaction({
id: this.item.tx.id,

View File

@@ -18,6 +18,7 @@ import {
} from '@angular/forms';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/mergeMap';
import { AdvancedEdit } from './advancededit';
@@ -76,8 +77,6 @@ export class TxListPage implements OnInit, AfterViewChecked {
this.accountId = this.route.snapshot.paramMap.get('id'); //+this.route.snapshot.paramMap.get('id');
this.org = this.orgService.getCurrentOrg();
console.log(this.org);
this.accountService.getAccountTree().subscribe(tree => {
this.account = tree.accountMap[this.accountId];
this.selectAccounts = tree.getFlattenedAccounts().filter(account => {
@@ -95,7 +94,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
splits: []
});
Util.setEndOfDay(newTx.date, this.org.timezone);
DateUtil.setEndOfDay(newTx.date, this.org.timezone);
newTx.splits.push(new Split({
accountId: this.account.id
@@ -410,7 +409,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
item.editing = true;
let dateString = Util.getLocalDateString(item.tx.date, this.org.timezone);
let dateString = DateUtil.getLocalDateString(item.tx.date, this.org.timezone);
this.log.debug(item);
let debit = this.getDebit(item);
@@ -629,9 +628,9 @@ export class TxListPage implements OnInit, AfterViewChecked {
}
let date = item.tx.id ? item.tx.date : new Date();
let formDate = Util.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
let formDate = DateUtil.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
let tx = new Transaction({
id: item.tx.id,
@@ -714,7 +713,7 @@ export class TxListPage implements OnInit, AfterViewChecked {
splits: []
});
Util.setEndOfDay(newTx.date, this.org.timezone);
DateUtil.setEndOfDay(newTx.date, this.org.timezone);
newTx.splits.push(new Split({
accountId: this.account.id
@@ -832,11 +831,11 @@ export class TxListPage implements OnInit, AfterViewChecked {
autocomplete(item: TxItem, tx: Transaction) {
this.log.debug('chose tx', tx);
let formDate = Util.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
let formDate = DateUtil.getDateFromLocalDateString(item.form.value.date, this.org.timezone);
item.tx = new Transaction(
{
id: item.tx.id,
date: Util.computeTransactionDate(formDate, new Date(), this.org.timezone),
date: DateUtil.computeTransactionDate(formDate, new Date(), this.org.timezone),
description: tx.description,
splits: tx.splits
}

View File

@@ -14,6 +14,7 @@ import { TransactionService } from '../core/transaction.service';
import { OrgService } from '../core/org.service';
import { Account, AccountApi, AccountTree } from '../shared/account';
import { Util } from '../shared/util';
import { DateUtil } from '../shared/dateutil';
import { AppError } from '../shared/error';
import { Transaction, Split } from '../shared/transaction';
import { Logger } from '../core/logger';
@@ -62,7 +63,7 @@ export class NewTransactionPage {
this.numAccountsShown = 3;
this.org = this.orgService.getCurrentOrg();
let dateString = Util.getLocalDateString(new Date(), this.org.timezone);
let dateString = DateUtil.getLocalDateString(new Date(), this.org.timezone);
this.form = this.fb.group({
type: ['', Validators.required],
firstAccountPrimary: [null, Validators.required],
@@ -226,9 +227,9 @@ export class NewTransactionPage {
this.error = null;
let date = new Date();
let formDate = Util.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
let formDate = DateUtil.getDateFromLocalDateString(this.form.value.date, this.org.timezone);
date = Util.computeTransactionDate(formDate, date, this.org.timezone);
date = DateUtil.computeTransactionDate(formDate, date, this.org.timezone);
let tx = new Transaction({
id: Util.newGuid(),