time_date_helpers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/time_date_helpers.rb +6 -0
- data/lib/time_date_helpers/date_helpers.rb +37 -0
- data/lib/time_date_helpers/time_helpers.rb +41 -0
- data/lib/time_date_helpers/version.rb +3 -0
- data/time_date_helpers.gemspec +24 -0
- metadata +78 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module TimeDateHelpers
|
2
|
+
module DateHelpers
|
3
|
+
# convert a date to human format of mm/dd/yyyy
|
4
|
+
def humanize_date(date, opt={})
|
5
|
+
# Set the default options
|
6
|
+
options = {:style => :calendar}
|
7
|
+
# Merge whatever options the user has selected with the defaults
|
8
|
+
options.merge!(opt)
|
9
|
+
# Make sure what is passed is legit
|
10
|
+
return nil if date.nil?
|
11
|
+
return nil unless date.class == Date || date.class == Time || date.class == DateTime
|
12
|
+
if options[:style] == :calendar
|
13
|
+
date.strftime("%m/%d/%Y")
|
14
|
+
elsif options[:style] == :full
|
15
|
+
date.day < 10 ? date.strftime("%B%e, %Y") : date.strftime("%B %e, %Y")
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# convert a string to a date (using Chronic, of course)
|
22
|
+
def convert_to_date(string)
|
23
|
+
return nil if (string.nil? || string.class != String)
|
24
|
+
tmp = Chronic.parse(string)
|
25
|
+
tmp ? tmp.to_date : nil
|
26
|
+
end
|
27
|
+
|
28
|
+
# convert a string to a datetime (Chronic's default)
|
29
|
+
def convert_to_datetime(string)
|
30
|
+
return nil if (string.nil? || string.class != String)
|
31
|
+
Chronic.parse(string)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Add methods to Object so can be called directly in main
|
37
|
+
Object.send :include, TimeDateHelpers::DateHelpers
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module TimeDateHelpers
|
2
|
+
module TimeHelpers
|
3
|
+
def round_minutes(time, opt={})
|
4
|
+
# Set the default options
|
5
|
+
options = {:direction => :up, :increment => 15}
|
6
|
+
# Merge whatever options the user has selected with the defaults
|
7
|
+
options.merge!(opt)
|
8
|
+
|
9
|
+
# make sure that increment is a Fixnum between 1 and 59
|
10
|
+
return nil unless options[:increment].class == Fixnum
|
11
|
+
return nil if (options[:increment] > 59 || options[:increment] < 1)
|
12
|
+
|
13
|
+
# Set up some local variables used in calculations
|
14
|
+
new_min, hr_adj = time.min, 0
|
15
|
+
total_segments = (60.0/options[:increment]).ceil # Need to round up
|
16
|
+
closest_segment_down = (new_min/options[:increment])
|
17
|
+
closest_segment_up = (new_min/options[:increment]) + 1
|
18
|
+
|
19
|
+
# Now it is time to actually adjust the minutes (and perhaps hour)
|
20
|
+
if (new_min - options[:increment]*closest_segment_down) == 0
|
21
|
+
new_min # we are on an increment, so just return the minutes
|
22
|
+
elsif options[:direction] == :up
|
23
|
+
# if rounding up, need to check if up against the end-of-the-hour limit
|
24
|
+
if closest_segment_up == total_segments
|
25
|
+
new_min = 00; hr_adj=1 # near end of hour, so move up to next hour
|
26
|
+
else
|
27
|
+
new_min = options[:increment]*(closest_segment_up)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
# we are rounding down, which is very easy
|
31
|
+
new_min = options[:increment]*(closest_segment_down)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Finally, return the adjusted time
|
35
|
+
Time.new(time.year, time.month, time.day, (time.hour+hr_adj), new_min, 0)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Add to the Object model so can call directly in main
|
41
|
+
Object.send :include, TimeDateHelpers::TimeHelpers
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "time_date_helpers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "time_date_helpers"
|
7
|
+
s.version = TimeDateHelpers::VERSION
|
8
|
+
s.authors = ["Klingon Code Warrior"]
|
9
|
+
s.email = ["profh@cmu.edu"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A series of helpers to deal with time and date issues in Rails.}
|
12
|
+
s.description = %q{The initial version has only a few date and time helpers and was created primarily for teaching purposes (how to create gems), but does have some value. I will try to get back to this gem and add more methods as time allows.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "time_date_helpers"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "chronic"
|
23
|
+
s.add_runtime_dependency "chronic"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_date_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Klingon Code Warrior
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: chronic
|
16
|
+
requirement: &2153346800 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153346800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: chronic
|
27
|
+
requirement: &2153362400 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2153362400
|
36
|
+
description: The initial version has only a few date and time helpers and was created
|
37
|
+
primarily for teaching purposes (how to create gems), but does have some value.
|
38
|
+
I will try to get back to this gem and add more methods as time allows.
|
39
|
+
email:
|
40
|
+
- profh@cmu.edu
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- Rakefile
|
48
|
+
- lib/time_date_helpers.rb
|
49
|
+
- lib/time_date_helpers/date_helpers.rb
|
50
|
+
- lib/time_date_helpers/time_helpers.rb
|
51
|
+
- lib/time_date_helpers/version.rb
|
52
|
+
- time_date_helpers.gemspec
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project: time_date_helpers
|
73
|
+
rubygems_version: 1.8.15
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: A series of helpers to deal with time and date issues in Rails.
|
77
|
+
test_files: []
|
78
|
+
has_rdoc:
|