PageRenderTime 22ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/scripts/units.js

http://showslow.googlecode.com/
JavaScript | 73 lines | 54 code | 15 blank | 4 comment | 16 complexity | 12f4c9c9bf895401703c6ac74cc31cb4 MD5 | raw file
  1. /*==================================================
  2. * Default Unit
  3. *==================================================
  4. */
  5. SimileAjax.NativeDateUnit = new Object();
  6. SimileAjax.NativeDateUnit.makeDefaultValue = function() {
  7. return new Date();
  8. };
  9. SimileAjax.NativeDateUnit.cloneValue = function(v) {
  10. return new Date(v.getTime());
  11. };
  12. SimileAjax.NativeDateUnit.getParser = function(format) {
  13. if (typeof format == "string") {
  14. format = format.toLowerCase();
  15. }
  16. var parser = (format == "iso8601" || format == "iso 8601") ?
  17. SimileAjax.DateTime.parseIso8601DateTime :
  18. SimileAjax.DateTime.parseGregorianDateTime;
  19. return function(d) {
  20. if (typeof d != 'undefined' && typeof d.toUTCString == "function") {
  21. return d;
  22. } else {
  23. return parser(d);
  24. }
  25. };
  26. };
  27. SimileAjax.NativeDateUnit.parseFromObject = function(o) {
  28. return SimileAjax.DateTime.parseGregorianDateTime(o);
  29. };
  30. SimileAjax.NativeDateUnit.toNumber = function(v) {
  31. return v.getTime();
  32. };
  33. SimileAjax.NativeDateUnit.fromNumber = function(n) {
  34. return new Date(n);
  35. };
  36. SimileAjax.NativeDateUnit.compare = function(v1, v2) {
  37. var n1, n2;
  38. if (typeof v1 == "object") {
  39. n1 = v1.getTime();
  40. } else {
  41. n1 = Number(v1);
  42. }
  43. if (typeof v2 == "object") {
  44. n2 = v2.getTime();
  45. } else {
  46. n2 = Number(v2);
  47. }
  48. return n1 - n2;
  49. };
  50. SimileAjax.NativeDateUnit.earlier = function(v1, v2) {
  51. return SimileAjax.NativeDateUnit.compare(v1, v2) < 0 ? v1 : v2;
  52. };
  53. SimileAjax.NativeDateUnit.later = function(v1, v2) {
  54. return SimileAjax.NativeDateUnit.compare(v1, v2) > 0 ? v1 : v2;
  55. };
  56. SimileAjax.NativeDateUnit.change = function(v, n) {
  57. return new Date(v.getTime() + n);
  58. };