.factory('egCore',
['egIDL','egNet','egEnv','egOrg','egPCRUD','egEvent','egAuth',
- 'egPerm','egHatch','egPrint','egStartup','egStrings',
+ 'egPerm','egHatch','egPrint','egStartup','egStrings','egDate',
function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth ,
- egPerm , egHatch , egPrint , egStartup , egStrings) {
+ egPerm , egHatch , egPrint , egStartup , egStrings , egDate) {
return {
idl : egIDL,
hatch : egHatch,
print : egPrint,
startup : egStartup,
- strings : egStrings
+ strings : egStrings,
+ date : egDate
};
}]);
--- /dev/null
+/**
+ * Core Service - egDate
+ *
+ * Date utility functions.
+ *
+ */
+angular.module('egCoreMod')
+
+.factory('egDate', function() {
+
+ var service = {};
+
+ /**
+ * Converts an interval string to seconds.
+ *
+ * egDate.intervalToSeconds('1 min 2 seconds')) => 62
+ * egDate.intervalToSeconds('2 days')) => 172800
+ * egDate.intervalToSeconds('02:00:23')) => 7223
+ */
+ service.intervalToSeconds = function(interval) {
+ var d = new Date();
+ var start = d.getTime();
+ var parts = interval.split(' ');
+
+ for(var i = 0; i < parts.length; i += 2) {
+
+ if (!parts[i+1]) {
+ // interval is a bare hour:min:sec string
+ var times = parts[i].split(':');
+ d.setHours(d.getHours() + Number(times[0]));
+ d.setMinutes(d.getMinutes() + Number(times[1]));
+ d.setSeconds(d.getSeconds() + Number(times[2]));
+ continue;
+ }
+
+ var count = Number(parts[i]);
+ var type = parts[i+1].replace(/s?,?$/,'');
+
+ if (type.match(/^s/)) {
+ d.setSeconds(d.getSeconds() + count);
+ } else if (type.match(/^min/)) {
+ d.setMinutes(d.getMinutes() + count);
+ } else if (type.match(/^h/)) {
+ d.setHours(d.getHours() + count);
+ } else if (type.match(/^d/)) {
+ d.setDate(d.getDate() + count);
+ } else if (type.match(/^mon/)) {
+ d.setMonth(d.getMonth() + count);
+ } else if (type.match(/^y/)) {
+ d.setFullYear(d.getFullYear() + count);
+ }
+ }
+
+ return Number((d.getTime() - start) / 1000);
+ }
+
+ return service;
+})
+
+
+
--- /dev/null
+'use strict';
+
+describe('egDate', function(){
+ beforeEach(module('egCoreMod'));
+
+ it('should parse a simple interval', inject(function(egDate) {
+ expect(egDate.intervalToSeconds('2 days')).toBe(172800);
+ }));
+
+ it('should parse a combined interval', inject(function(egDate) {
+ expect(egDate.intervalToSeconds('1 min 2 seconds')).toBe(62);
+ }));
+
+ it('should parse a time interval', inject(function(egDate) {
+ expect(egDate.intervalToSeconds('02:00:23')).toBe(7223);
+ }));
+
+});