All files / src/utils dateAndTime.ts

96.77% Statements 30/31
60% Branches 6/10
100% Functions 10/10
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1033x                                 3x           11x 11x 11x   11x       177x       62x       89x       300x 57x   300x       2x       2x 2x 1x     1x       171x         114x 114x 114x 114x       5x   5x 2x     3x               5x   5x 2x     3x              
import * as moment from "moment";
 
// These are meant to parse the date and time formats
// supported by Joplin. It doesn't support seconds or
// milliseconds.
interface ParsedDate {
    date: number;
    month: number;
    year: number;
}
 
interface ParsedTime {
    hours: number;
    minutes: number;
    seconds: number;
}
 
export class DateAndTimeUtils {
    private locale: string;
    private dateFormat: string;
    private timeFormat: string;
 
    constructor(locale: string, dateFormat: string, timeFormat: string) {
        this.locale = locale;
        this.dateFormat = dateFormat;
        this.timeFormat = timeFormat;
 
        moment.locale(this.locale);
    }
 
    public getDateFormat(): string {
        return this.dateFormat;
    }
 
    public getTimeFormat(): string {
        return this.timeFormat;
    }
 
    public getDateTimeFormat(): string {
        return `${this.dateFormat} ${this.timeFormat}`;
    }
 
    public formatMsToLocal(ms: number, format: string | null = null): string {
        if (!format) {
            format = this.getDateTimeFormat();
        }
        return moment(ms).format(format);
    }
 
    public formatLocalToJoplinCompatibleUnixTime(input: string, format: string | null = null): number {
        Iif (!format) {
            format = this.getDateTimeFormat();
        }
 
        const date = moment(input, format, true);
        if (!date.isValid()) {
            throw new Error(`Was not able to parse ${input} according to format ${format}`);
        }
 
        return date.unix() * 1000;
    }
 
    public getCurrentTime(format: string | null = null): string {
        return this.formatMsToLocal(new Date().getTime(), format);
    }
 
    public getBeginningOfWeek(startIndex: number): number {
        // startIndex: 0 for Sunday, 1 for Monday
        const currentDate = new Date();
        const day = currentDate.getDay();
        const diff = day >= startIndex ? day - startIndex : 6 - day;
        return new Date().setDate(currentDate.getDate() - diff);
    }
 
    public parseDate(input: string, format: string): ParsedDate {
        const date = moment(input, format, true);
 
        if (!date.isValid()) {
            throw new Error(`Was not able to parse ${input} according to format ${format}`);
        }
 
        return {
            date: date.date(),
            month: date.month(),
            year: date.year(),
        };
    }
 
    public parseTime(input: string, format: string): ParsedTime {
        const time = moment(input, format, true);
 
        if (!time.isValid()) {
            throw new Error(`Was not able to parse ${input} according to format ${format}`);
        }
 
        return {
            hours: time.hours(),
            minutes: time.minutes(),
            seconds: time.seconds(),
        };
    }
}