eztime 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "jeweler", "~> 1.8.4"
10
+ gem "rcov"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.3)
11
+ rake (0.9.2.2)
12
+ rcov (1.0.0)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ jeweler (~> 1.8.4)
21
+ rcov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Chris Scharf
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,60 @@
1
+ h1. EZTime
2
+
3
+ h2. Installation
4
+
5
+ script/plugin install git@github.com:scharfie/eztime.git
6
+
7
+ EZTime is a simple extension to the Date class in Ruby to provide easy
8
+ formatting of dates and times.
9
+
10
+ h2. Can't I just use strftime?
11
+
12
+ h3. Feature Support
13
+
14
+ EZTime has a number of formatting features not available to strftime.
15
+ For example, what if you want to display the ordinal (st, nd, rd, th)
16
+ for a date? (i.e. 1st, 2nd, etc.) With EZTime, ordinals are easy.
17
+
18
+ How about displaying the meridian (AM/PM) in lowercase? What about
19
+ only displaying the first letter (a/p)? While strftime is powerful,
20
+ there are simply some formatting options not available, which is a
21
+ shame.
22
+
23
+ h3. Simplicity
24
+
25
+ EZTime is much easier to use than strftime because it doesn't require
26
+ much memorization. For example, isn't @:month/:day/:year@ easier than
27
+ @%m/%d/%Y@ ?
28
+
29
+ Not convinced? How about @:day:ordinal of :month_name, :year@
30
+ (_i.e. 20th of December, 2003_).
31
+
32
+ h3. Extensibility
33
+
34
+ Perhaps the strongest feature of EZTime is that it can be "extended" by simply
35
+ adding new methods to the EZTime module. The reason this works is because EZTime
36
+ eval's the formatting string after a simple substitution, replacing the :name
37
+ with a method call to that name. The upside to this is that everything you put
38
+ in the formatting string can be accessed by itself as a method call.
39
+
40
+ h3. Formatting variants
41
+
42
+ Certain methods have one or more variants, all following a standard convention:
43
+ * 'z': (prefix) element will include a leading 0 digit
44
+ * 'l': (prefix) (lowercase L): element will be converted to all lowercase
45
+ * 's': (prefix) small variants. For example, smeridian will return 'A' or 'P'
46
+ * '12': (suffix): element will be in 12-hour format
47
+
48
+ See the documentation for the various methods for examples of these modifiers.
49
+
50
+ h3. Known Issues
51
+
52
+ Currently, using quotes or apostrophes in an EZTime format string is not supported -
53
+ it results in compile errors. If there is a need for either of these characters,
54
+ however, I will look into correcting the situation.
55
+
56
+ h2. Credits
57
+
58
+ EZTime was created by Chris Scharf (http://tiny.scharfie.com)
59
+
60
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "eztime"
18
+ gem.homepage = "http://github.com/scharfie/eztime"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Easier date/time manipulation}
21
+ gem.description = %Q{Easier date/time manipulation}
22
+ gem.email = "scharfie@gmail.com"
23
+ gem.authors = ["Chris Scharf"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "eztime #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/eztime.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{eztime}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Scharf"]
12
+ s.date = %q{2012-07-05}
13
+ s.description = %q{Easier date/time manipulation}
14
+ s.email = %q{scharfie@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.textile",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "eztime.gemspec",
27
+ "init.rb",
28
+ "install.rb",
29
+ "lib/eztime.rb",
30
+ "test/helper.rb",
31
+ "test/test_eztime.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/scharfie/eztime}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.4.2}
37
+ s.summary = %q{Easier date/time manipulation}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
44
+ s.add_development_dependency(%q<rcov>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
47
+ s.add_dependency(%q<rcov>, [">= 0"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ end
54
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'eztime'
data/install.rb ADDED
File without changes
data/lib/eztime.rb ADDED
@@ -0,0 +1,124 @@
1
+ require 'date'
2
+
3
+ # The following code was based on work found at:
4
+ # http://www.bigbold.com/snippets/user/jswizard#post2368
5
+ class Numeric
6
+ # Returns the cardinal (number) and ordinal (st, nd, rd, th, etc.)
7
+ # Pass include_cardinal as false to only return the ordinal
8
+ def ordinal(include_cardinal=true)
9
+ cardinal = self.to_i.abs
10
+ if (10...20).include?(cardinal) then
11
+ include_cardinal ? cardinal.to_s << 'th' : 'th'
12
+ else
13
+ ord = %w{th st nd rd th th th th th th}[cardinal % 10]
14
+ include_cardinal ? cardinal.to_s << ord : ord
15
+ end
16
+ end
17
+ end
18
+
19
+ module EZTime
20
+ public
21
+ # Returns only the lower two digits of the year
22
+ # (i.e. 2006 => 06)
23
+ def syear; year.to_s[-2..-1]; end
24
+
25
+ # Returns the name of the month
26
+ # (January, February, etc.)
27
+ def month_name; Date::MONTHNAMES[month]; end
28
+
29
+ # Returns the abbreviated name of the month
30
+ # (Jan, Feb, etc.)
31
+ def month_abbr; Date::ABBR_MONTHNAMES[month]; end
32
+
33
+ # Returns the name of the weekday
34
+ # (Sunday, Monday, etc.)
35
+ def day_name; Date::DAYNAMES[wday]; end
36
+
37
+ # Returns the abbreviated name of the weekday
38
+ # (Sun, Mon, etc.)
39
+ def day_abbr; Date::ABBR_DAYNAMES[wday]; end
40
+
41
+ alias :nmonth :month_name
42
+ alias :amonth :month_abbr
43
+
44
+ alias :nday :day_name
45
+ alias :aday :day_abbr
46
+
47
+ # Returns the month as a zero-padded string
48
+ # (i.e. June => 06)
49
+ def zmonth; '%02d' % month; end
50
+
51
+ # Returns the day as a zero-padded string
52
+ # (5 => 05)
53
+ def zday; '%02d' % mday; end
54
+
55
+ # Returns the hour as a zero-padded string
56
+ # (3 => 03)
57
+ def zhour; '%02d' % hour; end
58
+
59
+ # Returns the hour in 12-hour format
60
+ # (5:00pm => 5)
61
+ def hour12; hour % 12 == 0 ? 12 : hour % 12; end
62
+
63
+ # Returns the hour in 12-hour format as a zero-padded string
64
+ # (5:00pm => 05)
65
+ def zhour12; '%02d' % hour12; end
66
+
67
+ # Returns the minute as a zero-padded string
68
+ # Note: If you need just the minute, use min
69
+ def minute; '%02d' % min; end
70
+
71
+ # Returns the second as a zero-padded string
72
+ # Note: If you need just the second, use sec
73
+ def second; '%02d' % sec; end
74
+
75
+ # Returns the meridian
76
+ # (AM/PM)
77
+ def meridian; hour >= 12 ? 'PM' : 'AM'; end
78
+
79
+ # Returns the meridian in short form (first letter)
80
+ # (A/P)
81
+ def smeridian; meridian[0].chr; end
82
+
83
+ # Returns the meridian in lowercase
84
+ # (am/pm)
85
+ def lmeridian; meridian.downcase; end
86
+
87
+ # Returns the meridian in lowercase, short form (first letter)
88
+ # (a/p)
89
+ def lsmeridian; smeridian.downcase; end
90
+
91
+ # Returns the ordinal of the day
92
+ # (1 => st, 2 => nd, 3 => rd, 4.. => th)
93
+ def ordinal; mday.ordinal(false); end
94
+ alias :ord :ordinal
95
+
96
+ # Formats the date/time according to the formatting string format_str
97
+ # The formatting string consists of any of the methods defined in EZTime
98
+ # (such as meridian, ordinal, zhour, etc.) as well as any other methods
99
+ # available to the object class. The methods are named in the string by
100
+ # preceeded them with a single colon (:). Any characters not preceeded by
101
+ # a colon will be passed through directly.
102
+ #
103
+ # Example
104
+ #
105
+ # d = DateTime.civil(2003, 12, 20, 17, 30, 0)
106
+ # puts d.eztime(":day :nmonth :year at :hour12::minute::second :lmeridian")
107
+ #
108
+ # Output: 20 December 2003 at 5:30:00 pm
109
+ def eztime(format_str)
110
+ eval("'" + format_str.gsub(/:([a-z_]{1,}[0-9]{0,2})/, '\' + \1.to_s + \'') + "'")
111
+ end
112
+ end
113
+
114
+ # Include the EZTime module into the Time class
115
+ class Time; include EZTime; end
116
+
117
+ # Include the EZTime module into the Date class
118
+ class Date; include EZTime; end
119
+
120
+ # class DateTime
121
+ # include EZTime
122
+ # def ordinal; mday.ordinal(false); end
123
+ # def self.ordinal; mday.ordinal(false); end
124
+ # end
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'eztime'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,111 @@
1
+ require 'helper'
2
+
3
+ class EZTimeTest < Test::Unit::TestCase
4
+ def setup
5
+ # 20 December 2003 @ 5:45:23 PM
6
+ @stamp = DateTime.civil(2003, 12, 20, 17, 45, 23)
7
+
8
+ # 1 Jan 2004 4:30:00 PM
9
+ @ny = DateTime.civil(2004, 1, 1, 16, 30, 0)
10
+ end
11
+
12
+ def test_constant
13
+ assert_equal 'pizza time', @stamp.eztime('pizza time')
14
+ assert_equal 'pizza : time', @stamp.eztime('pizza : time')
15
+ assert_equal 'pizza :::: time', @stamp.eztime('pizza :::: time')
16
+ end
17
+
18
+ def test_month_day_year
19
+ assert_equal '12/20/2003', @stamp.eztime(':month/:day/:year')
20
+ assert_equal '2003-12-20', @stamp.eztime(':year-:month-:day')
21
+ assert_equal '12-03', @stamp.eztime(':month-:syear')
22
+
23
+ assert_equal '1/1/2004', @ny.eztime(':month/:day/:year')
24
+ assert_equal '01/01/2004', @ny.eztime(':zmonth/:zday/:year')
25
+ end
26
+
27
+ def test_named_months_and_days
28
+ assert_equal 'December 20, 2003', @stamp.eztime(':month_name :day, :year')
29
+ assert_equal '20 December 03', @stamp.eztime(':day :month_name :syear')
30
+
31
+ assert_equal 'Dec 20, 2003', @stamp.eztime(':month_abbr :day, :year')
32
+ assert_equal '20 Dec 03', @stamp.eztime(':day :month_abbr :syear')
33
+
34
+ 1.upto(12) do |month|
35
+ assert_equal Date::MONTHNAMES[month], Date.civil(2003, month, 1).eztime(':month_name')
36
+ assert_equal Date::ABBR_MONTHNAMES[month], Date.civil(2003, month, 1).eztime(':month_abbr')
37
+ end
38
+
39
+ 0.upto(6) do |wday|
40
+ d = Date.civil(2006, 8, 6 + wday)
41
+ assert_equal wday, d.wday
42
+ assert_equal Date::DAYNAMES[wday], d.eztime(':day_name')
43
+ assert_equal Date::ABBR_DAYNAMES[wday], d.eztime(':day_abbr')
44
+ end
45
+ end
46
+
47
+ def test_hour_minute_second_meridian
48
+ assert_equal '17:45:23 pm', @stamp.eztime(':hour::minute::second :lmeridian')
49
+ assert_equal '17:45:23 PM', @stamp.eztime(':hour::minute::second :meridian')
50
+ assert_equal '17:45:23 P', @stamp.eztime(':hour::minute::second :smeridian')
51
+ assert_equal '17:45:23 p', @stamp.eztime(':hour::minute::second :lsmeridian')
52
+
53
+ # now test am/AM/A/a
54
+ @stamp2 = DateTime.civil(2003, 12, 12, 5, 45, 23)
55
+ assert_equal '5:45:23 am', @stamp2.eztime(':hour::minute::second :lmeridian')
56
+ assert_equal '5:45:23 AM', @stamp2.eztime(':hour::minute::second :meridian')
57
+ assert_equal '5:45:23 A', @stamp2.eztime(':hour::minute::second :smeridian')
58
+ assert_equal '5:45:23 a', @stamp2.eztime(':hour::minute::second :lsmeridian')
59
+ end
60
+
61
+ def test_everything
62
+ d = DateTime.civil(2000, 1, 1, 0, 0, 0)
63
+ 0.upto(1440/5) do |min|
64
+ d += 300 if min > 0
65
+ assert_equal d.strftime('%I:%M'), d.eztime(':hour12::minute')
66
+ end
67
+
68
+ 1.upto(31) do |day|
69
+ d = DateTime.civil(2000, 1, day, 0, 0, 0)
70
+ assert_equal d.strftime('%m/%d/%Y %A'), d.eztime(':zmonth/:zday/:year :nday')
71
+ end
72
+ end
73
+
74
+ def test_all
75
+ assert_equal '05:45 PM on December 20th, 2003 (Saturday)',
76
+ @stamp.eztime(':zhour12::minute :meridian on :month_name :day:ord, :year (:day_name)')
77
+
78
+ assert_equal '20 December 03 was a Saturday.',
79
+ @stamp.eztime(':day :nmonth :syear was a :nday.')
80
+ end
81
+
82
+ def test_hours
83
+ 0.upto(24) do |hour|
84
+ hour12 = hour
85
+ d = DateTime.civil(2000, 1, 1, hour, 0, 0)
86
+ meridian = d.hour >= 12 ? 'pm' : 'am'
87
+ hour12, meridian = 12, 'am' if d.hour == 0
88
+ hour12 -=12 if hour12 > 12
89
+
90
+ expected = "#{hour12} #{d.hour} #{meridian}"
91
+ assert_equal expected, d.eztime(':hour12 :hour :lmeridian')
92
+ end
93
+ end
94
+
95
+ def test_ordinals
96
+ assert_equal 'st', @ny.ord
97
+ assert_equal 'January 1st', @ny.eztime(':month_name :day:ord')
98
+
99
+ ordinals = %w{
100
+ th st nd rd th th th th th th
101
+ th th th th th th th th th th
102
+ th st nd rd th th th th th th
103
+ th st nd rd th th th th th th
104
+ }
105
+
106
+ 1.upto(31) do |mday|
107
+ d = Date.civil(2006, 1, mday)
108
+ assert_equal ordinals[mday], d.eztime(':ord'), "Failed on " + d.to_s
109
+ end
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eztime
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Chris Scharf
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-05 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ name: jeweler
24
+ type: :development
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 63
31
+ segments:
32
+ - 1
33
+ - 8
34
+ - 4
35
+ version: 1.8.4
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ name: rcov
40
+ type: :development
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ requirement: *id002
51
+ description: Easier date/time manipulation
52
+ email: scharfie@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE.txt
59
+ - README.textile
60
+ files:
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - LICENSE.txt
64
+ - README.textile
65
+ - Rakefile
66
+ - VERSION
67
+ - eztime.gemspec
68
+ - init.rb
69
+ - install.rb
70
+ - lib/eztime.rb
71
+ - test/helper.rb
72
+ - test/test_eztime.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/scharfie/eztime
75
+ licenses:
76
+ - MIT
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.4.2
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Easier date/time manipulation
107
+ test_files: []
108
+