All files / Scripts MHADates.ts

84.61% Statements 22/26
71.42% Branches 10/14
100% Functions 1/1
84% Lines 21/25

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 635x 5x   5x   5x   5x                   65x   65x       65x 65x 65x 65x 19x 19x         65x 65x   65x   65x     65x 65x 8x     65x                                
import dayjs from "dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat";
 
import { DateWithNum } from "./DateWithNum";
 
export class MHADates {
    static {
        dayjs.extend(localizedFormat);
    }
 
    // parse date using dayjs, with fallback to browser based parsing
    public static parseDate(date: string): DateWithNum {
        // Cross browser dates - ugh!
        // http://dygraphs.com/date-formats.html
 
        // Invert any backwards dates: 2018-01-28 -> 01-28-2018
        // dayjs can handle these, but inverting manually makes it easier for the dash replacement
        date = date.replace(/\s*(\d{4})-(\d{1,2})-(\d{1,2})/g, "$2/$3/$1");
        // Replace dashes with slashes
        date = date.replace(/\s*(\d{1,2})-(\d{1,2})-(\d{4})/g, "$1/$2/$3");
 
        // If we don't have a +xxxx or -xxxx on our date, it will be interpreted in local time
        // This likely isn't the intended timezone, so we add a +0000 to get UTC
        const offset: RegExpMatchArray | null = date.match(/[+|-]\d{4}/);
        const originalDate: string = date;
        let offsetAdded = false;
        if (!offset || offset.length !== 1) {
            date += " +0000";
            offsetAdded = true;
        }
 
        // Some browsers (firefox) don't like milliseconds in dates, and dayjs doesn't hide that from us
        // Trim off milliseconds so we don't pass them into dayjs
        const milliseconds: RegExpMatchArray | null = date.match(/\d{1,2}:\d{2}:\d{2}.(\d+)/);
        date = date.replace(/(\d{1,2}:\d{2}:\d{2}).(\d+)/, "$1");
 
        if (dayjs) {
            // And now we can parse our date
            let time: dayjs.Dayjs = dayjs(date);
 
            // If adding offset didn't work, try adding time and offset
            if (!time.isValid() && offsetAdded) { time = dayjs(originalDate + " 12:00:00 AM +0000"); }
            if (milliseconds && milliseconds.length >= 2) {
                time = time.add(Math.floor(parseFloat("0." + milliseconds[1]) * 1000), "ms");
            }
 
            return new DateWithNum(
                time.valueOf(),
                time.format("l LTS"));
        }
        else E{
            let dateNum = Date.parse(date);
            Iif (milliseconds && milliseconds.length >= 2) {
                dateNum = dateNum + Math.floor(parseFloat("0." + milliseconds[1]) * 1000);
            }
 
            return new DateWithNum(
                dateNum,
                new Date(dateNum).toLocaleString().replace(/\u200E|,/g, ""));
        }
    }
}