<commit_before>export function parseTime($time: string) {
    const parts = $time.split(':').slice(0, 2);
    const lowercased = $time.trim().toLowerCase();
    const isAM = lowercased.endsWith('am') || lowercased.endsWith('a.m.');
    const isPM = lowercased.endsWith('pm') || lowercased.endsWith('p.m.');

    let hours = parts.length > 0 ? parseInt(parts[0]) : 0;
    if (isNaN(hours) || hours > 23) {
        hours = 0;
    }
    if (isAM && hours === 12) {
        hours = 0;
    }
    if (isPM && hours < 12) {
        hours += 12;
    }

    let minutes = parts.length > 1 ? parseInt(parts[1]) : 0;
    if (isNaN(minutes) || minutes > 59) {
        minutes = 0;
    }

    return [hours, minutes];
}

function parse24HTime(time: string) {
    return time.split(':').map((x) => parseInt(x));
}

function compareTime(time1: number[], time2: number[]) {
    if (time1[0] === time2[0] && time1[1] === time2[1]) {
        return 0;
    }
    if (time1[0] < time2[0] || (time1[0] === time2[0] && time1[1] < time2[1])) {
        return -1;
    }
    return 1;
}

export function nextTimeInterval(time0: string, time1: string, date: Date = new Date()): number {
    const a = parse24HTime(time0);
    const b = parse24HTime(time1);
    const t = [date.getHours(), date.getMinutes()];

    // Ensure a <= b
    if (compareTime(a, b) > 0) {
        return nextTimeInterval(time1, time0, date);
    }

    if (compareTime(a, b) === 0) {
        return null;
    }

    if (compareTime(t, a) < 0) {
        // t < a <= b
        // Schedule for todate at time a
        date.setHours(a[0]);
        date.setMinutes(a[1]);
        date.setSeconds(0);
        date.setMilliseconds(0);
        // Convert into UTC timezone Unix time.
        return date.getTime() + date.getTimezoneOffset() * 60 * 1000;
    }

    if (compareTime(t, b) < 0) {
        // a <= t < b
        // Schedule for today at time b
        date.setHours(b[0]);
        date.setMinutes(b[1]);
        date.setSeconds(0);
        date.setMilliseconds(0);
        // Convert into UTC timezone Unix time.
        return date.getTime() + date.getTimezoneOffset() * 60 * 60 * 1000;
    }

    // a <= b <= t
    // Schedule for tomorrow at time a
    return (new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, a[0], a[1])).getTime();<commit_msg>Add timeZoneOffset via `setMinutes` (#8634)<commit_after>export function parseTime($time: string) {
    const parts = $time.split(':').slice(0, 2);
    const lowercased = $time.trim().toLowerCase();
    const isAM = lowercased.endsWith('am') || lowercased.endsWith('a.m.');
    const isPM = lowercased.endsWith('pm') || lowercased.endsWith('p.m.');

    let hours = parts.length > 0 ? parseInt(parts[0]) : 0;
    if (isNaN(hours) || hours > 23) {
        hours = 0;
    }
    if (isAM && hours === 12) {
        hours = 0;
    }
    if (isPM && hours < 12) {
        hours += 12;
    }

    let minutes = parts.length > 1 ? parseInt(parts[1]) : 0;
    if (isNaN(minutes) || minutes > 59) {
        minutes = 0;
    }

    return [hours, minutes];
}

function parse24HTime(time: string) {
    return time.split(':').map((x) => parseInt(x));
}

function compareTime(time1: number[], time2: number[]) {
    if (time1[0] === time2[0] && time1[1] === time2[1]) {
        return 0;
    }
    if (time1[0] < time2[0] || (time1[0] === time2[0] && time1[1] < time2[1])) {
        return -1;
    }
    return 1;
}

export function nextTimeInterval(time0: string, time1: string, date: Date = new Date()): number {
    const a = parse24HTime(time0);
    const b = parse24HTime(time1);
    const t = [date.getHours(), date.getMinutes()];

    // Ensure a <= b
    if (compareTime(a, b) > 0) {
        return nextTimeInterval(time1, time0, date);
    }

    if (compareTime(a, b) === 0) {
        return null;
    }

    if (compareTime(t, a) < 0) {
        // t < a <= b
        // Schedule for todate at time a
        date.setHours(a[0]);
        // Add the timezoneOffset to ensure unix time returns it in UTC timezone.
        date.setMinutes(a[1] + date.getTimezoneOffset());
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    }

    if (compareTime(t, b) < 0) {
        // a <= t < b
        // Schedule for today at time b
        date.setHours(b[0]);
        // Add the timezoneOffset to ensure unix time returns it in UTC timezone.
        date.setMinutes(b[1] + date.getTimezoneOffset());
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    }

    // a <= b <= t
    // Schedule for tomorrow at time a
    return (new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1, a[0], a[1])).getTime();
{'fraction_non_alphanumeric': 0.17116160543639838, 'fraction_numerical': 0.034826927160756, 'mean_word_length': 2.3763440860215055, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6841547', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1638, 'n_words': 408}