Skip to content

Instantly share code, notes, and snippets.

@thomaslang
Created September 6, 2009 21:40
Show Gist options
  • Save thomaslang/182004 to your computer and use it in GitHub Desktop.
Save thomaslang/182004 to your computer and use it in GitHub Desktop.
/**
* Global formatters
* from display to internal representation and back
*
* @author Thomas Langemann
*/
sc_require('core');
BB.mixin({
format: function(propertyType, value){
// if no value return empty string
if (SC.none(value)) return '';
switch (propertyType) {
case BB.DECIMAL_2:
var d = value.toString();
var l = d.length;
var point = ",";
var precision = 2;
var integer = d.substr(0,l-precision);
var decimals = d.substr(l-precision);
if (!integer) integer = '0';
while (decimals.length < precision) {
decimals = '0' + decimals;
}
return integer + point + decimals;
break;
case BB.DATE:
return value.toFormattedString("%d.%m.%Y");
break;
case BB.DATE_TIME:
return value.toFormattedString("%d.%m.%Y - %H:%M:%S");
break;
default:
return value;
}
},
deformat: function(propertyType, value){
// if empty value return null
if (!value) return null;
switch (propertyType) {
case BB.DECIMAL_2:
var point = ",";
var precision = 2;
var d = value.split(point);
var integer = d[0] || '0';
var decimals = d[1] || '0';
while (decimals.length < precision) {
decimals = decimals + '0';
}
decimals = decimals.substr(0,precision);
return parseInt(integer) * Math.pow(10,precision) + parseInt(decimals);
break;
case BB.DATE:
var d = value.split('.');
return SC.DateTime.create({day: d[0], month: d[1], year: d[2]});
break;
case BB.DATE_TIME:
var dt = value.split(' - ');
var d = dt[0].split('.');
var t = dt[1].split(':');
return SC.DateTime.create({day: d[0], month: d[1], year: d[2],
hour: t[0], minute: t[1], second: t[2]});
break;
default:
return value;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment