├── .bundle └── install.log ├── .circleci └── config.yml ├── .gitignore ├── .rvmrc ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── lib ├── modules │ ├── configuration.rb │ ├── constant.rb │ ├── day.rb │ ├── month.rb │ ├── version.rb │ ├── week.rb │ └── year.rb ├── test │ └── modules │ │ ├── date │ │ ├── test_constant.rb │ │ ├── test_day.rb │ │ ├── test_month.rb │ │ ├── test_week.rb │ │ └── test_year.rb │ │ └── time │ │ ├── test_constant.rb │ │ ├── test_day.rb │ │ ├── test_month.rb │ │ ├── test_week.rb │ │ └── test_year.rb └── week_of_month.rb ├── license └── week_of_month.gemspec /.bundle/install.log: -------------------------------------------------------------------------------- 1 | # Logfile created on 2014-04-28 22:41:29 +0530 by logger.rb/36483 2 | I, [2014-04-28T22:41:29.237110 #16389] INFO -- : 0: rake (10.1.0) from /home/sachin/.rvm/gems/ruby-2.0.0-p247@global/specifications/rake-10.1.0.gemspec 3 | I, [2014-04-28T22:41:29.266843 #16389] INFO -- : 0: week_of_month (1.2.3.1) from /home/sachin/projects/week-of-month/week_of_month.gemspec 4 | I, [2014-04-28T22:41:29.267622 #16389] INFO -- : 0: bundler (1.5.0) from /home/sachin/.rvm/gems/ruby-2.0.0-p247@todo/specifications/bundler-1.5.0.gemspec 5 | I, [2014-04-28T23:06:54.580647 #16882] INFO -- : 0: rake (10.1.0) from /home/sachin/.rvm/gems/ruby-2.0.0-p247@global/specifications/rake-10.1.0.gemspec 6 | I, [2014-04-28T23:06:54.581035 #16882] INFO -- : 0: bundler (1.5.0) from /home/sachin/.rvm/gems/ruby-2.0.0-p247@todo/specifications/bundler-1.5.0.gemspec 7 | I, [2014-04-28T23:06:54.596977 #16882] INFO -- : 0: week_of_month (1.2.3.1) from /home/sachin/projects/week-of-month/week_of_month.gemspec 8 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Ruby CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-ruby/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/ruby:2.4.1-node-browsers 11 | 12 | working_directory: ~/week-of-month 13 | 14 | steps: 15 | - checkout 16 | 17 | - run: 18 | # install gems 19 | name: install dependencies 20 | command: | 21 | bundle install --jobs=4 --retry=3 --path vendor/bundle 22 | 23 | # run tests! 24 | - run: 25 | name: run tests 26 | command: | 27 | bundle exec rake test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Netbeans 2 | nbproject/ 3 | 4 | ## Aptans 5 | .project 6 | 7 | ## EMACS 8 | *~ 9 | \#* 10 | .\#* 11 | 12 | ## VIM 13 | *.swp 14 | 15 | # IDEA / RUBYMINE 16 | .idea/ 17 | 18 | ## PROJECT::GENERAL 19 | tags 20 | coverage 21 | rdoc 22 | doc 23 | .yardoc 24 | pkg 25 | -------------------------------------------------------------------------------- /.rvmrc: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # This is an RVM Project .rvmrc file, used to automatically load the ruby 4 | # development environment upon cd'ing into the directory 5 | 6 | # First we specify our desired [@], the @gemset name is optional, 7 | # Only full ruby name is supported here, for short names use: 8 | # echo "rvm use 2.4.1@wom" > .rvmrc 9 | environment_id="ruby-2.4.1@wom" 10 | 11 | # Uncomment the following lines if you want to verify rvm version per project 12 | # rvmrc_rvm_version="1.27.0 (latest)" # 1.10.1 seems like a safe start 13 | # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || { 14 | # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading." 15 | # return 1 16 | # } 17 | 18 | # First we attempt to load the desired environment directly from the environment 19 | # file. This is very fast and efficient compared to running through the entire 20 | # CLI and selector. If you want feedback on which environment was used then 21 | # insert the word 'use' after --create as this triggers verbose mode. 22 | if [[ -d "${rvm_path:-$HOME/.rvm}/environments" 23 | && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] 24 | then 25 | \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id" 26 | for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"* 27 | do 28 | if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]] 29 | then \. "${__hook}" || true 30 | fi 31 | done 32 | unset __hook 33 | if (( ${rvm_use_flag:=1} >= 1 )) # display automatically 34 | then 35 | if [[ $- == *i* ]] # check for interactive shells 36 | then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)\n" # show the user the ruby and gemset they are using in green 37 | else printf "%b" "Using: $GEM_HOME\n" # don't use colors in non-interactive shells 38 | fi 39 | fi 40 | else 41 | # If the environment file has not yet been created, use the RVM CLI to select. 42 | rvm --create use "$environment_id" || { 43 | echo "Failed to create RVM environment '${environment_id}'." 44 | return 1 45 | } 46 | fi 47 | 48 | # If you use bundler, this might be useful to you: 49 | # if [[ -s Gemfile ]] && { 50 | # ! builtin command -v bundle >/dev/null || 51 | # builtin command -v bundle | GREP_OPTIONS="" \command \grep $rvm_path/bin/bundle >/dev/null 52 | # } 53 | # then 54 | # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n" 55 | # gem install bundler 56 | # fi 57 | # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null 58 | # then 59 | # bundle install | GREP_OPTIONS="" \command \grep -vE '^Using|Your bundle is complete' 60 | # fi 61 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | group :development do 6 | # Debugging 7 | gem 'byebug' 8 | 9 | # Documentation 10 | gem 'redcarpet' # Markdown implementation (for yard) 11 | gem 'rubocop', require: false 12 | gem 'yard' # Documentation generator 13 | end 14 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | week_of_month (1.2.5) 5 | 6 | GEM 7 | remote: https://rubygems.org/ 8 | specs: 9 | ast (2.3.0) 10 | byebug (4.0.5) 11 | columnize (= 0.9.0) 12 | columnize (0.9.0) 13 | parallel (1.11.2) 14 | parser (2.4.0.0) 15 | ast (~> 2.2) 16 | power_assert (1.1.0) 17 | powerpack (0.1.1) 18 | rainbow (2.2.2) 19 | rake 20 | rake (13.0.1) 21 | redcarpet (3.5.1) 22 | rubocop (0.49.1) 23 | parallel (~> 1.10) 24 | parser (>= 2.3.3.1, < 3.0) 25 | powerpack (~> 0.1) 26 | rainbow (>= 1.99.1, < 3.0) 27 | ruby-progressbar (~> 1.7) 28 | unicode-display_width (~> 1.0, >= 1.0.1) 29 | ruby-progressbar (1.8.1) 30 | test-unit (3.2.5) 31 | power_assert 32 | unicode-display_width (1.2.1) 33 | yard (0.9.20) 34 | 35 | PLATFORMS 36 | ruby 37 | 38 | DEPENDENCIES 39 | bundler (>= 1.15.4) 40 | byebug 41 | rake (>= 12.0.0) 42 | redcarpet 43 | rubocop 44 | test-unit (>= 3.2.5) 45 | week_of_month! 46 | yard 47 | 48 | BUNDLED WITH 49 | 1.15.4 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Week of month 2 | 3 | [![Gem Version](https://badge.fury.io/rb/week_of_month.svg)][gem] 4 | [![Climate](https://codeclimate.com/github/sachin87/week-of-month.png)][climate] 5 | [![License](http://img.shields.io/license/MIT.png?color=green)][license] 6 | [![CircleCI](https://circleci.com/gh/sachin87/week-of-month.svg?style=svg)](https://circleci.com/gh/sachin87/week-of-month) 7 | 8 | [gem]: http://badge.fury.io/rb/week_of_month 9 | [climate]: https://codeclimate.com/github/sachin87/week-of-month 10 | [license]: http://opensource.org/licenses/MIT 11 | 12 | Week of month is a gem which extends Date and Time class with useful methods. Among accurately displaying the days of the week in the form of a calendar arrangement, this gem will also provide you with tools for identifying which week of the month a particular date lies. 13 | 14 | ## Getting Started 15 | 16 | Week of month is released as a Ruby Gem. The gem is to be installed within a Ruby 17 | on Rails application. To install, simply add the following to your Gemfile: 18 | 19 | ```ruby 20 | gem 'week_of_month' 21 | ``` 22 | 23 | Run bundle install and don't forget to restart your server after it. 24 | 25 | You can also install this gem from the command line as: 26 | 27 | ```ruby 28 | gem install 'week_of_month' 29 | ``` 30 | ## New! Configure week start day! 31 | 32 | include this line inside your application.rb: 33 | 34 | ```ruby 35 | WeekOfMonth.configuration.monday_active = true 36 | ``` 37 | This will do all manipulations considering Monday as the first day of the week. 38 | ## Usage 39 | 40 | **Return the days of the month as if they were displayed on a calendar. In this example, the first day of January starts on a Sunday. Note the format is always (year, month, day)** 41 | 42 | ```ruby 43 | Date.new(2012,1,1).week_split 44 | 45 | =begin 46 | [[1, 2, 3, 4, 5, 6, 7], 47 | [8, 9, 10, 11, 12, 13, 14], 48 | [15, 16, 17, 18, 19, 20, 21], 49 | [22, 23, 24, 25, 26, 27, 28], 50 | [29, 30, 31]] 51 | =end 52 | ``` 53 | **Return the total number of weeks in a month.** 54 | 55 | ```ruby 56 | Date.new(2012,1,31).total_weeks 57 | # => 5 58 | ``` 59 | 60 | **Return what number week in the month a specific date lies. Can also return the number in english.** 61 | 62 | ```ruby 63 | Date.new(2012,1,31).week_of_month 64 | # => 5 65 | 66 | Date.new(2012,1,31).week_of_month_in_eng 67 | # => "Fifth" 68 | ``` 69 | 70 | **Return true if date lies in the first week of a month, otherwise false will be returned. Also works with second week and last week.** 71 | 72 | ```ruby 73 | Date.new(2012,1,1).first_week? 74 | # => true 75 | 76 | Date.new(2012,1,9).second_week? 77 | # => true 78 | 79 | Date.new(2012,1,31).last_week? 80 | # => true 81 | ``` 82 | 83 | **Returns the month for the specified date.** 84 | 85 | ```ruby 86 | Date.new(2012,1,1).name_of_month 87 | # => "January" 88 | ``` 89 | 90 | **Return true if date lies in the month of which the method is called, otherwise false will be returned (works for all months).** 91 | 92 | ```ruby 93 | Date.new(2012,1,1).january? 94 | # => true 95 | 96 | Date.new(2012,1,9).august? 97 | # => false 98 | 99 | Date.new(2012,12,25).december? 100 | # => true 101 | ``` 102 | 103 | **Return the number of days in a given month (regardless of selected day).** 104 | 105 | ```ruby 106 | Date.new(2012,1,1).last_day_of_month 107 | # => 31 108 | 109 | Date.new(2012,2,9).last_day_of_month 110 | # => 29 111 | ``` 112 | 113 | **Return the dates for which the day of the method falls (works for all days).** 114 | 115 | ```ruby 116 | Date.new(2012,1,1).all_mondays_in_month 117 | # => [2, 9, 16, 23, 30] 118 | ``` 119 | 120 | **Return the day of the week for the specified date.** 121 | 122 | ```ruby 123 | Date.new(2012,1,1).name_of_week_day 124 | # => "Sunday" 125 | ``` 126 | 127 | **Returns true if date falls on Monday through Friday, else returns false.** 128 | 129 | ```ruby 130 | Date.new(2012,1,1).working_day? 131 | # => false 132 | 133 | Date.new(2012,2,3).working_day? 134 | # => true 135 | ``` 136 | 137 | **Returns true if date falls on Saturday or Sunday, else returns false.** 138 | 139 | ```ruby 140 | Date.new(2012,1,1).week_end? 141 | # => true 142 | 143 | Date.new(2012,2,3).week_end? 144 | # => false 145 | ``` 146 | 147 | 148 | 149 | 150 | 151 | ## Tools Being Used 152 | 153 | We believe strongly in not writing code unless we have to, so Week of month is built using: 154 | 155 | * Ruby Date Class 156 | 157 | * Ruby Time Class 158 | 159 | ## Version History 160 | 161 | ###1.2.5 162 | 163 | Methods beginning_of_week and end_of_week renamed to 164 | starting_of_week and ending_of_week. 165 | 166 | ###1.2.1 167 | 168 | Support for Time class 169 | 170 | **Methods Added:** 171 | 172 | name_of_week_day, name_of_month, week_end?, working_day?, 173 | all_sundays_in_month, all_mondays_in_month, all_tuesdays_in_month, 174 | all_wednesdays_in_month, all_thursdays_in_month, all_fridays_in_month, 175 | all_saturdays_in_month 176 | 177 | ###1.1.0 178 | 179 | ActiveSupport Dependency removed 180 | 181 | **Methods Added:** 182 | 183 | january?, february?, march?, april?, may?, june?, july?, 184 | august?, september?, october?, november?, december?, last_day_of_month 185 | 186 | ## Contributing to Week of month 187 | 188 | Fork, fix, then send me a pull request, 189 | and most importantly add yourself to a list of authors in a gemspec file. 190 | 191 | ## License 192 | 193 | MIT 194 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # @author Sachin Singh 2 | 3 | require 'rake/testtask' 4 | require 'bundler/gem_tasks' 5 | 6 | Rake::TestTask.new do |t| 7 | t.libs << 'test' 8 | t.test_files = FileList['lib/test/modules/date/test*.rb', 'lib/test/modules/time/test*.rb'] 9 | t.verbose = true 10 | end 11 | 12 | desc 'Run tests' 13 | task default: :test 14 | -------------------------------------------------------------------------------- /lib/modules/configuration.rb: -------------------------------------------------------------------------------- 1 | module WeekOfMonth 2 | class Configuration 3 | attr_accessor \ 4 | :monday_active 5 | 6 | def initialize 7 | @monday_active = false 8 | end 9 | 10 | def monday_active? 11 | @monday_active 12 | end 13 | end 14 | 15 | def self.configuration 16 | @configuration ||= Configuration.new 17 | end 18 | 19 | def self.configuration=(config) 20 | @configuration = config 21 | end 22 | 23 | def self.configure 24 | yield configuration 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/modules/constant.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | # @author Sachin Singh 5 | 6 | module WeekOfMonth 7 | module Constant 8 | # hash containing english words from one to seven 9 | WEEK_OF_MONTH_IN_ENG = { 1 => 'First', 2 => 'Second', 3 => 'Third', 10 | 4 => 'Fourth', 5 => 'Fifth', 6 => 'Sixth', 7 => 'Seventh' }.freeze 11 | 12 | # hash containing french words from one to seven 13 | WEEK_OF_MONTH_IN_FR = { 1 => 'Premier', 2 => 'Deuxième', 3 => 'Troisième', 14 | 4 => 'Quatrième', 5 => 'Cinquième', 6 => 'Sixième', 7 => 'Septième' }.freeze 15 | 16 | # hash containing german words from one to seven 17 | WEEK_OF_MONTH_IN_GER = { 1 => 'First', 2 => 'Second', 3 => 'Dritten', 18 | 4 => 'Vierte', 5 => 'Fünfte', 6 => 'Sechste', 7 => 'siebte' }.freeze 19 | 20 | # hash containing japanese words from one to seven 21 | WEEK_OF_MONTH_IN_JA = { 1 => '第一', 2 => '第二', 3 => '第三', 22 | 4 => '第四', 5 => '第五', 6 => '第六', 7 => '第七' }.freeze 23 | 24 | # hash containing month name with days in that month(in non leap year) 25 | MONTH_WITH_DAY = { january: 31, february: 28, march: 31, 26 | april: 30, may: 31, june: 30, july: 31, 27 | august: 31, september: 30, october: 31, 28 | november: 30, december: 31 }.freeze 29 | 30 | # hash containing month names with their sequence 31 | MONTH_WITH_SEQUENCE = { january: 1, february: 2, march: 3, 32 | april: 4, may: 5, june: 6, july: 7, 33 | august: 8, september: 9, october: 10, 34 | november: 11, december: 12 }.freeze 35 | 36 | # array of ordered days names starting from sunday and ending with saturday. 37 | DAYS_IN_SEQUENCE = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].freeze 38 | 39 | # array of ordered days names starting from sunday and ending with saturday. 40 | DAYS_IN_SEQUENCE_STARTING_MONDAY = %w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday].freeze 41 | 42 | # array of ordered days names starting from sunday and ending with saturday. 43 | WEEKS_IN_SEQUENCE = %w[Last First Second Third Fourth Fifth].freeze 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/modules/day.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | # @author Sachin Singh 5 | 6 | module WeekOfMonth 7 | attr_accessor :config 8 | 9 | def initialize(config = Configuration.new) 10 | @config = config 11 | end 12 | 13 | module Day 14 | # gives array of days in month 15 | # Date.new(2012,1,1).days_array 16 | # => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 17 | # 10, 11, 12, 13, 14, 15, 16, 18 | # 17, 18, 19, 20, 21, 22, 23, 19 | # 24, 25, 26, 27, 28, 29, 30, 20 | # 31] 21 | # @return [Array] 22 | def days_array 23 | day = beginning_of_month.to_date.wday 24 | if WeekOfMonth.configuration.monday_active 25 | day = day.zero? ? 6 : day - 1 26 | end 27 | array = [] 28 | array[day] = 1 29 | (2..ending_of_month.mday).each { |i| array << i } 30 | array 31 | end 32 | 33 | # Date.new(2012,11,1).name_of_week_day 34 | # => 'Thursday' 35 | # @return [String] 36 | def name_of_week_day 37 | self.class.new(year, month, day).strftime('%A') 38 | end 39 | 40 | # this code generates methods with names like 'upcoming_monday' and 'previous_monday' 41 | # Date.new(2013,1,1).upcoming_monday 42 | # => # 43 | # Date.new(2013,1,1).previous_monday 44 | # => # 45 | { 'upcoming' => '+', 'previous' => '-' }.each_pair do |key, value| 46 | Date::DAYNAMES.each do |day_name| 47 | name = "#{key}_#{day_name.downcase}".to_sym 48 | check = "#{day_name.downcase}?".to_sym 49 | define_method(name) do 50 | date = eval 'self' 51 | if date.send(check) 52 | if date.class == Date 53 | date = date.send(value, 7) 54 | elsif date.class == Time 55 | date = date.send(value, (60 * 60 * 24 * 7)) 56 | end 57 | else 58 | until date.send(check) 59 | if date.class == Date 60 | date = date.send(value, 1) 61 | elsif date.class == Time 62 | date = date.send(value, (60 * 60 * 24)) 63 | end 64 | end 65 | end 66 | date 67 | end 68 | end 69 | end 70 | 71 | # gives last working/business day of the month 72 | # Date.new(2014,12,1).last_business_day_of_month 73 | # => # 74 | def last_business_day_of_month 75 | all_working_days_of_month.first 76 | end 77 | 78 | # returns array of all working days of the month 79 | # Date.new(2014,12,1).all_working_days_of_month 80 | # => [#, #, 81 | # #, #, 82 | # #, #, 83 | # #, #, 84 | # #, #, 85 | # #, #, 86 | # #, #, 87 | # #, #, 88 | # #, #, 89 | # #, #, 90 | # #, #, 91 | # #] 92 | def all_working_days_of_month 93 | ending_of_month.downto(beginning_of_month).select(&:working_day?) 94 | end 95 | 96 | # returns array of all non working days of the month 97 | # Date.new(2014,12,1).all_non_week_days_of_month 98 | # => [#, #, 99 | # #, #, 100 | # #, #, 101 | # #, #] 102 | def all_non_week_days_of_month 103 | ending_of_month.downto(beginning_of_month).select(&:week_end?) 104 | end 105 | 106 | # returns first working/business day of the month 107 | # Date.new(2014,12,1).first_working_day_of_the_month 108 | # => # 109 | def first_working_day_of_the_month 110 | beginning_of_month.upto(ending_of_month).find(&:working_day?) 111 | end 112 | end 113 | end 114 | -------------------------------------------------------------------------------- /lib/modules/month.rb: -------------------------------------------------------------------------------- 1 | # @author Sachion Singh 2 | 3 | RUBY_VERSION < '1.9' ? require('modules/constant') : require_relative('constant') 4 | 5 | module WeekOfMonth 6 | module Month 7 | include WeekOfMonth::Constant 8 | 9 | # this code generates method named like january?..december? 10 | # to check whether a month is january or march? etc. 11 | # @return [Boolean] 12 | MONTH_WITH_DAY.keys.each do |month_name| 13 | define_method((month_name.to_s + '?').to_sym) do 14 | MONTH_WITH_SEQUENCE[month_name] == month 15 | end 16 | end 17 | 18 | # returns day of last day of month for a given date. 19 | # Date.new(2012,11,1).last_day_of_month 20 | # => 30 21 | # @return [Fixnum] 22 | def last_day_of_month 23 | if leap? && february? 24 | 29 25 | else 26 | MONTH_WITH_DAY[MONTH_WITH_SEQUENCE.key(month)] 27 | end 28 | end 29 | 30 | # returns date of last day of month for a given date. 31 | # Date.new(2012,11,1).ending_of_month 32 | # => # 33 | # @return [Date] 34 | def ending_of_month 35 | self.class.new(year, month, last_day_of_month) 36 | end 37 | 38 | # returns date of first day of month for a given date. 39 | # Date.new(2012,11,1).beginning_of_month 40 | # => # 41 | # @return [Date] 42 | def beginning_of_month 43 | self.class.new(year, month, 1) 44 | end 45 | 46 | # returns name of month for a given date. 47 | # Date.new(2012,11,1).name_of_month 48 | # => "November" 49 | # @return [String] 50 | def name_of_month 51 | self.class.new(year, month, day).strftime('%B') 52 | end 53 | 54 | # this code generates method named like 'all_mondays_in_month', 55 | # 'all_tuesdays_in_month' etc. for all seven days of week 56 | # Date.new(2012,11,1).all_mondays_in_month 57 | # => [5, 12, 19, 26] 58 | # @return [Array] 59 | DAYS_IN_SEQUENCE.each_with_index do |day_name, i| 60 | method_name = "all_#{day_name.downcase}s_in_month".to_sym 61 | define_method(method_name) do 62 | if WeekOfMonth.configuration.monday_active 63 | week_split.map { |d| d[i-1] }.compact 64 | else 65 | week_split.map { |d| d[i] }.compact 66 | end 67 | end 68 | end 69 | 70 | # this code generates method named like 'first_monday_in_month', 71 | # 'second_tuesday_in_month', 'last_friday_in_month' etc. for 72 | # first, second, third, fourth and last seven days of week 73 | # Date.new(2014,11).third_saturday_in_month 74 | # => # 75 | # @return [Date] 76 | DAYS_IN_SEQUENCE.each_with_index do |day_name, i| 77 | WEEKS_IN_SEQUENCE.each.with_index(-1) do |week_number, j| 78 | method_name = "#{week_number.downcase}_#{day_name.downcase}_in_month".to_sym 79 | define_method(method_name) do 80 | if WeekOfMonth.configuration.monday_active 81 | self.class.new(year, month, week_split.map { |d| d[i-1] }.compact[j]) 82 | else 83 | self.class.new(year, month, week_split.map { |d| d[i] }.compact[j]) 84 | end 85 | end 86 | end 87 | end 88 | end 89 | end 90 | -------------------------------------------------------------------------------- /lib/modules/version.rb: -------------------------------------------------------------------------------- 1 | # @author Sachin Singh 2 | 3 | module WeekOfMonth 4 | VERSION = '1.2.5'.freeze 5 | end 6 | -------------------------------------------------------------------------------- /lib/modules/week.rb: -------------------------------------------------------------------------------- 1 | # @author Sachion Singh 2 | 3 | RUBY_VERSION < '1.9' ? require('modules/constant') : require_relative('constant') 4 | 5 | module WeekOfMonth 6 | module Week 7 | include WeekOfMonth::Constant 8 | 9 | # returns week of month for a given date 10 | # Date.new(2012,11,15).week_of_month 11 | # => 3 12 | # @return [Fixnum] 13 | def week_of_month 14 | week_split.each_with_index do |o, i| 15 | return (i + 1) if o.include?(day) 16 | end 17 | end 18 | 19 | # November week_split would like this 20 | # [[nil, nil, nil, nil, nil, 1, 2], 21 | # [3, 4, 5, 6, 7, 8, 9], 22 | # [10, 11, 12, 13, 14, 15, 16], 23 | # [17, 18, 19, 20, 21, 22, 23], 24 | # [24, 25, 26, 27, 28, 29, 30]] 25 | # 26 | # The First Day of week decide its month of week. 27 | # The W1 Nov should be Nov 3 - Nov 9 28 | # 29 | # Date.new(2012,11,15).general_week_of_month 30 | # => 2 31 | # 32 | # Date.new(2013,11,3).general_week_of_month 33 | # => 1 34 | # @return [Fixnum] 35 | def general_week_of_month 36 | if week_split.first.first.nil? 37 | week_of_month - 1 38 | else 39 | week_of_month 40 | end 41 | end 42 | 43 | # checks whether the given day lies in first week of month 44 | # Date.new(2012,11,1).first_week? 45 | # => true 46 | # @return [Boolean] 47 | def first_week? 48 | week_split[0].include?(day) 49 | end 50 | 51 | # checks whether the given day lies in second week of month 52 | # Date.new(2012,11,8).second_week? 53 | # => true 54 | # @return [Boolean] 55 | def second_week? 56 | week_split[1].include?(day) 57 | end 58 | 59 | # checks whether the given day lies in last week of month 60 | # Date.new(2012,11,8).last_week? 61 | # => false 62 | # @return [Boolean] 63 | def last_week? 64 | week_split.last.include?(day) 65 | end 66 | 67 | # returns total number of weeks in month 68 | # Date.new(2012,11,8).total_weeks 69 | # => 5 70 | # @return [Fixnum] 71 | def total_weeks 72 | week_split.size 73 | end 74 | 75 | # checks whether the given day is saturday or sunday. 76 | # Date.new(2012,11,8).week_end? 77 | # => false 78 | # @return [Boolean] 79 | def week_end? 80 | saturday? || sunday? 81 | end 82 | 83 | # checks whether the given day is not saturday or sunday. 84 | # Date.new(2012,11,8).working_day? 85 | # => true 86 | # @return [Boolean] 87 | def working_day? 88 | !week_end? 89 | end 90 | 91 | # returns week split of the month for the given date 92 | # example- 93 | # Optional argument to change first week day to Monday 94 | # Date.new(2012,1,1).week_split -> Sunday is first week day 95 | # => [[1, 2, 3, 4, 5, 6, 7], 96 | # [8, 9, 10, 11, 12, 13, 14], 97 | # [15, 16, 17, 18, 19, 20, 21], 98 | # [22, 23, 24, 25, 26, 27, 28], 99 | # [29, 30, 31] 100 | # Date.new(2012,1,1).week_split(true) -> Monday is first week day 101 | # => [[nil,nil,nil,nil,nil,nil,1], 102 | # [2, 3, 4, 5, 6, 7, 8], 103 | # [9, 10, 11, 12, 13, 14, 15], 104 | # [16, 17, 18, 19, 20, 21, 22], 105 | # [23, 24, 25, 26, 27, 28, 29], 106 | # [30, 31] 107 | # @return [Array] 108 | def week_split 109 | days_array.each_slice(7).to_a 110 | end 111 | 112 | # this code generates method like 'week_of_month_eng', 'week_of_month_fr' etc. 113 | # Date.new(2012,11,15).week_of_month_in_eng 114 | # => 'Third' 115 | # Date.new(2012,11,30).week_of_month_in_fr 116 | # => "Cinquième" 117 | # @return [String] 118 | constants.select { |x| x.to_s.match('WEEK_OF_MONTH_IN_') }.each do |const| 119 | define_method(const.to_s.downcase) do 120 | eval "#{const}[week_of_month]" 121 | end 122 | end 123 | 124 | # it returns days past in the week 125 | # Date.new(2012,11,15).days_past_in_week 126 | # => 4 127 | # Time.new(2012,11,30).days_past_in_week 128 | # => 5 129 | # @return [Fixnum] 130 | def days_past_in_week 131 | to_date.cwday 132 | end 133 | 134 | # it returns days left in the week 135 | # Date.new(2012,11,15).days_left_in_week 136 | # => 3 137 | # Time.new(2012,11,30).days_left_in_week 138 | # => 2 139 | # @return [Fixnum] 140 | def days_left_in_week 141 | 7 - days_past_in_week 142 | end 143 | 144 | # it returns date of the first day(sunday) of the week 145 | # Date.new(2012,11,15).starting_of_week 146 | # => # 147 | # Time.new(2012,11,30).starting_of_week 148 | # => 2012-11-29 23:59:55 +0530 149 | # @return [Date || Time] 150 | def starting_of_week 151 | self.class.new(year, month, current_week.detect { |i| !i.nil? }) 152 | end 153 | 154 | # it returns date of the last day(saturday) of the week 155 | # Date.new(2012,11,15).ending_of_week 156 | # => # 157 | # Time.new(2012,11,30).ending_of_week 158 | # => 2012-11-30 00:00:02 +0530 159 | # @return [Date || Time] 160 | def ending_of_week 161 | if current_week.index(day) == 6 162 | self.class.new(year, month, current_week.last) 163 | elsif current_week.index(day) < 6 164 | if self.class == Date 165 | self + (6 - current_week.index(day)) 166 | elsif self.class == Time 167 | self + (60 * 60 * 24 * (6 - current_week.index(day))) 168 | end 169 | end 170 | end 171 | 172 | # it returns date of the next week day. 173 | # Date.new(2012,11,15).next_week 174 | # => # 175 | # Time.new(2012,11,30).next_week 176 | # => 2012-11-30 00:00:07 +0530 177 | # @return [Date || Time] 178 | def next_week 179 | if self.class == Date 180 | self + 7 181 | elsif self.class == Time 182 | self + (60 * 60 * 24 * 7) 183 | end 184 | end 185 | 186 | # it returns date of the previous week day. 187 | # Date.new(2012,11,15).previous_week 188 | # => # 189 | # Time.new(2012,11,30).previous_week 190 | # => 2012-11-29 23:59:53 +0530 191 | # @return [Date || Time] 192 | def previous_week 193 | if self.class == Date 194 | self - 7 195 | elsif self.class == Time 196 | self - (60 * 60 * 24 * 7) 197 | end 198 | end 199 | 200 | # it returns array of days in current week. 201 | # Date.new(2013,1,13).current_week 202 | # => [7, 8, 9, 10, 11, 12, 13] 203 | # @return [Array] 204 | def current_week 205 | week_split.select { |c| c.include?(day) }.flatten 206 | end 207 | end 208 | end 209 | -------------------------------------------------------------------------------- /lib/modules/year.rb: -------------------------------------------------------------------------------- 1 | # @author Sachin Singh 2 | 3 | module WeekOfMonth 4 | module Year 5 | def self.included(klass) 6 | klass.extend(ClassMethods) 7 | end 8 | 9 | module ClassMethods 10 | # @param[Date,Date] 11 | # Date.years_between_dates(Date.new(2015,11,1),Date.new(2012,11,15)) 12 | # => 3 13 | # @param[Time,Time] 14 | # Time.years_between_dates(Time.new(2015,11,1),Time.new(2012,11,15)) 15 | # => 3 16 | # @return [Fixnum] 17 | def years_between_dates(date1, date2) 18 | (date1.year - date2.year).abs 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/test/modules/date/test_constant.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 6 | 7 | class TestConstantForDate < Test::Unit::TestCase 8 | def test_constants_present? 9 | assert Date::WEEK_OF_MONTH_IN_ENG 10 | 11 | assert Date::WEEK_OF_MONTH_IN_GER 12 | 13 | assert Date::WEEK_OF_MONTH_IN_FR 14 | 15 | assert Date::WEEK_OF_MONTH_IN_JA 16 | 17 | assert Date::MONTH_WITH_DAY 18 | 19 | assert Date::MONTH_WITH_SEQUENCE 20 | 21 | assert Date::DAYS_IN_SEQUENCE 22 | end 23 | 24 | def test_constants_value 25 | assert_equal({ 1 => 'First', 2 => 'Second', 26 | 3 => 'Third', 4 => 'Fourth', 27 | 5 => 'Fifth', 6 => 'Sixth', 28 | 7 => 'Seventh' }, Date::WEEK_OF_MONTH_IN_ENG) 29 | 30 | assert_equal({ 1 => 'Premier', 2 => 'Deuxième', 31 | 3 => 'Troisième', 4 => 'Quatrième', 32 | 5 => 'Cinquième', 6 => 'Sixième', 33 | 7 => 'Septième' }, Date::WEEK_OF_MONTH_IN_FR) 34 | 35 | assert_equal({ 1 => 'First', 2 => 'Second', 36 | 3 => 'Dritten', 4 => 'Vierte', 37 | 5 => 'Fünfte', 6 => 'Sechste', 38 | 7 => 'siebte' }, Date::WEEK_OF_MONTH_IN_GER) 39 | 40 | assert_equal({ 1 => '第一', 2 => '第二', 41 | 3 => '第三', 4 => '第四', 42 | 5 => '第五', 6 => '第六', 43 | 7 => '第七' }, Date::WEEK_OF_MONTH_IN_JA) 44 | 45 | assert_equal({ january: 1, february: 2, march: 3, 46 | april: 4, may: 5, june: 6, july: 7, 47 | august: 8, september: 9, october: 10, 48 | november: 11, december: 12 }, Date::MONTH_WITH_SEQUENCE) 49 | 50 | assert_equal({ january: 31, february: 28, march: 31, 51 | april: 30, may: 31, june: 30, july: 31, 52 | august: 31, september: 30, october: 31, 53 | november: 30, december: 31 }, Date::MONTH_WITH_DAY) 54 | 55 | assert_equal(%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday], 56 | Date::DAYS_IN_SEQUENCE) 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/test/modules/date/test_day.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | 6 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 7 | class TestDayForDate < Test::Unit::TestCase 8 | def test_days_array 9 | object = Date.new(2012, 2, 8) 10 | 11 | days_array_for_february = [nil, nil, nil, 1, 2, 3, 4, 5, 12 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 13 | 15, 16, 17, 18, 19, 20, 21, 22, 14 | 23, 24, 25, 26, 27, 28, 29] 15 | assert_kind_of Array, object.days_array 16 | assert_equal days_array_for_february, object.days_array 17 | 18 | object = Date.new(2012, 7, 1) 19 | days_array_for_july = [1, 2, 3, 4, 5, 6, 7, 20 | 8, 9, 10, 11, 12, 13, 21 | 14, 15, 16, 17, 18, 19, 22 | 20, 21, 22, 23, 24, 25, 23 | 26, 27, 28, 29, 30, 31] 24 | assert_kind_of Array, object.days_array 25 | assert_equal days_array_for_july, object.days_array 26 | end 27 | 28 | def test_days_array_monday 29 | WeekOfMonth.configuration.monday_active = true 30 | object = Date.new(2014, 11, 3) 31 | 32 | days_array_for_november = [nil, nil, nil, nil, nil, 1, 2, 33 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 34 | 12, 13, 14, 15, 16, 17, 18, 35 | 19, 20, 21, 22, 23, 24, 25, 26, 36 | 27, 28, 29, 30] 37 | assert_kind_of Array, object.days_array 38 | assert_equal days_array_for_november, object.days_array 39 | 40 | object = Date.new(2014, 12, 1) 41 | days_array_for_december = [1, 2, 3, 4, 5, 6, 7, 42 | 8, 9, 10, 11, 12, 13, 43 | 14, 15, 16, 17, 18, 19, 44 | 20, 21, 22, 23, 24, 25, 45 | 26, 27, 28, 29, 30, 31] 46 | assert_kind_of Array, object.days_array 47 | assert_equal days_array_for_december, object.days_array 48 | end 49 | 50 | def test_name_of_week_day 51 | WeekOfMonth.configuration.monday_active = false 52 | assert_equal 'Saturday', Date.new(2012, 12, 1).name_of_week_day 53 | assert_equal 'Sunday', Date.new(2012, 12, 2).name_of_week_day 54 | assert_equal 'Monday', Date.new(2012, 12, 3).name_of_week_day 55 | assert_equal 'Tuesday', Date.new(2012, 12, 4).name_of_week_day 56 | assert_equal 'Wednesday', Date.new(2012, 12, 5).name_of_week_day 57 | assert_equal 'Thursday', Date.new(2012, 12, 6).name_of_week_day 58 | assert_equal 'Friday', Date.new(2012, 12, 7).name_of_week_day 59 | end 60 | 61 | def test_upcoming_sunday 62 | assert_equal Date.new(2013, 1, 6), Date.new(2013, 1, 1).upcoming_sunday 63 | assert_equal Date.new(2013, 1, 6), Date.new(2013, 1, 5).upcoming_sunday 64 | assert_equal Date.new(2013, 1, 13), Date.new(2013, 1, 7).upcoming_sunday 65 | assert_equal Date.new(2013, 1, 6), Date.new(2012, 12, 30).upcoming_sunday 66 | end 67 | 68 | def test_upcoming_monday 69 | assert_equal Date.new(2013, 1, 7), Date.new(2013, 1, 1).upcoming_monday 70 | end 71 | 72 | def test_upcoming_tuesday 73 | assert_equal Date.new(2013, 1, 8), Date.new(2013, 1, 1).upcoming_tuesday 74 | end 75 | 76 | def test_upcoming_wednesday 77 | assert_equal Date.new(2013, 1, 2), Date.new(2013, 1, 1).upcoming_wednesday 78 | end 79 | 80 | def test_upcoming_thursday 81 | assert_equal Date.new(2013, 1, 3), Date.new(2013, 1, 1).upcoming_thursday 82 | end 83 | 84 | def test_upcoming_friday 85 | assert_equal Date.new(2013, 1, 4), Date.new(2013, 1, 1).upcoming_friday 86 | end 87 | 88 | def test_upcoming_saturday 89 | assert_equal Date.new(2013, 1, 5), Date.new(2013, 1, 1).upcoming_saturday 90 | end 91 | 92 | def test_previous_saturday 93 | assert_equal Date.new(2012, 12, 29), Date.new(2013, 1, 1).previous_saturday 94 | end 95 | 96 | def test_previous_friday 97 | assert_equal Date.new(2012, 12, 28), Date.new(2013, 1, 1).previous_friday 98 | end 99 | 100 | def test_previous_thursday 101 | assert_equal Date.new(2012, 12, 27), Date.new(2013, 1, 1).previous_thursday 102 | end 103 | 104 | def test_previous_wednesday 105 | assert_equal Date.new(2012, 12, 26), Date.new(2013, 1, 1).previous_wednesday 106 | end 107 | 108 | def test_previous_tuesday 109 | assert_equal Date.new(2012, 12, 25), Date.new(2013, 1, 1).previous_tuesday 110 | end 111 | 112 | def test_previous_monday 113 | assert_equal Date.new(2012, 12, 31), Date.new(2013, 1, 1).previous_monday 114 | end 115 | 116 | def test_previous_sunday 117 | assert_equal Date.new(2012, 12, 30), Date.new(2013, 1, 1).previous_sunday 118 | assert_equal Date.new(2012, 12, 30), Date.new(2013, 1, 1).previous_sunday 119 | end 120 | 121 | def test_all_working_days_of_month 122 | assert_equal [ 123 | Date.new(2013, 4, 30), Date.new(2013, 4, 29), 124 | Date.new(2013, 4, 26), Date.new(2013, 4, 25), Date.new(2013, 4, 24), Date.new(2013, 4, 23), Date.new(2013, 4, 22), 125 | Date.new(2013, 4, 19), Date.new(2013, 4, 18), Date.new(2013, 4, 17), Date.new(2013, 4, 16), Date.new(2013, 4, 15), 126 | Date.new(2013, 4, 12), Date.new(2013, 4, 11), Date.new(2013, 4, 10), Date.new(2013, 4, 9), Date.new(2013, 4, 8), 127 | Date.new(2013, 4, 5), Date.new(2013, 4, 4), Date.new(2013, 4, 3), Date.new(2013, 4, 2), Date.new(2013, 4, 1) 128 | ], Date.new(2013, 4, 1).all_working_days_of_month 129 | end 130 | 131 | def test_all_non_week_days_of_month 132 | assert_equal [ 133 | Date.new(2013, 4, 28), Date.new(2013, 4, 27), 134 | Date.new(2013, 4, 21), Date.new(2013, 4, 20), 135 | Date.new(2013, 4, 14), Date.new(2013, 4, 13), 136 | Date.new(2013, 4, 7), Date.new(2013, 4, 6) 137 | ], Date.new(2013, 4, 1).all_non_week_days_of_month 138 | end 139 | 140 | def test_first_working_day_of_the_month 141 | assert_equal Date.new(2013, 1, 1), Date.new(2013, 1, 1).first_working_day_of_the_month 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /lib/test/modules/date/test_month.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | 6 | if RUBY_VERSION < '1.9' 7 | require('lib/week_of_month') 8 | else 9 | require_relative('../../../week_of_month') 10 | end 11 | 12 | class TestMonthForDate < Test::Unit::TestCase 13 | def test_month? 14 | assert Date.new(2012, 1, 1).january? 15 | 16 | assert Date.new(2012, 2, 1).february? 17 | 18 | assert Date.new(2012, 3, 1).march? 19 | 20 | assert Date.new(2012, 4, 1).april? 21 | 22 | assert Date.new(2012, 5, 1).may? 23 | 24 | assert Date.new(2012, 6, 1).june? 25 | 26 | assert Date.new(2012, 7, 1).july? 27 | 28 | assert Date.new(2012, 8, 1).august? 29 | 30 | assert Date.new(2012, 9, 1).september? 31 | 32 | assert Date.new(2012, 10, 1).october? 33 | 34 | assert Date.new(2012, 11, 1).november? 35 | 36 | assert Date.new(2012, 12, 1).december? 37 | end 38 | 39 | def test_last_day_of_month 40 | assert_equal 31, Date.new(2012, 1, 31).last_day_of_month 41 | 42 | assert_equal 29, Date.new(2012, 2, 29).last_day_of_month 43 | 44 | assert_equal 31, Date.new(2012, 3, 31).last_day_of_month 45 | 46 | assert_equal 30, Date.new(2012, 4, 30).last_day_of_month 47 | 48 | assert_equal 31, Date.new(2012, 5, 31).last_day_of_month 49 | 50 | assert_equal 30, Date.new(2012, 6, 30).last_day_of_month 51 | 52 | assert_equal 31, Date.new(2012, 7, 31).last_day_of_month 53 | 54 | assert_equal 31, Date.new(2012, 8, 31).last_day_of_month 55 | 56 | assert_equal 30, Date.new(2012, 9, 30).last_day_of_month 57 | 58 | assert_equal 31, Date.new(2012, 10, 31).last_day_of_month 59 | 60 | assert_equal 30, Date.new(2012, 11, 30).last_day_of_month 61 | 62 | assert_equal 31, Date.new(2012, 12, 31).last_day_of_month 63 | end 64 | 65 | def test_ending_of_month 66 | assert_equal Date.new(2012, 1, 31), Date.new(2012, 1, 1).ending_of_month 67 | 68 | assert_equal Date.new(2012, 2, 29), Date.new(2012, 2, 2).ending_of_month 69 | 70 | assert_equal Date.new(2012, 3, 31), Date.new(2012, 3, 1).ending_of_month 71 | 72 | assert_equal Date.new(2012, 4, 30), Date.new(2012, 4, 3).ending_of_month 73 | 74 | assert_equal Date.new(2012, 5, 31), Date.new(2012, 5, 1).ending_of_month 75 | 76 | assert_equal Date.new(2012, 6, 30), Date.new(2012, 6, 30).ending_of_month 77 | 78 | assert_equal Date.new(2012, 7, 31), Date.new(2012, 7, 1).ending_of_month 79 | 80 | assert_equal Date.new(2012, 8, 31), Date.new(2012, 8, 5).ending_of_month 81 | 82 | assert_equal Date.new(2012, 9, 30), Date.new(2012, 9, 2).ending_of_month 83 | 84 | assert_equal Date.new(2012, 10, 31), Date.new(2012, 10, 22).ending_of_month 85 | 86 | assert_equal Date.new(2012, 11, 30), Date.new(2012, 11, 10).ending_of_month 87 | 88 | assert_equal Date.new(2012, 12, 31), Date.new(2012, 12, 15).ending_of_month 89 | end 90 | 91 | def test_beginning_of_month 92 | assert_equal Date.new(2012, 1, 1), Date.new(2012, 1, 31).beginning_of_month 93 | 94 | assert_equal Date.new(2012, 2, 1), Date.new(2012, 2, 29).beginning_of_month 95 | 96 | assert_equal Date.new(2012, 3, 1), Date.new(2012, 3, 31).beginning_of_month 97 | 98 | assert_equal Date.new(2012, 4, 1), Date.new(2012, 4, 30).beginning_of_month 99 | 100 | assert_equal Date.new(2012, 5, 1), Date.new(2012, 5, 31).beginning_of_month 101 | 102 | assert_equal Date.new(2012, 6, 1), Date.new(2012, 6, 30).beginning_of_month 103 | 104 | assert_equal Date.new(2012, 7, 1), Date.new(2012, 7, 31).beginning_of_month 105 | 106 | assert_equal Date.new(2012, 8, 1), Date.new(2012, 8, 31).beginning_of_month 107 | 108 | assert_equal Date.new(2012, 9, 1), Date.new(2012, 9, 30).beginning_of_month 109 | 110 | assert_equal Date.new(2012, 10, 1), Date.new(2012, 10, 31).beginning_of_month 111 | 112 | assert_equal Date.new(2012, 11, 1), Date.new(2012, 11, 30).beginning_of_month 113 | 114 | assert_equal Date.new(2012, 12, 1), Date.new(2012, 12, 31).beginning_of_month 115 | end 116 | 117 | def test_all_sundays_in_month 118 | assert_equal [6, 13, 20, 27], Date.new(2013, 1, 1).all_sundays_in_month 119 | assert_equal [3, 10, 17, 24], Date.new(2013, 2, 1).all_sundays_in_month 120 | end 121 | 122 | def test_all_mondays_in_month 123 | assert_equal [7, 14, 21, 28], Date.new(2013, 1, 1).all_mondays_in_month 124 | assert_equal [4, 11, 18, 25], Date.new(2013, 2, 1).all_mondays_in_month 125 | end 126 | 127 | def test_all_tuesdays_in_month 128 | assert_equal [1, 8, 15, 22, 29], Date.new(2013, 1, 1).all_tuesdays_in_month 129 | assert_equal [5, 12, 19, 26], Date.new(2013, 2, 1).all_tuesdays_in_month 130 | end 131 | 132 | def test_all_wednesdays_in_month 133 | assert_equal [2, 9, 16, 23, 30], Date.new(2013, 1, 1).all_wednesdays_in_month 134 | assert_equal [6, 13, 20, 27], Date.new(2013, 2, 1).all_wednesdays_in_month 135 | end 136 | 137 | def test_all_thursdays_in_month 138 | assert_equal [3, 10, 17, 24, 31], Date.new(2013, 1, 1).all_thursdays_in_month 139 | assert_equal [7, 14, 21, 28], Date.new(2013, 2, 1).all_thursdays_in_month 140 | end 141 | 142 | def test_all_fridays_in_month 143 | assert_equal [4, 11, 18, 25], Date.new(2013, 1, 1).all_fridays_in_month 144 | assert_equal [1, 8, 15, 22], Date.new(2013, 2, 1).all_fridays_in_month 145 | end 146 | 147 | def test_all_saturdays_in_month 148 | assert_equal [5, 12, 19, 26], Date.new(2013, 1, 1).all_saturdays_in_month 149 | assert_equal [2, 9, 16, 23], Date.new(2013, 2, 1).all_saturdays_in_month 150 | end 151 | 152 | def test_name_of_month 153 | assert_equal 'January', Date.new(2012, 1, 1).name_of_month 154 | assert_equal 'February', Date.new(2012, 2, 1).name_of_month 155 | assert_equal 'March', Date.new(2012, 3, 1).name_of_month 156 | assert_equal 'April', Date.new(2012, 4, 1).name_of_month 157 | assert_equal 'May', Date.new(2012, 5, 1).name_of_month 158 | assert_equal 'June', Date.new(2012, 6, 1).name_of_month 159 | assert_equal 'July', Date.new(2012, 7, 1).name_of_month 160 | assert_equal 'August', Date.new(2012, 8, 1).name_of_month 161 | assert_equal 'September', Date.new(2012, 9, 1).name_of_month 162 | assert_equal 'October', Date.new(2012, 10, 1).name_of_month 163 | assert_equal 'November', Date.new(2012, 11, 1).name_of_month 164 | assert_equal 'December', Date.new(2012, 12, 1).name_of_month 165 | end 166 | end 167 | -------------------------------------------------------------------------------- /lib/test/modules/date/test_week.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 6 | 7 | class TestWeekForDate < Test::Unit::TestCase 8 | def test_week_of_month 9 | assert_equal 5, Date.new(2013, 1, 31).week_of_month 10 | 11 | assert_equal 5, Date.new(2013, 2, 28).week_of_month 12 | 13 | assert_equal 6, Date.new(2013, 3, 31).week_of_month 14 | 15 | assert_equal 5, Date.new(2013, 4, 30).week_of_month 16 | 17 | assert_equal 5, Date.new(2013, 5, 31).week_of_month 18 | 19 | assert_equal 6, Date.new(2013, 6, 30).week_of_month 20 | 21 | assert_equal 5, Date.new(2013, 7, 31).week_of_month 22 | 23 | assert_equal 5, Date.new(2013, 8, 31).week_of_month 24 | 25 | assert_equal 5, Date.new(2013, 9, 30).week_of_month 26 | 27 | assert_equal 5, Date.new(2013, 10, 31).week_of_month 28 | 29 | assert_equal 5, Date.new(2013, 11, 30).week_of_month 30 | 31 | assert_equal 5, Date.new(2013, 12, 31).week_of_month 32 | end 33 | 34 | def test_general_week_of_month 35 | assert_equal 4, Date.new(2013, 1, 31).general_week_of_month 36 | 37 | assert_equal 4, Date.new(2013, 2, 28).general_week_of_month 38 | 39 | assert_equal 5, Date.new(2013, 3, 31).general_week_of_month 40 | 41 | assert_equal 4, Date.new(2013, 4, 30).general_week_of_month 42 | 43 | assert_equal 4, Date.new(2013, 5, 31).general_week_of_month 44 | 45 | assert_equal 5, Date.new(2013, 6, 30).general_week_of_month 46 | 47 | assert_equal 4, Date.new(2013, 7, 31).general_week_of_month 48 | 49 | assert_equal 4, Date.new(2013, 8, 31).general_week_of_month 50 | 51 | assert_equal 5, Date.new(2013, 9, 30).general_week_of_month 52 | 53 | assert_equal 4, Date.new(2013, 10, 31).general_week_of_month 54 | 55 | assert_equal 4, Date.new(2013, 11, 30).general_week_of_month 56 | 57 | assert_equal 5, Date.new(2013, 12, 31).general_week_of_month 58 | end 59 | 60 | def test_week_split 61 | object = Date.new(2013, 1, 10) 62 | split_for_january = [[nil, nil, 1, 2, 3, 4, 5], 63 | [6, 7, 8, 9, 10, 11, 12], 64 | [13, 14, 15, 16, 17, 18, 19], 65 | [20, 21, 22, 23, 24, 25, 26], 66 | [27, 28, 29, 30, 31]] 67 | assert_kind_of Array, object.week_split 68 | assert_equal split_for_january, object.week_split 69 | 70 | object = Date.new(2013, 2, 15) 71 | split_for_october = [[nil, nil, nil, nil, nil, 1, 2], 72 | [3, 4, 5, 6, 7, 8, 9], 73 | [10, 11, 12, 13, 14, 15, 16], 74 | [17, 18, 19, 20, 21, 22, 23], 75 | [24, 25, 26, 27, 28]] 76 | assert_kind_of Array, object.week_split 77 | assert_equal split_for_october, object.week_split 78 | end 79 | 80 | def test_first_week? 81 | assert Date.new(2012, 1, 1).first_week? 82 | assert !Date.new(2012, 1, 31).first_week? 83 | end 84 | 85 | def test_second_week? 86 | assert Date.new(2013, 1, 6).second_week? 87 | assert !Date.new(2013, 1, 2).second_week? 88 | end 89 | 90 | def test_last_week? 91 | assert Date.new(2012, 10, 31).last_week? 92 | assert !Date.new(2012, 10, 20).last_week? 93 | end 94 | 95 | def test_total_weeks 96 | assert_equal 5, Date.new(2012, 10, 31).total_weeks 97 | assert_equal 6, Date.new(2012, 12, 20).total_weeks 98 | end 99 | 100 | def test_week_of_month_in_eng 101 | assert_equal 'First', Date.new(2012, 12, 1).week_of_month_in_eng 102 | assert_equal 'Second', Date.new(2012, 12, 4).week_of_month_in_eng 103 | assert_equal 'Third', Date.new(2012, 12, 9).week_of_month_in_eng 104 | assert_equal 'Fourth', Date.new(2012, 12, 16).week_of_month_in_eng 105 | assert_equal 'Fifth', Date.new(2012, 12, 24).week_of_month_in_eng 106 | assert_equal 'Sixth', Date.new(2012, 12, 31).week_of_month_in_eng 107 | end 108 | 109 | def test_week_of_month_in_fr 110 | assert_equal 'Premier', Date.new(2012, 12, 1).week_of_month_in_fr 111 | assert_equal 'Deuxième', Date.new(2012, 12, 4).week_of_month_in_fr 112 | assert_equal 'Troisième', Date.new(2012, 12, 9).week_of_month_in_fr 113 | assert_equal 'Quatrième', Date.new(2012, 12, 16).week_of_month_in_fr 114 | assert_equal 'Cinquième', Date.new(2012, 12, 24).week_of_month_in_fr 115 | assert_equal 'Sixième', Date.new(2012, 12, 31).week_of_month_in_fr 116 | end 117 | 118 | def test_week_of_month_in_ger 119 | assert_equal 'First', Date.new(2012, 12, 1).week_of_month_in_ger 120 | assert_equal 'Second', Date.new(2012, 12, 4).week_of_month_in_ger 121 | assert_equal 'Dritten', Date.new(2012, 12, 9).week_of_month_in_ger 122 | assert_equal 'Vierte', Date.new(2012, 12, 16).week_of_month_in_ger 123 | assert_equal 'Fünfte', Date.new(2012, 12, 24).week_of_month_in_ger 124 | assert_equal 'Sechste', Date.new(2012, 12, 31).week_of_month_in_ger 125 | end 126 | 127 | def test_week_of_month_in_ja 128 | assert_equal '第一', Date.new(2012, 12, 1).week_of_month_in_ja 129 | assert_equal '第二', Date.new(2012, 12, 4).week_of_month_in_ja 130 | assert_equal '第三', Date.new(2012, 12, 9).week_of_month_in_ja 131 | assert_equal '第四', Date.new(2012, 12, 16).week_of_month_in_ja 132 | assert_equal '第五', Date.new(2012, 12, 24).week_of_month_in_ja 133 | assert_equal '第六', Date.new(2012, 12, 31).week_of_month_in_ja 134 | end 135 | 136 | def test_week_end? 137 | assert !Date.new(2012, 10, 1).week_end? 138 | assert !Date.new(2012, 10, 31).week_end? 139 | assert Date.new(2012, 10, 6).week_end? 140 | assert Date.new(2012, 10, 7).week_end? 141 | end 142 | 143 | def test_working_day? 144 | assert Date.new(2012, 10, 1).working_day? 145 | assert Date.new(2012, 10, 31).working_day? 146 | assert !Date.new(2012, 10, 6).working_day? 147 | assert !Date.new(2012, 10, 7).working_day? 148 | end 149 | 150 | def test_days_past_in_week 151 | assert_equal 2, Date.new(2013, 1, 1).days_past_in_week 152 | end 153 | 154 | def test_days_left_in_week 155 | assert_equal 5, Date.new(2013, 1, 1).days_left_in_week 156 | end 157 | 158 | def test_starting_of_week 159 | assert_equal Date.new(2012, 11, 25), Date.new(2012, 11, 25).starting_of_week 160 | assert_equal Date.new(2012, 11, 25), Date.new(2012, 11, 26).starting_of_week 161 | assert_equal Date.new(2012, 11, 25), Date.new(2012, 11, 30).starting_of_week 162 | end 163 | 164 | def test_ending_of_week 165 | assert_equal Date.new(2012, 12, 1), Date.new(2012, 12, 1).ending_of_week 166 | assert_equal Date.new(2012, 12, 8), Date.new(2012, 12, 2).ending_of_week 167 | assert_equal Date.new(2012, 12, 8), Date.new(2012, 12, 3).ending_of_week 168 | assert_equal Date.new(2012, 12, 8), Date.new(2012, 12, 7).ending_of_week 169 | end 170 | 171 | def test_next_week 172 | assert_equal Date.new(2012, 12, 8), Date.new(2012, 12, 1).next_week 173 | assert_equal Date.new(2012, 12, 22), Date.new(2012, 12, 15).next_week 174 | assert_equal Date.new(2013, 1, 5), Date.new(2012, 12, 29).next_week 175 | assert_equal Date.new(2012, 12, 26), Date.new(2012, 12, 19).next_week 176 | end 177 | 178 | def test_previous_week 179 | assert_equal Date.new(2012, 12, 1), Date.new(2012, 12, 8).previous_week 180 | assert_equal Date.new(2012, 12, 15), Date.new(2012, 12, 22).previous_week 181 | assert_equal Date.new(2012, 12, 29), Date.new(2013, 1, 5).previous_week 182 | assert_equal Date.new(2012, 12, 19), Date.new(2012, 12, 26).previous_week 183 | end 184 | 185 | def test_monday_configured_starting_of_week 186 | WeekOfMonth.configuration.monday_active = true 187 | assert_equal Date.new(2012, 11, 19), Date.new(2012, 11, 25).starting_of_week 188 | assert_equal Date.new(2012, 11, 26), Date.new(2012, 11, 26).starting_of_week 189 | assert_equal Date.new(2012, 11, 26), Date.new(2012, 11, 30).starting_of_week 190 | WeekOfMonth.configuration.monday_active = false 191 | end 192 | 193 | def test_monday_configured_ending_of_week 194 | WeekOfMonth.configuration.monday_active = true 195 | assert_equal Date.new(2012, 12, 2), Date.new(2012, 12, 1).ending_of_week 196 | assert_equal Date.new(2012, 12, 2), Date.new(2012, 12, 2).ending_of_week 197 | assert_equal Date.new(2012, 12, 9), Date.new(2012, 12, 3).ending_of_week 198 | assert_equal Date.new(2012, 12, 9), Date.new(2012, 12, 7).ending_of_week 199 | WeekOfMonth.configuration.monday_active = false 200 | end 201 | end 202 | -------------------------------------------------------------------------------- /lib/test/modules/date/test_year.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | # @author Sachin Singh 5 | 6 | require 'test/unit' 7 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 8 | 9 | class TestYearForDate < Test::Unit::TestCase 10 | def test_years_between_dates 11 | assert_equal 3, Date.years_between_dates(Date.new(2015, 11, 1), Date.new(2012, 11, 15)) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/test/modules/time/test_constant.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 6 | 7 | class TestConstantForTime < Test::Unit::TestCase 8 | def test_constants_present? 9 | assert Time::WEEK_OF_MONTH_IN_ENG 10 | 11 | assert Time::WEEK_OF_MONTH_IN_GER 12 | 13 | assert Time::WEEK_OF_MONTH_IN_FR 14 | 15 | assert Time::WEEK_OF_MONTH_IN_JA 16 | 17 | assert Time::MONTH_WITH_DAY 18 | 19 | assert Time::MONTH_WITH_SEQUENCE 20 | 21 | assert Time::DAYS_IN_SEQUENCE 22 | end 23 | 24 | def test_constants_value 25 | assert_equal({ 1 => 'First', 2 => 'Second', 26 | 3 => 'Third', 4 => 'Fourth', 27 | 5 => 'Fifth', 6 => 'Sixth', 28 | 7 => 'Seventh' }, Time::WEEK_OF_MONTH_IN_ENG) 29 | 30 | assert_equal({ 1 => 'Premier', 2 => 'Deuxième', 31 | 3 => 'Troisième', 4 => 'Quatrième', 32 | 5 => 'Cinquième', 6 => 'Sixième', 33 | 7 => 'Septième' }, Time::WEEK_OF_MONTH_IN_FR) 34 | 35 | assert_equal({ 1 => 'First', 2 => 'Second', 36 | 3 => 'Dritten', 4 => 'Vierte', 37 | 5 => 'Fünfte', 6 => 'Sechste', 38 | 7 => 'siebte' }, Time::WEEK_OF_MONTH_IN_GER) 39 | 40 | assert_equal({ 1 => '第一', 2 => '第二', 41 | 3 => '第三', 4 => '第四', 42 | 5 => '第五', 6 => '第六', 43 | 7 => '第七' }, Time::WEEK_OF_MONTH_IN_JA) 44 | 45 | assert_equal({ january: 1, february: 2, march: 3, 46 | april: 4, may: 5, june: 6, july: 7, 47 | august: 8, september: 9, october: 10, 48 | november: 11, december: 12 }, Time::MONTH_WITH_SEQUENCE) 49 | 50 | assert_equal({ january: 31, february: 28, march: 31, 51 | april: 30, may: 31, june: 30, july: 31, 52 | august: 31, september: 30, october: 31, 53 | november: 30, december: 31 }, Time::MONTH_WITH_DAY) 54 | 55 | assert_equal(%w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday], 56 | Time::DAYS_IN_SEQUENCE) 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /lib/test/modules/time/test_day.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | 6 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 7 | class TestDayForTime < Test::Unit::TestCase 8 | def test_days_array 9 | object = Time.new(2012, 2, 8) 10 | 11 | days_array_for_february = [nil, nil, nil, 1, 2, 3, 4, 5, 12 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 13 | 15, 16, 17, 18, 19, 20, 21, 22, 14 | 23, 24, 25, 26, 27, 28, 29] 15 | assert_kind_of Array, object.days_array 16 | assert_equal days_array_for_february, object.days_array 17 | 18 | object = Time.new(2012, 7, 1) 19 | days_array_for_july = [1, 2, 3, 4, 5, 6, 7, 20 | 8, 9, 10, 11, 12, 13, 21 | 14, 15, 16, 17, 18, 19, 22 | 20, 21, 22, 23, 24, 25, 23 | 26, 27, 28, 29, 30, 31] 24 | assert_kind_of Array, object.days_array 25 | assert_equal days_array_for_july, object.days_array 26 | end 27 | 28 | def test_days_array_monday 29 | WeekOfMonth.configuration.monday_active = true 30 | object = Time.new(2014, 11, 3) 31 | 32 | days_array_for_november = [nil, nil, nil, nil, nil, 1, 2, 33 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 34 | 12, 13, 14, 15, 16, 17, 18, 35 | 19, 20, 21, 22, 23, 24, 25, 26, 36 | 27, 28, 29, 30] 37 | assert_kind_of Array, object.days_array 38 | assert_equal days_array_for_november, object.days_array 39 | 40 | object = Time.new(2014, 12, 1) 41 | days_array_for_december = [1, 2, 3, 4, 5, 6, 7, 42 | 8, 9, 10, 11, 12, 13, 43 | 14, 15, 16, 17, 18, 19, 44 | 20, 21, 22, 23, 24, 25, 45 | 26, 27, 28, 29, 30, 31] 46 | assert_kind_of Array, object.days_array 47 | assert_equal days_array_for_december, object.days_array 48 | end 49 | 50 | def test_name_of_week_day 51 | WeekOfMonth.configuration.monday_active = false 52 | assert_equal 'Saturday', Time.new(2012, 12, 1).name_of_week_day 53 | assert_equal 'Sunday', Time.new(2012, 12, 2).name_of_week_day 54 | assert_equal 'Monday', Time.new(2012, 12, 3).name_of_week_day 55 | assert_equal 'Tuesday', Time.new(2012, 12, 4).name_of_week_day 56 | assert_equal 'Wednesday', Time.new(2012, 12, 5).name_of_week_day 57 | assert_equal 'Thursday', Time.new(2012, 12, 6).name_of_week_day 58 | assert_equal 'Friday', Time.new(2012, 12, 7).name_of_week_day 59 | end 60 | 61 | def test_upcoming_sunday 62 | assert_equal Time.new(2013, 1, 6), Time.new(2013, 1, 1).upcoming_sunday 63 | assert_equal Time.new(2013, 1, 6), Time.new(2013, 1, 5).upcoming_sunday 64 | assert_equal Time.new(2013, 1, 13), Time.new(2013, 1, 7).upcoming_sunday 65 | assert_equal Time.new(2013, 1, 6), Time.new(2012, 12, 30).upcoming_sunday 66 | end 67 | 68 | def test_upcoming_monday 69 | assert_equal Time.new(2013, 1, 7), Time.new(2013, 1, 1).upcoming_monday 70 | end 71 | 72 | def test_upcoming_tuesday 73 | assert_equal Time.new(2013, 1, 8), Time.new(2013, 1, 1).upcoming_tuesday 74 | end 75 | 76 | def test_upcoming_wednesday 77 | assert_equal Time.new(2013, 1, 2), Time.new(2013, 1, 1).upcoming_wednesday 78 | end 79 | 80 | def test_upcoming_thursday 81 | assert_equal Time.new(2013, 1, 3), Time.new(2013, 1, 1).upcoming_thursday 82 | end 83 | 84 | def test_upcoming_friday 85 | assert_equal Time.new(2013, 1, 4), Time.new(2013, 1, 1).upcoming_friday 86 | end 87 | 88 | def test_upcoming_saturday 89 | assert_equal Time.new(2013, 1, 5), Time.new(2013, 1, 1).upcoming_saturday 90 | end 91 | 92 | def test_previous_saturday 93 | assert_equal Time.new(2012, 12, 29), Time.new(2013, 1, 1).previous_saturday 94 | end 95 | 96 | def test_previous_friday 97 | assert_equal Time.new(2012, 12, 28), Time.new(2013, 1, 1).previous_friday 98 | end 99 | 100 | def test_previous_thursday 101 | assert_equal Time.new(2012, 12, 27), Time.new(2013, 1, 1).previous_thursday 102 | end 103 | 104 | def test_previous_wednesday 105 | assert_equal Time.new(2012, 12, 26), Time.new(2013, 1, 1).previous_wednesday 106 | end 107 | 108 | def test_previous_tuesday 109 | assert_equal Time.new(2012, 12, 25), Time.new(2013, 1, 1).previous_tuesday 110 | end 111 | 112 | def test_previous_monday 113 | assert_equal Time.new(2012, 12, 31), Time.new(2013, 1, 1).previous_monday 114 | end 115 | 116 | def test_previous_sunday 117 | assert_equal Time.new(2012, 12, 30), Time.new(2013, 1, 1).previous_sunday 118 | assert_equal Time.new(2012, 12, 30), Time.new(2013, 1, 1).previous_sunday 119 | end 120 | 121 | end 122 | -------------------------------------------------------------------------------- /lib/test/modules/time/test_month.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | 6 | if RUBY_VERSION < '1.9' 7 | require('lib/week_of_month') 8 | else 9 | require_relative('../../../week_of_month') 10 | end 11 | 12 | class TestMonthForTime < Test::Unit::TestCase 13 | def test_month? 14 | assert Time.new(2012, 1, 1).january? 15 | 16 | assert Time.new(2012, 2, 1).february? 17 | 18 | assert Time.new(2012, 3, 1).march? 19 | 20 | assert Time.new(2012, 4, 1).april? 21 | 22 | assert Time.new(2012, 5, 1).may? 23 | 24 | assert Time.new(2012, 6, 1).june? 25 | 26 | assert Time.new(2012, 7, 1).july? 27 | 28 | assert Time.new(2012, 8, 1).august? 29 | 30 | assert Time.new(2012, 9, 1).september? 31 | 32 | assert Time.new(2012, 10, 1).october? 33 | 34 | assert Time.new(2012, 11, 1).november? 35 | 36 | assert Time.new(2012, 12, 1).december? 37 | end 38 | 39 | def test_last_day_of_month 40 | assert_equal 31, Time.new(2012, 1, 31).last_day_of_month 41 | 42 | assert_equal 29, Time.new(2012, 2, 29).last_day_of_month 43 | 44 | assert_equal 31, Time.new(2012, 3, 31).last_day_of_month 45 | 46 | assert_equal 30, Time.new(2012, 4, 30).last_day_of_month 47 | 48 | assert_equal 31, Time.new(2012, 5, 31).last_day_of_month 49 | 50 | assert_equal 30, Time.new(2012, 6, 30).last_day_of_month 51 | 52 | assert_equal 31, Time.new(2012, 7, 31).last_day_of_month 53 | 54 | assert_equal 31, Time.new(2012, 8, 31).last_day_of_month 55 | 56 | assert_equal 30, Time.new(2012, 9, 30).last_day_of_month 57 | 58 | assert_equal 31, Time.new(2012, 10, 31).last_day_of_month 59 | 60 | assert_equal 30, Time.new(2012, 11, 30).last_day_of_month 61 | 62 | assert_equal 31, Time.new(2012, 12, 31).last_day_of_month 63 | end 64 | 65 | def test_ending_of_month 66 | assert_equal Time.new(2012, 1, 31), Time.new(2012, 1, 1).ending_of_month 67 | 68 | assert_equal Time.new(2012, 2, 29), Time.new(2012, 2, 2).ending_of_month 69 | 70 | assert_equal Time.new(2012, 3, 31), Time.new(2012, 3, 1).ending_of_month 71 | 72 | assert_equal Time.new(2012, 4, 30), Time.new(2012, 4, 3).ending_of_month 73 | 74 | assert_equal Time.new(2012, 5, 31), Time.new(2012, 5, 1).ending_of_month 75 | 76 | assert_equal Time.new(2012, 6, 30), Time.new(2012, 6, 30).ending_of_month 77 | 78 | assert_equal Time.new(2012, 7, 31), Time.new(2012, 7, 1).ending_of_month 79 | 80 | assert_equal Time.new(2012, 8, 31), Time.new(2012, 8, 5).ending_of_month 81 | 82 | assert_equal Time.new(2012, 9, 30), Time.new(2012, 9, 2).ending_of_month 83 | 84 | assert_equal Time.new(2012, 10, 31), Time.new(2012, 10, 22).ending_of_month 85 | 86 | assert_equal Time.new(2012, 11, 30), Time.new(2012, 11, 10).ending_of_month 87 | 88 | assert_equal Time.new(2012, 12, 31), Time.new(2012, 12, 15).ending_of_month 89 | end 90 | 91 | def test_beginning_of_month 92 | assert_equal Time.new(2012, 1, 1), Time.new(2012, 1, 31).beginning_of_month 93 | 94 | assert_equal Time.new(2012, 2, 1), Time.new(2012, 2, 29).beginning_of_month 95 | 96 | assert_equal Time.new(2012, 3, 1), Time.new(2012, 3, 31).beginning_of_month 97 | 98 | assert_equal Time.new(2012, 4, 1), Time.new(2012, 4, 30).beginning_of_month 99 | 100 | assert_equal Time.new(2012, 5, 1), Time.new(2012, 5, 31).beginning_of_month 101 | 102 | assert_equal Time.new(2012, 6, 1), Time.new(2012, 6, 30).beginning_of_month 103 | 104 | assert_equal Time.new(2012, 7, 1), Time.new(2012, 7, 31).beginning_of_month 105 | 106 | assert_equal Time.new(2012, 8, 1), Time.new(2012, 8, 31).beginning_of_month 107 | 108 | assert_equal Time.new(2012, 9, 1), Time.new(2012, 9, 30).beginning_of_month 109 | 110 | assert_equal Time.new(2012, 10, 1), Time.new(2012, 10, 31).beginning_of_month 111 | 112 | assert_equal Time.new(2012, 11, 1), Time.new(2012, 11, 30).beginning_of_month 113 | 114 | assert_equal Time.new(2012, 12, 1), Time.new(2012, 12, 31).beginning_of_month 115 | end 116 | 117 | def test_all_sundays_in_month 118 | assert_equal [6, 13, 20, 27], Time.new(2013, 1, 1).all_sundays_in_month 119 | assert_equal [3, 10, 17, 24], Time.new(2013, 2, 1).all_sundays_in_month 120 | end 121 | 122 | def test_all_mondays_in_month 123 | assert_equal [7, 14, 21, 28], Time.new(2013, 1, 1).all_mondays_in_month 124 | assert_equal [4, 11, 18, 25], Time.new(2013, 2, 1).all_mondays_in_month 125 | end 126 | 127 | def test_all_tuesdays_in_month 128 | assert_equal [1, 8, 15, 22, 29], Time.new(2013, 1, 1).all_tuesdays_in_month 129 | assert_equal [5, 12, 19, 26], Time.new(2013, 2, 1).all_tuesdays_in_month 130 | end 131 | 132 | def test_all_wednesdays_in_month 133 | assert_equal [2, 9, 16, 23, 30], Time.new(2013, 1, 1).all_wednesdays_in_month 134 | assert_equal [6, 13, 20, 27], Time.new(2013, 2, 1).all_wednesdays_in_month 135 | end 136 | 137 | def test_all_thursdays_in_month 138 | assert_equal [3, 10, 17, 24, 31], Time.new(2013, 1, 1).all_thursdays_in_month 139 | assert_equal [7, 14, 21, 28], Time.new(2013, 2, 1).all_thursdays_in_month 140 | end 141 | 142 | def test_all_fridays_in_month 143 | assert_equal [4, 11, 18, 25], Time.new(2013, 1, 1).all_fridays_in_month 144 | assert_equal [1, 8, 15, 22], Time.new(2013, 2, 1).all_fridays_in_month 145 | end 146 | 147 | def test_all_saturdays_in_month 148 | assert_equal [5, 12, 19, 26], Time.new(2013, 1, 1).all_saturdays_in_month 149 | assert_equal [2, 9, 16, 23], Time.new(2013, 2, 1).all_saturdays_in_month 150 | end 151 | 152 | def test_name_of_month 153 | assert_equal 'January', Time.new(2012, 1, 1).name_of_month 154 | assert_equal 'February', Time.new(2012, 2, 1).name_of_month 155 | assert_equal 'March', Time.new(2012, 3, 1).name_of_month 156 | assert_equal 'April', Time.new(2012, 4, 1).name_of_month 157 | assert_equal 'May', Time.new(2012, 5, 1).name_of_month 158 | assert_equal 'June', Time.new(2012, 6, 1).name_of_month 159 | assert_equal 'July', Time.new(2012, 7, 1).name_of_month 160 | assert_equal 'August', Time.new(2012, 8, 1).name_of_month 161 | assert_equal 'September', Time.new(2012, 9, 1).name_of_month 162 | assert_equal 'October', Time.new(2012, 10, 1).name_of_month 163 | assert_equal 'November', Time.new(2012, 11, 1).name_of_month 164 | assert_equal 'December', Time.new(2012, 12, 1).name_of_month 165 | end 166 | end 167 | -------------------------------------------------------------------------------- /lib/test/modules/time/test_week.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | require 'test/unit' 5 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 6 | 7 | class TestWeekForTime < Test::Unit::TestCase 8 | def test_week_of_month 9 | assert_equal 5, Time.new(2013, 1, 31).week_of_month 10 | 11 | assert_equal 5, Time.new(2013, 2, 28).week_of_month 12 | 13 | assert_equal 6, Time.new(2013, 3, 31).week_of_month 14 | 15 | assert_equal 5, Time.new(2013, 4, 30).week_of_month 16 | 17 | assert_equal 5, Time.new(2013, 5, 31).week_of_month 18 | 19 | assert_equal 6, Time.new(2013, 6, 30).week_of_month 20 | 21 | assert_equal 5, Time.new(2013, 7, 31).week_of_month 22 | 23 | assert_equal 5, Time.new(2013, 8, 31).week_of_month 24 | 25 | assert_equal 5, Time.new(2013, 9, 30).week_of_month 26 | 27 | assert_equal 5, Time.new(2013, 10, 31).week_of_month 28 | 29 | assert_equal 5, Time.new(2013, 11, 30).week_of_month 30 | 31 | assert_equal 5, Time.new(2013, 12, 31).week_of_month 32 | end 33 | 34 | def test_general_week_of_month 35 | assert_equal 4, Time.new(2013, 1, 31).general_week_of_month 36 | 37 | assert_equal 4, Time.new(2013, 2, 28).general_week_of_month 38 | 39 | assert_equal 5, Time.new(2013, 3, 31).general_week_of_month 40 | 41 | assert_equal 4, Time.new(2013, 4, 30).general_week_of_month 42 | 43 | assert_equal 4, Time.new(2013, 5, 31).general_week_of_month 44 | 45 | assert_equal 5, Time.new(2013, 6, 30).general_week_of_month 46 | 47 | assert_equal 4, Time.new(2013, 7, 31).general_week_of_month 48 | 49 | assert_equal 4, Time.new(2013, 8, 31).general_week_of_month 50 | 51 | assert_equal 5, Time.new(2013, 9, 30).general_week_of_month 52 | 53 | assert_equal 4, Time.new(2013, 10, 31).general_week_of_month 54 | 55 | assert_equal 4, Time.new(2013, 11, 30).general_week_of_month 56 | 57 | assert_equal 5, Time.new(2013, 12, 31).general_week_of_month 58 | end 59 | 60 | def test_week_split 61 | object = Time.new(2013, 1, 10) 62 | split_for_january = [[nil, nil, 1, 2, 3, 4, 5], 63 | [6, 7, 8, 9, 10, 11, 12], 64 | [13, 14, 15, 16, 17, 18, 19], 65 | [20, 21, 22, 23, 24, 25, 26], 66 | [27, 28, 29, 30, 31]] 67 | assert_kind_of Array, object.week_split 68 | assert_equal split_for_january, object.week_split 69 | 70 | object = Time.new(2013, 2, 15) 71 | split_for_october = [[nil, nil, nil, nil, nil, 1, 2], 72 | [3, 4, 5, 6, 7, 8, 9], 73 | [10, 11, 12, 13, 14, 15, 16], 74 | [17, 18, 19, 20, 21, 22, 23], 75 | [24, 25, 26, 27, 28]] 76 | assert_kind_of Array, object.week_split 77 | assert_equal split_for_october, object.week_split 78 | end 79 | 80 | def test_first_week? 81 | assert Time.new(2012, 1, 1).first_week? 82 | assert !Time.new(2012, 1, 31).first_week? 83 | end 84 | 85 | def test_second_week? 86 | assert Time.new(2013, 1, 6).second_week? 87 | assert !Time.new(2013, 1, 2).second_week? 88 | end 89 | 90 | def test_last_week? 91 | assert Time.new(2012, 10, 31).last_week? 92 | assert !Time.new(2012, 10, 20).last_week? 93 | end 94 | 95 | def test_total_weeks 96 | assert_equal 5, Time.new(2012, 10, 31).total_weeks 97 | assert_equal 6, Time.new(2012, 12, 20).total_weeks 98 | end 99 | 100 | def test_week_of_month_in_eng 101 | assert_equal 'First', Time.new(2012, 12, 1).week_of_month_in_eng 102 | assert_equal 'Second', Time.new(2012, 12, 4).week_of_month_in_eng 103 | assert_equal 'Third', Time.new(2012, 12, 9).week_of_month_in_eng 104 | assert_equal 'Fourth', Time.new(2012, 12, 16).week_of_month_in_eng 105 | assert_equal 'Fifth', Time.new(2012, 12, 24).week_of_month_in_eng 106 | assert_equal 'Sixth', Time.new(2012, 12, 31).week_of_month_in_eng 107 | end 108 | 109 | def test_week_of_month_in_fr 110 | assert_equal 'Premier', Time.new(2012, 12, 1).week_of_month_in_fr 111 | assert_equal 'Deuxième', Time.new(2012, 12, 4).week_of_month_in_fr 112 | assert_equal 'Troisième', Time.new(2012, 12, 9).week_of_month_in_fr 113 | assert_equal 'Quatrième', Time.new(2012, 12, 16).week_of_month_in_fr 114 | assert_equal 'Cinquième', Time.new(2012, 12, 24).week_of_month_in_fr 115 | assert_equal 'Sixième', Time.new(2012, 12, 31).week_of_month_in_fr 116 | end 117 | 118 | def test_week_of_month_in_ger 119 | assert_equal 'First', Time.new(2012, 12, 1).week_of_month_in_ger 120 | assert_equal 'Second', Time.new(2012, 12, 4).week_of_month_in_ger 121 | assert_equal 'Dritten', Time.new(2012, 12, 9).week_of_month_in_ger 122 | assert_equal 'Vierte', Time.new(2012, 12, 16).week_of_month_in_ger 123 | assert_equal 'Fünfte', Time.new(2012, 12, 24).week_of_month_in_ger 124 | assert_equal 'Sechste', Time.new(2012, 12, 31).week_of_month_in_ger 125 | end 126 | 127 | def test_week_of_month_in_ja 128 | assert_equal '第一', Time.new(2012, 12, 1).week_of_month_in_ja 129 | assert_equal '第二', Time.new(2012, 12, 4).week_of_month_in_ja 130 | assert_equal '第三', Time.new(2012, 12, 9).week_of_month_in_ja 131 | assert_equal '第四', Time.new(2012, 12, 16).week_of_month_in_ja 132 | assert_equal '第五', Time.new(2012, 12, 24).week_of_month_in_ja 133 | assert_equal '第六', Time.new(2012, 12, 31).week_of_month_in_ja 134 | end 135 | 136 | def test_week_end? 137 | assert !Time.new(2012, 10, 1).week_end? 138 | assert !Time.new(2012, 10, 31).week_end? 139 | assert Time.new(2012, 10, 6).week_end? 140 | assert Time.new(2012, 10, 7).week_end? 141 | end 142 | 143 | def test_working_day? 144 | assert Time.new(2012, 10, 1).working_day? 145 | assert Time.new(2012, 10, 31).working_day? 146 | assert !Time.new(2012, 10, 6).working_day? 147 | assert !Time.new(2012, 10, 7).working_day? 148 | end 149 | 150 | def test_days_past_in_week 151 | assert_equal 2, Time.new(2013, 1, 1).days_past_in_week 152 | end 153 | 154 | def test_days_left_in_week 155 | assert_equal 5, Time.new(2013, 1, 1).days_left_in_week 156 | end 157 | 158 | def test_starting_of_week 159 | assert_equal Time.new(2012, 11, 25), Time.new(2012, 11, 25).starting_of_week 160 | assert_equal Time.new(2012, 11, 25), Time.new(2012, 11, 26).starting_of_week 161 | assert_equal Time.new(2012, 11, 25), Time.new(2012, 11, 30).starting_of_week 162 | end 163 | 164 | def test_ending_of_week 165 | assert_equal Time.new(2012, 12, 1), Time.new(2012, 12, 1).ending_of_week 166 | assert_equal Time.new(2012, 12, 8), Time.new(2012, 12, 2).ending_of_week 167 | assert_equal Time.new(2012, 12, 8), Time.new(2012, 12, 3).ending_of_week 168 | assert_equal Time.new(2012, 12, 8), Time.new(2012, 12, 7).ending_of_week 169 | end 170 | 171 | def test_next_week 172 | assert_equal Time.new(2012, 12, 8), Time.new(2012, 12, 1).next_week 173 | assert_equal Time.new(2012, 12, 22), Time.new(2012, 12, 15).next_week 174 | assert_equal Time.new(2013, 1, 5), Time.new(2012, 12, 29).next_week 175 | assert_equal Time.new(2012, 12, 26), Time.new(2012, 12, 19).next_week 176 | end 177 | 178 | def test_previous_week 179 | assert_equal Time.new(2012, 12, 1), Time.new(2012, 12, 8).previous_week 180 | assert_equal Time.new(2012, 12, 15), Time.new(2012, 12, 22).previous_week 181 | assert_equal Time.new(2012, 12, 29), Time.new(2013, 1, 5).previous_week 182 | assert_equal Time.new(2012, 12, 19), Time.new(2012, 12, 26).previous_week 183 | end 184 | 185 | def test_monday_configured_starting_of_week 186 | WeekOfMonth.configuration.monday_active = true 187 | assert_equal Time.new(2012, 11, 19), Time.new(2012, 11, 25).starting_of_week 188 | assert_equal Time.new(2012, 11, 26), Time.new(2012, 11, 26).starting_of_week 189 | assert_equal Time.new(2012, 11, 26), Time.new(2012, 11, 30).starting_of_week 190 | WeekOfMonth.configuration.monday_active = false 191 | end 192 | 193 | def test_monday_configured_ending_of_week 194 | WeekOfMonth.configuration.monday_active = true 195 | assert_equal Time.new(2012, 12, 2), Time.new(2012, 12, 1).ending_of_week 196 | assert_equal Time.new(2012, 12, 2), Time.new(2012, 12, 2).ending_of_week 197 | assert_equal Time.new(2012, 12, 9), Time.new(2012, 12, 3).ending_of_week 198 | assert_equal Time.new(2012, 12, 9), Time.new(2012, 12, 7).ending_of_week 199 | WeekOfMonth.configuration.monday_active = false 200 | end 201 | end 202 | -------------------------------------------------------------------------------- /lib/test/modules/time/test_year.rb: -------------------------------------------------------------------------------- 1 | #!/bin/env ruby 2 | # encoding: utf-8 3 | 4 | # @author Sachin Singh 5 | 6 | require 'test/unit' 7 | RUBY_VERSION < '1.9' ? require('lib/week_of_month') : require_relative('../../../week_of_month') 8 | 9 | class TestYearForTime < Test::Unit::TestCase 10 | def test_years_between_dates 11 | assert_equal 3, Time.years_between_dates(Time.new(2015, 11, 1), Time.new(2012, 11, 15)) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/week_of_month.rb: -------------------------------------------------------------------------------- 1 | require 'date' 2 | require 'time' 3 | 4 | # Supports both version of ruby 1.8 and 1.9 for loading a file. 5 | def require_file(file_name) 6 | RUBY_VERSION < '1.9' ? require(file_name) : require_relative(file_name) 7 | end 8 | 9 | require_file 'modules/configuration' 10 | require_file 'modules/day' 11 | require_file 'modules/month' 12 | require_file 'modules/week' 13 | require_file 'modules/year' 14 | 15 | class Date 16 | include WeekOfMonth::Day 17 | include WeekOfMonth::Month 18 | include WeekOfMonth::Week 19 | include WeekOfMonth::Year 20 | end 21 | 22 | [Time, Date].each do |klass| 23 | klass.class_eval do 24 | unless method_defined?(:to_date) 25 | def to_date 26 | ::Date.new(year, month, day) 27 | end 28 | end 29 | 30 | # these methods are already defined in ruby 1.9 and above 31 | unless method_defined?(:sunday?) 32 | def sunday? 33 | wday == 0 34 | end 35 | 36 | def monday? 37 | wday == 1 38 | end 39 | 40 | def tuesday? 41 | wday == 2 42 | end 43 | 44 | def wednesday? 45 | wday == 3 46 | end 47 | 48 | def thursday? 49 | wday == 4 50 | end 51 | 52 | def friday? 53 | wday == 5 54 | end 55 | 56 | def saturday? 57 | wday == 6 58 | end 59 | end 60 | end 61 | end 62 | 63 | class Time 64 | include WeekOfMonth::Day 65 | include WeekOfMonth::Month 66 | include WeekOfMonth::Week 67 | include WeekOfMonth::Year 68 | 69 | def leap? 70 | to_date.leap? 71 | end 72 | 73 | if RUBY_VERSION < '1.9' 74 | def self.new(year = Time.now.year, month = Time.now.month, day = Time.now.day, hour = Time.now.hour, min = Time.now.min, sec = Time.now.sec, millisecond = (Time.now.to_f * 1000.0).to_i) 75 | Time.local(year, month, day, hour, min, sec, millisecond) 76 | end 77 | end 78 | end 79 | 80 | class Hash 81 | unless method_defined?(:key) 82 | def key(value) 83 | result = nil 84 | each { |k, v| result = k if v == value && result.nil?; } if values.include?(value) 85 | result 86 | end 87 | end 88 | end 89 | 90 | module WeekOfMonth 91 | def self.first_day=(val) 92 | @first_day = DAYS_IN_SEQUENCE[val] 93 | end 94 | 95 | def self.first_day 96 | @first_day ||= 0 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012-2017 Sachin Singh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | Creative Commons License 23 | -------------------------------------------------------------------------------- /week_of_month.gemspec: -------------------------------------------------------------------------------- 1 | # @author Sachin Singh 2 | 3 | require File.expand_path('../lib/modules/version', __FILE__) 4 | 5 | Gem::Specification.new do |s| 6 | s.name = 'week_of_month' 7 | s.version = WeekOfMonth::VERSION 8 | s.summary = 'Week of month!' 9 | s.description = 'Its gives you week_of_month method on date and time objects, that returns week of the month.' 10 | s.authors = %w[Sachin87 Tommyixi Matt-- berikin pablorusso AstonJ swapnilchincholkar hitendrasingh ilake fursich] 11 | s.email = %w[sachin.y87@gmail.com] 12 | s.homepage = 'https://github.com/sachin87/week-of-month' 13 | s.files = `git ls-files`.split("\n").sort 14 | s.require_paths = ['lib'] 15 | s.license = 'MIT' 16 | 17 | s.add_development_dependency 'rake', '>= 12.0.0' 18 | s.add_development_dependency 'bundler', '>= 1.15.4' 19 | s.add_development_dependency 'test-unit', '>= 3.2.5' 20 | end 21 | --------------------------------------------------------------------------------