Permalink
Browse files

initial commit. bringing over from promt

  • Loading branch information...
0 parents commit 3b7972df5bf8e1ce25e06770612df58d276b8d11 @zackdever committed Sep 10, 2012
Showing with 171 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +38 −0 README.md
  3. +11 −0 package.json
  4. +2 −0 test/mocha.opts
  5. +80 −0 test/time.test.js
  6. +39 −0 time.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,38 @@
+time
+====
+Parses time input with no relation to dates,
+with the option to convert to the next immediate corresponding Date.
+
+Built for [Promt](http://promtapp.com).
+
+Parses strings such as "8:20" into a Date-less Time.
+
+ Time.parse('1') // { hours: 1, minutes: 0 }
+ Time.parse('1:23') // { hours: 1, minutes: 23 }
+ Time.parse('1.23') // { hours: 1, minutes: 23 }
+ Time.parse('123') // { hours: 1, minutes: 23 }
+
+Converts Time into the next corresponding JavaScript Date.
+
+ // assume it's 3:15 pm Aug 10
+ Time.parseToDate('3:15') // 3:15 pm Aug 10
+ Time.parseToDate('415') // 4:15 pm Aug 10
+ Time.parseToDate('2') // 2:00 am Aug 11
+
+Does simple validation.
+
+ Time.isValid('1') // true
+ Time.isValid('1.20') // true
+ Time.isValid('8:00') // true
+ Time.isValid('12.0') // false
+ Time.isValid('12:202') // false
+ Time.isValid('12:60') // false
+ Time.isValid('13:23') // false
+
+Periods (am/pm) and military time are not supported, but probably will be.
+
+Test
+----
+
+ $ npm test
+
@@ -0,0 +1,11 @@
+{
+ "name" : "time"
+ , "version" : "0.0.1"
+ , "devDependencies" : {
+ "mocha" : "1.4.1"
+ , "should" : "1.1.0"
+ }
+ , "scripts": {
+ "test": "mocha"
+ }
+}
@@ -0,0 +1,2 @@
+--require should
+--slow 20
@@ -0,0 +1,80 @@
+time = require('../time').Time;
+
+var hours=[], minutes=[], periods=[];
+
+for (var i = 1; i < 13; i++) hours.push(i.toString());
+for (var i = 0; i < 60; i++) minutes.push(('0' + i).slice(-2));
+
+describe('time', function() {
+ describe('#isValid', function() {
+ it('should pass all the hours 1-12', function() {
+ var hour, result;
+ for (var i = 0; i < hours.length; i++) {
+ hour = hours[i];
+ time.isValid(hour).should.be.ok;
+ result = time.parse(hours[i]);
+ result.hours.should.equal(parseInt(hour));
+ result.minutes.should.equal(0);
+ }
+ });
+
+ it('should fail made up hours e.g. 0, 13, 50', function() {
+ time.isValid('0').should.not.be.ok;
+ time.isValid('13').should.not.be.ok;
+ time.isValid('50').should.not.be.ok;
+ });
+
+ it('should fail made up hours e.g. 0:20, 13:12, 50:00', function() {
+ time.isValid('0:20').should.not.be.ok;
+ time.isValid('13:12').should.not.be.ok;
+ time.isValid('50:00').should.not.be.ok;
+ });
+
+ it('should pass all the hours with all the minutes 1:00 - 12:59', function() {
+ var hour, minute, result, input;
+ for (var i = 0; i < hours.length; i++) {
+ for (var j = 0; j < minutes.length; j++) {
+ hour = hours[i];
+ minute = minutes[j];
+ input = hour + ':' + minute;
+ time.isValid(input).should.be.ok;
+ result = time.parse(input);
+ result.hours.should.equal(parseInt(hour));
+ result.minutes.should.equal(parseInt(minute));
+ }
+ }
+ });
+
+ it('should pass all the times without the colon 100 - 1259', function() {
+ var hour, minute, result, input;
+ for (var i = 0; i < hours.length; i++) {
+ for (var j = 0; j < minutes.length; j++) {
+ hour = hours[i];
+ minute = minutes[j];
+ input = hour + minute;
+ time.isValid(input).should.be.ok;
+ result = time.parse(input);
+ result.hours.should.equal(parseInt(hour));
+ result.minutes.should.equal(parseInt(minute));
+ }
+ }
+ });
+
+ it('should fail made up minutes e.g. 1:3, 1:60, 1:122', function() {
+ time.isValid('1:3').should.not.be.ok;
+ time.isValid('1:60').should.not.be.ok;
+ time.isValid('1:122').should.not.be.ok;
+ time.isValid('12:99').should.not.be.ok;
+ time.isValid('12:021').should.not.be.ok;
+ time.isValid('12:218').should.not.be.ok;
+ });
+
+ it('should fail made up minutes without the colon e.g. 13, 160', function() {
+ time.isValid('13').should.not.be.ok;
+ time.isValid('160').should.not.be.ok;
+ time.isValid('1299').should.not.be.ok;
+ time.isValid('12021').should.not.be.ok;
+ time.isValid('12218').should.not.be.ok;
+ });
+ });
+});
39 time.js
@@ -0,0 +1,39 @@
+(function() {
+ var root = (typeof exports == 'undefined' ? window : exports);
+ var re = /^(10|11|12|[1-9])(?::|\.)?([0-5][0-9])?$/;
+
+ function Time() { }
+ root.Time = Time;
+
+ Time.isValid = function(time) {
+ return re.test(time);
+ };
+
+ Time.parse = function(time) {
+ time.replace(' ', '');
+ var result = re.exec(time);
+ if (!result) return null;
+
+ var minutes = result[2] ? parseInt(result[2]) : 0;
+ return { hours: parseInt(result[1]), minutes: minutes};
+ };
+
+ Time.parseToDate = function(time) {
+ var result = Time.parse(time);
+ if (result == null) return null;
+ return toDate(result.hours, result.minutes);
+ }
+
+ function toDate(hours, minutes) {
+ if (hours === 12) hours = 0; // this lets us uniformly handle am/pm adjustments
+
+ var d = new Date();
+ d.setMinutes(minutes);
+ d.setHours(hours);
+
+ // if it has already passed, add 12 hours at a time until it's in the future
+ while (new Date() > d) d.setHours(d.getHours() + 12);
+
+ return d;
+ }
+})();

0 comments on commit 3b7972d

Please sign in to comment.