Class: Date2
- Inherits:
-
Object
- Object
- Date2
- Defined in:
- app/lib/date2.rb
Overview
Lesli
Copyright © 2023, Lesli Technologies, S. A.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see www.gnu.org/licenses/.
Lesli · Ruby on Rails SaaS Development Framework.
Made with ♥ by www.lesli.tech Building a better future, one line of code at a time.
// · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ // ·
Instance Method Summary collapse
-
#date ⇒ Object
set date format and return Date2 instance.
-
#date_time ⇒ Object
set date_time format and return Date2 instance.
-
#date_time_words ⇒ Object
set date_time_words format and return Date2 instance.
-
#date_words ⇒ Object
set date_words format and return Date2 instance.
-
#db_column(column, table = "") ⇒ Object
return query string to get a datetime column from database.
-
#db_timestamps(table = "") ⇒ Object
return query string to get timestamps columns from database.
- #get ⇒ Object
-
#initialize(datetime = Time.current, format = "%Y-%m-%d %H:%M:%S") ⇒ Date2
constructor
A new instance of Date2.
-
#time ⇒ Object
set time format and return Date2 instance.
-
#to_s ⇒ Object
convert a datetime object to string representation using defined format.
Constructor Details
#initialize(datetime = Time.current, format = "%Y-%m-%d %H:%M:%S") ⇒ Date2
Returns a new instance of Date2.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/lib/date2.rb', line 35 def initialize(datetime = Time.current, format = "%Y-%m-%d %H:%M:%S") # NOTE: user should be able to change this through settings table # get initial datetime configuration config = Lesli.config.datetime # NOTE: Do not modify settings here, # if you need a different date format you should change it in the config file # Check the docs for more information: /development/docs/rails-lib-time # configuration example # @settings = { # :time_zone => config[:time_zone], # :start_week_on => config[:start_week_on], # :format => { # :date => "%d.%m.%Y", # :time => "%H:%M %p", # 12 hours datetime format # :time => "%H:%M", # 24 hours datetime format (default) # :date_time => "%d.%m.%Y %I:%M %p", # 12 hours datetime format # :date_time => "%d.%m.%Y %H:%M", # 24 hours datetime format (default) # :date_words => "%A, %B %d, %Y", # :date_time_words => "%A, %B %d, %Y, %I:%M %p", # 12 hours datetime in words format # :date_time_words => "%A, %B %d, %Y, %H:%M" # 24 hours datetime in words format (default) # } # } @settings = { :time_zone => config[:time_zone], :start_week_on => config[:start_week_on], :format => { :date => "%d.%m.%Y", :time => "%H:%M", :date_time => config[:formats][:date_time], :date_words => "%A, %B %d, %Y", :date_time_words => "%A, %B %d, %Y, %I:%M %p", # 12 hours datetime in words format :date_time_words => "%A, %B %d, %Y, %H:%M" # 24 hours datetime in words format (default) } } # default date format @format = set_format(:date) # get a valid timezone @zone = ActiveSupport::TimeZone.new(@settings[:time_zone]) # get datetime object from user params @datetime = parse_initial_datetime(datetime, format).in_time_zone(@zone) end |
Instance Method Details
#date ⇒ Object
set date format and return Date2 instance
90 91 92 93 |
# File 'app/lib/date2.rb', line 90 def date set_format(:date) self end |
#date_time ⇒ Object
set date_time format and return Date2 instance
102 103 104 105 |
# File 'app/lib/date2.rb', line 102 def date_time set_format(:date_time) self end |
#date_time_words ⇒ Object
set date_time_words format and return Date2 instance
114 115 116 117 |
# File 'app/lib/date2.rb', line 114 def date_time_words set_format(:date_time_words) self end |
#date_words ⇒ Object
set date_words format and return Date2 instance
108 109 110 111 |
# File 'app/lib/date2.rb', line 108 def date_words set_format(:date_words) self end |
#db_column(column, table = "") ⇒ Object
return query string to get a datetime column from database
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/lib/date2.rb', line 133 def db_column column, table="" # avoid ambiguous columns table = table.concat(".") if table != "" # get right format for dates format = self.db_format # compatibility for SQLite if ActiveRecord::Base.connection.adapter_name == "SQLite" return "strftime('#{format}', #{table}#{column}) as #{column}_string" end "TO_CHAR(#{table}#{column} at time zone 'utc' at time zone '#{@settings[:time_zone]}', '#{format}') as #{column}_string" end |
#db_timestamps(table = "") ⇒ Object
return query string to get timestamps columns from database
120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/lib/date2.rb', line 120 def table="" # avoid ambiguous columns table = table.concat(".") if table != "" # get right format for dates format = self.db_format "TO_CHAR(#{table}created_at at time zone 'utc' at time zone '#{@settings[:time_zone]}', '#{format}') as created_at_date, TO_CHAR(#{table}updated_at at time zone 'utc' at time zone '#{@settings[:time_zone]}', '#{format}') as updated_at_date" end |
#get ⇒ Object
155 156 157 |
# File 'app/lib/date2.rb', line 155 def get @datetime end |
#time ⇒ Object
set time format and return Date2 instance
96 97 98 99 |
# File 'app/lib/date2.rb', line 96 def time set_format(:time) self end |
#to_s ⇒ Object
convert a datetime object to string representation using defined format
151 152 153 |
# File 'app/lib/date2.rb', line 151 def to_s @datetime.strftime(@format) end |