From 1f518a18f97fd76b051050cab75c5ee4978049e0 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sun, 30 Nov 2014 10:13:00 -0500 Subject: [PATCH] initial commit --- .gitignore | 2 + LICENSE | 21 +++++++++++ README.md | 2 + example.json | 13 +++++++ lib/index.js | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++++++ 6 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 example.json create mode 100644 lib/index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3351ce2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.log +node_modules diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a221c0f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Brett Langdon (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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c9d5d48 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Calendar Events +=============== diff --git a/example.json b/example.json new file mode 100644 index 0000000..8ce2385 --- /dev/null +++ b/example.json @@ -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" + } + } +] diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..39d65c8 --- /dev/null +++ b/lib/index.js @@ -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; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..6897c92 --- /dev/null +++ b/package.json @@ -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 (http://brett.is)", + "license": "MIT", + "bugs": { + "url": "https://github.com/brettlangdon/node-calendar-events/issues" + }, + "homepage": "https://github.com/brettlangdon/node-calendar-events" +}