Module: DateFormat
- Defined in:
- lib/date_format/base.rb,
lib/date_format/version.rb
Constant Summary collapse
- VERSION =
'0.3.3'.freeze
Class Method Summary collapse
- .change_to(element, format_type) ⇒ Object
- .choose_date_format(format_type) ⇒ Object
- .choose_time_difference_format(time_difference_in_second, days, hours, minutes, seconds, format_type) ⇒ Object
- .day_difference(start_date, end_date) ⇒ Object
-
.time_difference(start_date, end_date, format_type) ⇒ Object
FORMAT => DAY_ONLY, HOUR_ONLY, MINUTE_ONLY, SECOND_ONLY.
Class Method Details
.change_to(element, format_type) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/date_format/base.rb', line 3 def self.change_to(element, format_type) begin unless element.nil? || element == "" element.strftime(choose_date_format(format_type)) else "NA" end rescue element end end |
.choose_date_format(format_type) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/date_format/base.rb', line 15 def self.choose_date_format(format_type) case format_type when "ISO_8601_FORMAT" # => 2014-05-15 "%F" when "GENERAL_DATE" # => 4/3/93 05:34:00 PM "%m/%d/%y %r" when "LONG_DATE" # => Saturday, April 3, 2014 "%A, %B %d, %Y" when "MEDIUM_DATE" # => 3-Apr-2014 "%d-%b-%Y" when "SHORT_DATE" # => 4/3/93 "%D" when "LONG_TIME" # => 5:34:23 PM "%r" when "MEDIUM_TIME" # => 5:34 PM "%H:%M %p" when "SHORT_TIME" # => 17:34 "%H:%M" when "WEEK_OF_YEAR" # => 19 "%W" when "DAY_IN_MONTH" # => 3 "%d" when "JULIAN_DAY" # => 2014 "%G" when "ONLY_HOUR_IN_24HOUR_FORMAT" # => 13 Hour "%H" when "HOURS_IN_24HOUR_FORMAT" # => 13:34 Hour:Minute "%H:%M" when "MINUTE_IN_HOUR" # => 34 Minute "%M" when "LONG_DATE_SHORT" # => Sat, April 3, 2014*** "%a, %B %d, %Y" when "ONLY_DATE" # => April 3, 2014*** "%B %d, %Y" when "ONLY_DATE_SHORT" # => Apr 3, 2014*** "%b %d, %Y" when "ONLY_DATE_SHORTEST" # => Sat, Apr 3, 2014*** "%a, %b %d, %Y" when "ONLY_DATE_DEFAULT" # => 3/4/2014 *** "%d/%m/%Y" when "ONLY_CURRENT_DATE_NUMBER" # => 3 *** "%d" when "ONLY_CURRENT_DATE_ALPHABET" # => Sat *** "%a" when "ONLY_CURRENT_MONTH_NUMBER" # => 4 *** "%m" when "ONLY_CURRENT_MONTH_ALPHABET" # => Apr *** "%b" when "ONLY_CURRENT_YEAR_MONTH" # => 201404 *** "%Y%m" when 'MYSQL_QUERY_FORMAT' "%Y-%m-%d %H:%M:%S.%6N" else "%d.%m.%y" end end |
.choose_time_difference_format(time_difference_in_second, days, hours, minutes, seconds, format_type) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/date_format/base.rb', line 93 def self.choose_time_difference_format(time_difference_in_second, days, hours, minutes, seconds, format_type) case format_type when "DAY_ONLY" "#{days} Days" when "HOUR_ONLY" begin only_hours = (time_difference_in_second/3600).round(2) rescue only_hours = (time_difference_in_second/3600) end "#{only_hours} Hrs" when "MINUTE_ONLY" begin only_minutes = (time_difference_in_second/60).round(2) rescue only_minutes = (time_difference_in_second/60) end "#{only_minutes} Mins" when "SECOND_ONLY" "#{time_difference_in_second} Seconds" else "#{days} Days, #{hours} Hours #{minutes} Minutes #{seconds} Seconds" end end |
.day_difference(start_date, end_date) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/date_format/base.rb', line 123 def self.day_difference(start_date, end_date) formatted_time = "" end_date = Time.now if end_date == 'PRESENT_DAY' time_difference_in_second = (end_date - start_date).to_i rest, seconds = time_difference_in_second.divmod( 60 ) rest, minutes = rest.divmod( 60 ) days, hours = rest.divmod( 24 ) if days > 0 tag = (days < 2) ? "day" : "days" formatted_time += "#{days} #{tag} " end if hours > 0 tag = (hours < 2) ? "hour" : "hours" formatted_time += "#{hours} #{tag} " end if minutes > 0 tag = (minutes < 2) ? "minute" : "minutes" formatted_time += "#{minutes} #{tag} " end if seconds > 0 tag = (seconds < 2) ? "second" : "seconds" formatted_time += "#{seconds} #{tag} " end formatted_time end |
.time_difference(start_date, end_date, format_type) ⇒ Object
FORMAT => DAY_ONLY, HOUR_ONLY, MINUTE_ONLY, SECOND_ONLY
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/date_format/base.rb', line 73 def self.time_difference(start_date, end_date, format_type) begin begin end_date = Time.now if end_date == 'PRESENT_DAY' || end_date == '' time_difference_in_second = (end_date - start_date).to_i rescue time_difference_in_second = (Time.now - start_date).to_i end rest, seconds = time_difference_in_second.divmod( 60 ) rest, minutes = rest.divmod( 60 ) days, hours = rest.divmod( 24 ) choose_time_difference_format(time_difference_in_second, days, hours, minutes, seconds, format_type) rescue "DATE - Wrong Format" end end |