Browse Source

initial commit

master
Brett Langdon 11 years ago
commit
1f518a18f9
6 changed files with 166 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +21
    -0
      LICENSE
  3. +2
    -0
      README.md
  4. +13
    -0
      example.json
  5. +103
    -0
      lib/index.js
  6. +25
    -0
      package.json

+ 2
- 0
.gitignore View File

@ -0,0 +1,2 @@
*.log
node_modules

+ 21
- 0
LICENSE View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Brett Langdon <brett@blangdon.com> (http://brett.is)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 2
- 0
README.md View File

@ -0,0 +1,2 @@
Calendar Events
===============

+ 13
- 0
example.json View File

@ -0,0 +1,13 @@
[
{
"name": "tech book club",
"from": "9:00",
"to": "13:00",
"every": "friday",
"start": "2014-11-28",
"end": "2015-11-28",
"data": {
"do": "something"
}
}
]

+ 103
- 0
lib/index.js View File

@ -0,0 +1,103 @@
var weekdays = [
"sunday",
"monday",
"yuesday",
"wednesday",
"thursday",
"friday",
"saturday",
];
Date.prototype.getWeekNumber = function(){
var d = new Date(+this);
d.setHours(0,0,0);
d.setDate(d.getDate()+4-(d.getDay()||7));
return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
};
Date.prototype.getUTCWeekNumber = function(){
var d = new Date(+this);
d.setHours(0,0,0);
d.setDate(d.getUTCDate()+4-(d.getUTCDay()||7));
return Math.ceil((((d-new Date(d.getUTCFullYear(),0,1))/8.64e7)+1)/7);
};
Date.prototype.getDayName = function(){
return weekdays[this.getDay()];
};
Date.prototype.getUTCDayName = function(){
return weekdays[this.getUTCDay()];
};
Date.prototype.getWeekOfMonth = function(){
var month = this.getMonth();
var year = this.getFullYear();
var firstWeekday = new Date(year, month, 1).getDay();
var lastDateOfMonth = new Date(year, month + 1, 0).getDate();
var offsetDate = this.getDate() + firstWeekday - 1;
var index = 1;
var weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7);
var week = index + Math.floor(offsetDate / 7);
if(week < 2 + index){
return week;
}
return (week === weeksInMonth)? index + 5 : week;
};
Date.prototype.getUTCWeekOfMonth = function(){
var month = this.getUTCMonth();
var year = this.getUTCFullYear();
var firstWeekday = new Date(year, month, 1).getUTCDay();
var lastDateOfMonth = new Date(year, month + 1, 0).getUTCDate();
var offsetDate = this.getUTCDate() + firstWeekday - 1;
var index = 1;
var weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7);
var week = index + Math.floor(offsetDate / 7);
if(week < 2 + index){
return week;
}
return (week === weeksInMonth)? index + 5 : week;
};
module.exports.now = function(utc){
return module.exports.parseTime(new Date(), utc);
};
module.exports.parseTime = function(time, utc){
if(!time.constructor && time.constructor.name === "Date"){
throw new Error("first argument 'time' must be a Date object");
}
var date = {
epochMs: time.getTime(),
epochSec: Math.floor(time.getTime() / 1000.0),
year: (utc)? time.getUTCFullYear() : time.getFullYear(),
month: (utc)? time.getUTCMonth() : time.getMonth(),
day: (utc)? time.getUTCDate() : time.getDate(),
hour: (utc)? time.getUTCHours() : time.getHours(),
minute: (utc)? time.getUTCMinutes() : time.getMinutes(),
second: (utc)? time.getUTCSeconds() : time.getSeconds(),
dayOfWeek: (utc)? time.getUTCDay() + 1: time.getDay() + 1,
dayOfWeekName: (utc)? time.getUTCDayName() : time.getDayName(),
weekNumber: (utc)? time.getUTCWeekNumber() : time.getWeekNumber(),
weekOfMonth: (utc)? time.getUTCWeekOfMonth() : time.getWeekOfMonth(),
};
return date;
};
module.exports.getActiveEvents = function(events, time){
var active = [];
events.forEach(function(event){
if(module.exports.isEventActive(event, time)){
active.push(event);
}
});
return active;
};
module.exports.isEventActive = function(event, time){
return true;
};

+ 25
- 0
package.json View File

@ -0,0 +1,25 @@
{
"name": "calendar-events",
"version": "0.1.0",
"description": "Calendar event parser",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/brettlangdon/node-calendar-events.git"
},
"keywords": [
"calendar",
"events",
"parser",
"scheduler"
],
"author": "Brett Langdon <brett@blangdon.com> (http://brett.is)",
"license": "MIT",
"bugs": {
"url": "https://github.com/brettlangdon/node-calendar-events/issues"
},
"homepage": "https://github.com/brettlangdon/node-calendar-events"
}

Loading…
Cancel
Save