Success! Looks like we've fixed this one. According to our records the fix was applied for TOUCH-4145 in a recent build.
  1. #1
    Sencha Premium Member
    Join Date
    Apr 2008
    Posts
    263
    Vote Rating
    26
    themightychris will become famous soon enough themightychris will become famous soon enough

      0  

    Default 2.1/2.2: WebStorage proxies corrupt timestamp/time date formats

    2.1/2.2: WebStorage proxies corrupt timestamp/time date formats


    Ext.data.proxy.WebStorage uses code like this to encode/decode date fields:

    Code:
    dateFormat = field.getDateFormat();
    if (dateFormat) {
    	data[name] = Ext.Date.format(rawData[name], dateFormat);
    } else {
    	data[name] = rawData[name].getTime();
    }
    If dateFormat is "time" or "timestamp", it will use that as the format string, producing timestamps like this "300004e0030am04p"

    Ext.data.writer.Writer and Ext.data.proxy.Sql both have similar-but-not-identical writeDate methods that have smarts for this:
    Code:
    // Ext.data.writer.Writer
    writeDate: function(field, date) {
    	if (!date) {
    		return null;
    	}
    
    	var dateFormat = field.getDateFormat() || 'timestamp';
    	switch (dateFormat) {
    		case 'timestamp':
    			return date.getTime()/1000;
    		case 'time':
    			return date.getTime();
    		default:
    			return Ext.Date.format(date, dateFormat);
    	}
    }
    
    // Ext.data.proxy.Sql
    writeDate: function (field, date) {
    	if (Ext.isEmpty(date)) {
    		return null;
    	}
    
    	var dateFormat = field.getDateFormat() || this.getDefaultDateFormat();
    	switch (dateFormat) {
    		case 'timestamp':
    			return date.getTime() / 1000;
    		case 'time':
    			return date.getTime();
    		default:
    			return Ext.Date.format(date, dateFormat);
    	}
    }
    How about moving that logic into field and let it worry about encoding/decoding itself?

    getRecord has the same problem, but the fix there is even simpler -- leave the date alone and let the model convert it.

    Here's my patch for 2.1.1, may work for 2.2b also:
    Code:
    Ext.define('Jarvus.patch.WebStorageDates', {
        override: 'Ext.data.proxy.WebStorage',
    
        // respect 'timestamp' and 'time' dateFormats
        getRecord: function(id) {
            if (this.cache[id] === undefined) {
                var recordKey = this.getRecordKey(id),
                    item = this.getStorageObject().getItem(recordKey),
                    data    = {},
                    Model   = this.getModel(),
                    fields  = Model.getFields().items,
                    length  = fields.length,
                    i, field, name, record, rawData, dateFormat;
    
                if (!item) {
                    return undefined;
                }
    
                rawData = Ext.decode(item);
    
                for (i = 0; i < length; i++) {
                    field = fields[i];
                    name  = field.getName();
    
                    if (typeof field.getDecode() == 'function') {
                        data[name] = field.getDecode()(rawData[name]);
                    } else {
                        data[name] = rawData[name];
                    }
                }
    
                record = new Model(data, id);
                this.cache[id] = record;
            }
    
            return this.cache[id];
        },
    
        // respect 'timestamp' and 'time' dateFormats
        setRecord: function(record, id) {
            if (id) {
                record.setId(id);
            } else {
                id = record.getId();
            }
    
            var me = this,
                rawData = record.getData(),
                data    = {},
                Model   = me.getModel(),
                fields  = Model.getFields().items,
                length  = fields.length,
                i = 0,
                field, name, obj, key, dateFormat;
    
            for (; i < length; i++) {
                field = fields[i];
                name  = field.getName();
    
                if (field.getPersist() === false) {
                    continue;
                }
    
                if (typeof field.getEncode() == 'function') {
                    data[name] = field.getEncode()(rawData[name], record);
                } else {
                    if (field.getType().type == 'date' && Ext.isDate(rawData[name])) {
                        dateFormat = field.getDateFormat() || 'timestamp';
                        switch (dateFormat) {
                            case 'timestamp':
                                data[name] = rawData[name].getTime()/1000;
                                break;
                            case 'time':
                                data[name] = rawData[name].getTime();
                                break;
                            default:
                                data[name] = Ext.Date.format(rawData[name], dateFormat);
                        }
                    } else {
                        data[name] = rawData[name];
                    }
                }
            }
    
            obj = me.getStorageObject();
            key = me.getRecordKey(id);
    
            //keep the cache up to date
            me.cache[id] = record;
    
            //iPad bug requires that we remove the item before setting it
            obj.removeItem(key);
            try {
                obj.setItem(key, Ext.encode(data));
            } catch(e){
                this.fireEvent('exception', this, e);
            }
    
            record.commit();
        }
    });
    Chief Architect @ Jarv.us Innovations
    Co-captain @ Code for Philly

    Jarvus builds and optimizes top-quality Sencha Touch and ExtJS apps for open-source projects and clients of all sizes.

    Don't waste time with bugs that have already been found and fixed by the community, compile our tried and tested hotfixes packages into all your projects: https://github.com/JarvusInnovations/sencha-hotfixes

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,408
    Vote Rating
    851
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Thanks for the report! I have opened a bug in our bug tracker.

Thread Participants: 1