Module: Msip::FormatoFechaHelper

Included in:
Concerns::Models::EtiquetaMunicipio, Concerns::Models::EtiquetaPersona, Localizacion
Defined in:
app/helpers/msip/formato_fecha_helper.rb

Constant Summary collapse

MESES =
[
  "",
  "Enero",
  "Febrero",
  "Marzo",
  "Abril",
  "Mayo",
  "Junio",
  "Julio",
  "Agosto",
  "Septiembre",
  "Octubre",
  "Noviembre",
  "Diciembre",
]
ABMESES =
[
  "",
  "Ene",
  "Feb",
  "Mar",
  "Abr",
  "May",
  "Jun",
  "Jul",
  "Ago",
  "Sep",
  "Oct",
  "Nov",
  "Dic",
]

Class Method Summary collapse

Class Method Details

.dif_meses_dias(fechaini, fechafin) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/helpers/msip/formato_fecha_helper.rb', line 263

def dif_meses_dias(fechaini, fechafin)
  m = 0
  d = 0
  if fechaini && fechafin && fechaini < fechafin
    if fechafin.month < fechaini.month
      m = (fechafin.year - fechaini.year - 1) * 12
      m += 12 - fechaini.month + fechafin.month
    else
      m = (fechafin.year - fechaini.year) * 12
      m += fechafin.month - fechaini.month
    end
    if fechafin.day < fechaini.day
      m -= 1
      d = fechaini.end_of_month.day - fechaini.day + fechafin.day
    else
      d = fechafin.day - fechaini.day
    end
    # Hasta aquí, era preciso, el siguiente tiene en cuenta lo típico
    if (d + 1) == fechafin.end_of_month.day
      m += 1
      d = 0
    end
    if m == 0
      return I18n.t(:dia, count: d)
    elsif d == 0
      return I18n.t(:mes, count: m)
    end

    I18n.t(:mes, count: m) + " y " +
      I18n.t(:dia, count: d)
  else
    ""
  end
end

.fecha_estandar_local(f) ⇒ Object

Convierte una fecha de formato estándar a formato local



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/helpers/msip/formato_fecha_helper.rb', line 144

def fecha_estandar_local(f)
  if !f || (f.class != String && f.class != Date &&
      f.class != ActiveSupport::TimeWithZone) ||
      (f.class == String && f == "")
    return nil
  end

  if f.class == String
    fr = Date.strptime(f, "%Y-%m-%d")
  elsif f.class == Date
    fr = f
  elsif f.class == ActiveSupport::TimeWithZone
    fr = f.to_date
  end
  nf = case Rails.application.config.x.formato_fecha
  when "dd/M/yyyy"
    I18n.l(fr, format: "%d/%b/%Y")
  when "dd-M-yyyy"
    I18n.l(fr, format: "%d-%b-%Y")
  when "dd-mm-yyyy"
    fr.strftime("%d-%m-%Y")
  when "dd/mm/yyyy"
    fr.strftime("%d/%m/%Y")
  else
    fr.strftime("%Y-%m-%d")
  end
  nf
end

.fecha_local_colombia_estandar(f, menserror = nil) ⇒ Object

Este ayudador emplea Rails.application.config.x.formato_fecha al que llama formato local.

Por el momento soporta bien: dd-M-yyyy, dd/M/yyyy, dd-mm-yyyy, dd/mm/yyyy y yyyy-mm-ddd

El formato estándar es el usado por PostgreSQL yyyy-mm-dd



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
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
# File 'app/helpers/msip/formato_fecha_helper.rb', line 44

def fecha_local_colombia_estandar(f, menserror = nil)
  # Date.strptime(f, '%d-%M-%Y') no ha funcionado,
  # %b debe ser en ingles
  # rails-i18n I18n.localize con %b produce mes en minuscula
  unless f
    return nil
  end
  if f == ""
    return ""
  end

  nf = nil
  pf = f.split("/")
  if pf.count < 3
    pf = f.split("-")
  end
  if pf.count < 3
    if menserror
      menserror << "  Formato de fecha en locale de colombia desconocido: #{f}"
    else
      Rails.logger.debug { "Formato de fecha en locale de colombia desconocido: #{f}" }
    end
    return nil
    # nf = Date.strptime(f, "%d-%M-%Y").strftime("%Y-%m-%d")
  else
    return nil unless pf[1]

    m = case pf[1].downcase
    when "ene", "ene.", "jan", "jan.", "1", "01"
      1
    when "feb", "feb.", "2", "02"
      2
    when "mar", "mar.", "3", "03"
      3
    when "abr", "abr.", "apr", "apr.", "4", "04"
      4
    when "may", "may.", "5", "05"
      5
    when "jun", "jun.", "6", "06"
      6
    when "jul", "jul.", "7", "07"
      7
    when "ago", "ago.", "aug", "aug.", "8", "08"
      8
    when "sep", "sep.", "9", "09"
      9
    when "oct", "oct.", "10"
      10
    when "nov", "nov.", "11"
      11
    when "dic", "dic.", "dec", "dec.", "12"
      12
    else
      if menserror
        menserror << "  Formato de fecha en locale de Colombia con mes desconocido suponiendo 12."
      end
      12
    end
    a = pf[2].to_i
    if a < 100
      a += 2000
    end
    begin
      nf = Date.new(a, m, pf[0].to_i).strftime("%Y-%m-%d")
    rescue
      if menserror
        menserror << "  Formato de fecha en locale de colombia desconocido: #{f}"
      else
        Rails.logger.debug { "Formato de fecha en locale de colombia desconocido: #{f}" }
      end
    end
  end
  nf
