Class: Zwite::DateFormat::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/zwite/core/dateformat.rb

Constant Summary collapse

WEEKDAYS =
{
  0 => "Sunday",
  1 => "Monday",
  2 => "Tuesday",
  3 => "Wednesday",
  4 => "Thursday",
  5 => "Friday",
  6 => "Saturday"
}
WEEKDAYS_ABBR =
{
  0 => "Sun",
  1 => "Mon",
  2 => "Tue",
  3 => "Wed",
  4 => "Thu",
  5 => "Fri",
  6 => "Sat"
}
WEEKDAYS_REV =
{
  "sunday" => 0,
  "monday" => 1,
  "tuesday" => 2,
  "wednesday" => 3,
  "thursday" => 4,
  "friday" => 5,
  "saturday" => 6
}
MONTHS =
{
  1 => "January",
  2 => "February",
  3 => "March",
  4 => "April",
  5 => "May",
  6 => "June",
  7 => "July",
  8 => "August",
  9 => "September",
  10 => "October",
  11 => "November",
  12 => "December"
}
MONTHS_3 =
{
  1 => "Jan",
  2 => "Feb",
  3 => "Mar",
  4 => "Apr",
  5 => "May",
  6 => "Jun",
  7 => "Jul",
  8 => "Aug",
  9 => "Sep",
  10 => "Oct",
  11 => "Nov",
  12 => "Dec"
}
MONTHS_3_REV =
{
  "Jan" => 1,
  "Feb" => 2,
  "Mar" => 3,
  "Apr" => 4,
  "May" => 5,
  "Jun" => 6,
  "Jul" => 7,
  "Aug" => 8,
  "Sep" => 9,
  "Oct" => 10,
  "Nov" => 11,
  "Dec" => 12
}
MONTHS_AP =
{
  1 => "Jan.",
  2 => "Feb.",
  3 => "March",
  4 => "April",
  5 => "May",
  6 => "June",
  7 => "July",
  8 => "Aug.",
  9 => "Sept.",
  10 => "Oct.",
  11 => "Nov.",
  12 => "Dec."
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datetime) ⇒ Formatter

Returns a new instance of Formatter.



93
94
95
# File 'lib/zwite/core/dateformat.rb', line 93

def initialize(datetime)
  self.data = datetime
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



91
92
93
# File 'lib/zwite/core/dateformat.rb', line 91

def data
  @data
end

Instance Method Details

#aObject

a.m or p.m



110
111
112
113
114
115
# File 'lib/zwite/core/dateformat.rb', line 110

def a
  if self.data.hour > 11
    return "p.m."
  end
  return "a.m"
end

#AObject

‘AM’ or ‘PM’.



118
119
120
121
122
123
# File 'lib/zwite/core/dateformat.rb', line 118

def A
  if self.data.hour > 11
    return "PM"
  end
  return "AM"
end

#bObject

Month, textual, 3 letters, lowercase.



126
127
128
# File 'lib/zwite/core/dateformat.rb', line 126

def b
  return MONTHS_3[self.data.month].downcase
end

#BObject

Unimplemented



131
132
133
# File 'lib/zwite/core/dateformat.rb', line 131

def B
  return "[B]Unimplemented"
end

#cObject

