TimeZone Shit

by alr3

HTML

<div id="results"></div>

CSS

#results div { width: 100%; clear: both; padding: 5px; }
.good { background-color: green; }
.bad { background-color: red; }

JavaScript

function time_zone_converter(_original_date, _original_time_zone, _to_time_zone) {
    var original_date = _original_date;
    var original_time_zone = _original_time_zone;
    var to_time_zone = _to_time_zone;

    function parse_time_zone_to_offset(time_zone_name) {
        var utc_hour_offset = time_zone_name.substring(4, 10).split(":");
        if (utc_hour_offset.length < 2) {
            throw 'Invalid time zone name format: ' + time_zone_name;
        }
        
        // convert the offset (hh:mm) to milliseconds

        var offset_in_milliseconds = ((parseInt(utc_hour_offset[0]) * 60) + utc_hour_offset(gmt[1])) * 60 * 1000;
        
        return offset_in_milliseconds;
    }

    this.get_original_time_zone_offset = function() { return parse_time_zone_to_offset(_original_time_zone); };
    
    this.get_to_time_zone_offset = function() { return parse_time_zone_to_offset(_to_time_zone); };

    function get_original_utc() {
        return null;
    }

}

function a(is_good, txt) {
    $('#resulst').appendTo('<div class="' + (is_good ? 'pass' : 'fail') + '">' + txt + '</div>');
}

var tzc = new time_zone_converter(new Date(), "Invalid", "Invalid");

try {
    var shouldnt_exist = tzc.get_original_time_zone_offset;
    a(false, shouldnt_exist + ' shouldn\'t exist');
} catch(ex) {
    if (ex === 'Invalid time zone name format: Invalid') {
        a(true, 'Throws error on invalid times correctly!');
    } else {
        a(false, 'Unexpected Error Message: ' + ex);
    }
}