end

.fecha_local_estandar(f) ⇒ Object

Convierte una fecha de formato local a formato estándar



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/msip/formato_fecha_helper.rb', line 121

def fecha_local_estandar(f)
  unless f
    return nil
  end
  if f == ""
    return ""
  end

  nf = case Rails.application.config.x.formato_fecha
  when "dd/M/yyyy", "dd-M-yyyy"
    fecha_local_colombia_estandar(f)
  when "dd-mm-yyyy"
    Date.strptime(f, "%d-%m-%Y").strftime("%Y-%m-%d")
  when "dd/mm/yyyy"
    Date.strptime(f, "%d/%m/%Y").strftime("%Y-%m-%d")
  else
    Date.strptime(f, "%Y-%m-%d").strftime("%Y-%m-%d")
  end
  nf
end

.fin_semestre(f) ⇒ Object



218
219
220
221
222
223
224
# File 'app/helpers/msip/formato_fecha_helper.rb', line 218

def fin_semestre(f)
  if f.month <= 6
    Date.new(f.year, 6, 30)
  else
    Date.new(f.year, 12, 31)
  end
end

.fin_semestre_antObject

Retorna fecha final del semestre anterior



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/helpers/msip/formato_fecha_helper.rb', line 248

def fin_semestre_ant
  hoy = Time.zone.today
  anio = hoy.year
  if hoy.mon >= 7 && hoy.mon < 12
    fin = anio.to_s + "-" + "06-30"
  elsif hoy.mon == 12
    fin = anio.to_s + "-" + "12-31"
  else
    anio -= 1
    fin = anio.to_s + "-" + "12-31"
  end
  fin
end

.inicio_semestre(f) ⇒ Object



209
210
211
212
213
214
215
# File 'app/helpers/msip/formato_fecha_helper.rb', line 209

def inicio_semestre(f)
  if f.month <= 6
    Date.new(f.year, 1, 1)
  else
    Date.new(f.year, 7, 1)
  end
end

.inicio_semestre_antObject

Retorna fecha inicial del semestre anterior



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/helpers/msip/formato_fecha_helper.rb', line 230

def inicio_semestre_ant
  hoy = Time.zone.today
  anio = hoy.year
  if hoy.mon >= 7 && hoy.mon < 12
    ini = anio.to_s + "-" + "01-01"
  elsif hoy.mon == 12
    ini = anio.to_s + "-" + "07-01"
  else
    anio -= 1
    ini = anio.to_s + "-" + "07-01"
  end
  ini
end

.reconoce_adivinando_locale(f, menserror = nil) ⇒ Object

Adivina locale de fecha y retorna Date



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/helpers/msip/formato_fecha_helper.rb', line 175

def reconoce_adivinando_locale(f, menserror = nil)
  if !f || (f.class != String && f.class != Date) ||
      (f.class == String && f == "")
    return nil
  end
  if f.class == Date
    return f
  end

  nf = f # nf se espera yyyy-mm-dd
  if f.include?("/")
    # 'dd/M/yyyy'
    nf = fecha_local_colombia_estandar(f, menserror)
  elsif f.include?("-")
    p = f.split("-")
    if p[0].to_i >= 1 && p[0].to_i <= 31 && p[2].to_i >= 0
      nf = fecha_local_colombia_estandar(f, menserror)
    end
  end
  begin
    r = Date.strptime(nf, "%Y-%m-%d")
  rescue
    r = nil
    if menserror
      menserror << "  Formato de fecha desconocido: #{f}"
    else
      Rails.logger.debug { "Formato de fecha desconocido: #{f}" }
    end
  end

  r
end