ISO 8601 format. (Note: unlike others formatters, such as “Z”, “O” or “r”, the “c” formatter will not add timezone offset if value is a naive datetime (see datetime.tzinfo).



136
137
138
# File 'lib/zwite/core/dateformat.rb', line 136

def c
  return self.data.iso8601
end

#DObject

Day of the week, textual, 3 letters.



146
147
148
# File 'lib/zwite/core/dateformat.rb', line 146

def D
  return WEEKDAYS_ABBR[self.data.weekday]
end

#dObject

Day of the month, 2 digits with leading zeros.



141
142
143
# File 'lib/zwite/core/dateformat.rb', line 141

def d
  return "%02d" % self.data.day
end

#eObject

Unimplemented



151
152
153
# File 'lib/zwite/core/dateformat.rb', line 151

def e
  return "[E]Unimplemented"
end

#EObject

Month, locale specific alternative representation usually used for long date representation



156
157
158
# File 'lib/zwite/core/dateformat.rb', line 156

def E
  return MONTHS_ALT[self.data.month]
end

#fObject

Time, in 12-hour hours and minutes, with minutes left off if they’re zero. Proprietary extension.



161
162
163
164
165
166
# File 'lib/zwite/core/dateformat.rb', line 161

def f
  if self.data.minute == 0
    return self.g
  end
  return "%s:%s" % self.g, self.i
end

#FObject

Month, textual, long.



169
170
171
# File 'lib/zwite/core/dateformat.rb', line 169

def F
  return MONTHS[self.data.month]
end

#format(str) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zwite/core/dateformat.rb', line 97

def format(str)
  pieces = []
  str.each_char do |c|
    if Formatter.method_defined?(c)
      pieces << self.send(c)
    elsif c
      pieces << c
    end
  end
  return pieces.join("")
end

#GObject

Hour 24 hour format - without leading zeros



185
186
187
# File 'lib/zwite/core/dateformat.rb', line 185

def G
  return self.data.hour
end

#gObject

Hour 12 hour format - without leading zeros



174
175
176
177
178
179
180
181
182
# File 'lib/zwite/core/dateformat.rb', line 174

def g
  if self.data.hour == 0
    return 12
  end
  if self.data.hour > 12
    return self.data.hour - 12
  end
  return self.data.hour
end

#hObject

Hour 12 hour format



190
191
192
# File 'lib/zwite/core/dateformat.rb', line 190

def h
  return "%02d" % self.g
end

#HObject

Hour 24 hour format



195
196
197
# File 'lib/zwite/core/dateformat.rb', line 195

def H
  return "%02d" % self.G
end

#iObject

Minutes



200
201
202
# File 'lib/zwite/core/dateformat.rb', line 200

def i
  return "%02d" % self.data.minute
end

#IObject

Unimplemented



205
206
207
# File 'lib/zwite/core/dateformat.rb', line 205

def I
  return "[I]Unimplemented"
end

#jObject

Day of the month without leading zeros.



210
211
212
# File 'lib/zwite/core/dateformat.rb', line 210

def j
  return self.data.day
end

#lObject

Day of the week, textual, long.



215
216
217
# File 'lib/zwite/core/dateformat.rb', line 215

def l
  return WEEKDAYS[self.data.wday]
end

#LObject

Boolean for whether it’s a leap year.



220
221
222
# File 'lib/zwite/core/dateformat.rb', line 220

def L
  return self.data.leap?
end

#mObject

Month, 2 digits with leading zeros.



225
226
227
# File 'lib/zwite/core/dateformat.rb', line 225

def m
  return "%02d" % self.data.month
end

#MObject

Month, textual, 3 letters.



230
231
232
# File 'lib/zwite/core/dateformat.rb', line 230

def M
  return MONTHS_3[self.data.month]
end

#nObject

Month without leading zero



235
236
237
# File 'lib/zwite/core/dateformat.rb', line 235

def n
  return self.data.month
end

#NObject

Month abbreviation in Associated Press style. Proprietary extension.



240
241
242
# File 'lib/zwite/core/dateformat.rb', line 240

def N
  return MONTHS_AP[self.data.month]
end

#oObject

ISO-8601 week-numbering year, corresponding to the ISO-8601 week number (W)



245
246
247
# File 'lib/zwite/core/dateformat.rb', line 245

def o
  return self.data.strftime("%G")
end

#OObject

Difference to Greenwich time in hours.



250
251
252
# File 'lib/zwite/core/dateformat.rb', line 250

def O
  return self.data.strftime("%z")
end

#PObject

Time, in 12-hour hours, minutes and ‘a.m.’/‘p.m.’, with minutes left off if they’re zero and the special-case strings ‘midnight’ and ‘noon’ if appropriate. Proprietary extension.



255
256
257
258
259
260
261
262
# File 'lib/zwite/core/dateformat.rb', line 255

def P
  if self.data.minute == 0 && self.data.hour == 0
    return "midnight"
  elsif self.data.minute == 0 and self.data.hour == 12
    return "noon"
  end
  return "%s %s" % self.f, self.a
end

#rObject

RFC 2822 formatted date.



265
266
267
# File 'lib/zwite/core/dateformat.rb', line 265

def r
  return self.data.rfc2822
end

#sObject

Seconds, 2 digits with leading zeros.



270
271
272
# File 'lib/zwite/core/dateformat.rb', line 270

def s
  return "%02d" % self.data.second
end

#SObject

English ordinal suffix for day of the month, 2 characters.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/zwite/core/dateformat.rb', line 275

def S
  if (11..13).include?(self.data.day)
    return "#{self}th"
  end
  last = self.data.day % 10
  if last == 1
    return "st"
  elsif last == 2
    return "nd"
  elsif last == 3
    return "rd"
  end
  return "th"
end

#tObject

Number of days in the given month



291
292
293
# File 'lib/zwite/core/dateformat.rb', line 291

def t
  return (Date.new(self.data.year, 12, 31) << (12 - self.data.month)).day
end

#TObject

Timezone of this machine



296
297
298
# File 'lib/zwite/core/dateformat.rb', line 296

def T
  return DateTime.now.strftime("%Z")
end

#uObject

Microseconds



301
302
303
# File 'lib/zwite/core/dateformat.rb', line 301

def u
  return self.data.strftime("%6N").to_i
end

#UObject

Seconds since the Unix Epoch (January 1 1970 00:00:00 UTC).



306
307
308
# File 'lib/zwite/core/dateformat.rb', line 306

def U
  return self.data.strftime("%s").to_i
end

#WObject

ISO-8601 week number of year, weeks starting on Monday



311
312
313
# File 'lib/zwite/core/dateformat.rb', line 311

def W
  return self.data.strftime("%-V").to_i
end

#yObject

Year 2 digits



316
317
318
# File 'lib/zwite/core/dateformat.rb', line 316

def y
  return self.data.year.to_s[2..3]
end

#YObject

Year 4 digits



321
322
323
# File 'lib/zwite/core/dateformat.rb', line 321

def Y
  return self.data.year
end

#zObject

Day of the year



326
327
328
# File 'lib/zwite/core/dateformat.rb', line 326

def z
  return self.data.yday - 1
end

#ZObject

Time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.



331
332
333
# File 'lib/zwite/core/dateformat.rb', line 331

def Z
  return self.data.offset.numerator * 60 *60
end