Class: LesliDate::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lesli_date/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(datetime = Time.current, format = "%Y-%m-%d %H:%M:%S") ⇒ Formatter

Returns a new instance of Formatter.



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
88
89
90
91
92
# File 'lib/lesli_date/formatter.rb', line 38

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 => config[:formats][:date] || "%d.%m.%Y",
            :time => "%H:%M",
            :date_time => config[:formats][:date_time] || "%d.%m.%Y %H:%M",
            :date_words => "%B %d, %Y",             # date in words including day's name
            :date_words_day => "%A, %B %d, %Y",     # date in words including day's name
            :date_time_words => "%B %d, %Y, %H:%M", # 24 hours datetime in words format (default)
            :date_time_words_pm => "%B %d, %Y, %I:%M %p",   # 12 hours datetime in words format
            :date_time_words_day => "%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

#db_column(column, table = "") ⇒ Object

return query string to get a datetime column from database



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lesli_date/formatter.rb', line 125

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lesli_date/formatter.rb', line 112

def db_timestamps 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

#getObject



147
148
149
# File 'lib/lesli_date/formatter.rb', line 147

def get
    @datetime
end

#to_sObject

convert a datetime object to string representation using defined format



143
144
145
# File 'lib/lesli_date/formatter.rb', line 143

def to_s
    @datetime.strftime(@format)
end