lukehamilton
8/13/2012 - 7:30 PM

Beginnings of function to disassemble a timestamp of various formats into its component units...

Beginnings of function to disassemble a timestamp of various formats into its component units...

function disassembleTimestamp(timestamp, formatIn) {

    var timestampOffsets = {},
        timestampComponents = {},
        unitCharacters = null,
        unitString = null,
        startAt = null,
        endAt = null;

    // timestampFormat values arrays of the character offsets for each unit in
    // the timestamp... [ [yearStart, yearEnd], [ monthStart, monthEnd], ... ]

    timestampOffsets = {
        'javaPropertiesFile': {
                                'year':  [0, 4],
                                'month': [5, 7],
                                'day': [8, 10],
                                'hour': [11, 13],
                                'minute': [15, 17],
                                'second': [19, 21],
                                'timezoneOffset': [21, 25] 
        },                              
        'nativeJavaScriptDate': {
                                'dayAbrv': [0, 3],
                                'monthAbrv': [4, 7],
                                'year': [11, 15],
                                'hour': [16, 18],
                                'minute': [19, 21],
                                'second': [22, 24],
                                'timezoneOffset': [25, 33],
                                'timezone': [35, 38]
        }
    }

    unitOffsets = timestampOffsets[formatIn];

    for (var unit in unitOffsets) {
        startAt = unitOffsets[unit][0];
        endAt = unitOffsets[unit][1];
        unitCharacters = timestamp.substring(startAt, endAt);
        unitString = unitCharacters.toString();
        timestampComponents[unit] = unitString;
    }

    return timestampComponents;

}