import {Buffer} from "buffer"; import {Unsigned16} from "./Unsigned16"; import {Unsigned8} from "./Unsigned8"; import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; export class Date implements AxdrType{ dataCode: Buffer | null = null; year: Unsigned16 | null = null; month: Unsigned8 | null = null; day_of_month: Unsigned8 | null = null; day_of_week: Unsigned8 | null = null; constructor() { } set_dataCode(dataCode: Buffer) { this.dataCode = dataCode; } set_date(year: Unsigned16, month: Unsigned8, day_of_month: Unsigned8, day_of_week : Unsigned8) { this.year = year; this.month = month; this.day_of_month = day_of_month; this.day_of_week = day_of_week; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; this.year = new Unsigned16(); this.year.set_const() codeLength += this.year.decode(input); this.month = new Unsigned8(); this.month.set_const() codeLength += this.month.decode(input); this.day_of_month = new Unsigned8(); this.day_of_month.set_const() codeLength += this.day_of_month.decode(input); this.day_of_week = new Unsigned8(); this.day_of_week.set_const() codeLength += this.day_of_week.decode(input); return codeLength; } encode(output: ReverseByteArrayOutputStream): number { let codeLength; if (this.dataCode != null) { codeLength = this.dataCode.length; for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } } else { if(this.year != null && this.month != null && this.day_of_week != null && this.day_of_month != null){ codeLength = 0; codeLength += this.day_of_week.encode(output); codeLength += this.day_of_month.encode(output); codeLength += this.month.encode(output); codeLength += this.year.encode(output); }else{ throw new Error("date is null"); } } return codeLength; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } toString() : string{ if(this.year != null && this.month != null && this.day_of_month != null && this.day_of_week != null){ return "sequence: {"+ "year: " + this.year + ", month: " + this.month + ", day_of_month: " + this.day_of_month + ", day_of_week" + this.day_of_week + "}"; }else{ return "date is null"; } } }