Class: Time

Inherits:
Object show all
Defined in:
lib/nobiru/extensions/time_extension.rb

Instance Method Summary collapse

Instance Method Details

#format(string = "yyyy-m-d h:n z") ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nobiru/extensions/time_extension.rb', line 3

def format(string="yyyy-m-d h:n z")
  delimiters = string.scan /\W+/
  formatters = string.scan /[a-z0-9_]+/i

  format_units = {
    ## Date keys
    "d"                       =>  "d",
    "day"                     =>  "d",
    "day_zero"                =>  "d",
    "dd"                      =>  "-d",
    "Day"                     =>  "-d",
    "day_unpadded"            =>  "-d",
    "ddd"                     =>  "_d",
    "DAY"                     =>  "_d",
    "day_blank"               =>  "_d",
    "dddd"                    =>  "j",
    "day_of_the_year"         =>  "j",
    "m"                       =>  "m",
    "month"                   =>  "m",
    "month_zero"              =>  "m",
    "mm"                      =>  "-m",
    "Month"                   =>  "-m",
    "month_unpadded"          =>  "-m",
    "mmm"                     =>  "_m",
    "MONTH"                   =>  "_m",
    "month_blank"             =>  "_m",
    "mmmm"                    =>  "B",
    "month_name"              =>  "B",
    "mmmmm"                   =>  "b",
    "month_name_abbr"         =>  "b",
    "w"                       =>  "u",
    "weekday"                 =>  "u",
    "ww"                      =>  "w",
    "weekday_offset"          =>  "w",
    "www"                     =>  "A",
    "weekday_name"            =>  "A",
    "wwww"                    =>  "a",
    "weekday_name_abbr"       =>  "a",
    "wwwww"                   =>  "W",
    "week"                    =>  "W",
    "wwwwww"                  =>  "U",
    "week_offset"             =>  "U",
    "yy"                      =>  "y",
    "yr"                      =>  "y",
    "yyyy"                    =>  "Y",
    "year"                    =>  "Y",

    ## Time keys
    "h"                       =>  "H",
    "hour"                    =>  "H",
    "hour_zero"               =>  "H",
    "hh"                      =>  "k",
    "HOUR"                    =>  "k",
    "hour_blank"              =>  "k",
    "hhh"                     =>  "I",
    "hour_imperical"          =>  "I",
    "hour_imperical_zero"     =>  "I",
    "hhhh"                    =>  "l",
    "HOUR_IMPERICAL"          =>  "l",
    "hour_imperical_blank"    =>  "l",
    "ampm"                    =>  "P",
    "meridian"                =>  "P",
    "AMPM"                    =>  "p",
    "MERIDIAN"                =>  "p",
    "n"                       =>  "M",
    "minute"                  =>  "M",
    "s"                       =>  "S",
    "second"                  =>  "S",
    "z"                       =>  "z",
    "time_zone"               =>  "z",
    "zz"                      =>  ":z",
    "time_zone_offset"        =>  ":z",
    "zzz"                     =>  "::z",
    "time_zone_offset_full"   =>  "::z",
    "zzzz"                    =>  "Z",
    "time_zone_name"          =>  "Z"
  }

  strftime(
    if string.empty? || string.nil?
      "%Y-%m-%d %H:%M %z"
    else
      formatters.map do |f|
        "%#{format_units[f]}#{delimiters.shift || ''}"
      end.join
    end
  )
end

#to_format(key = :datetime_iso_tzn) ⇒ Object


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
118
119
120
121
122
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
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/nobiru/extensions/time_extension.rb', line 92

def to_format(key=:datetime_iso_tzn)
  format_units = {
    ## Date keys
    month:                        "%m",
    month_zero:                   "%m",
    month_unpadded:               "%-m",
    month_blank:                  "%_m",
    month_name:                   "%B",
    month_name_abbr:              "%b",
    weekday:                      "%d",
    weekday_zero:                 "%d",
    weekday_unpadded:             "%-d",
    weekday_blank:                "%_d",
    weekday_name:                 "%A",
    weekday_name_abbr:            "%a",
    yr:                           "%y",
    year:                         "%Y",

    ## Time keys
    hour:                         "%H",
    hour_zero:                    "%H",
    hour_blank:                   "%k",
    hour_imperical:               "%I",
    hour_imperical_zero:          "%I",
    hour_imperical_blank:         "%l",
    ampm:                         "%P",
    meridian:                     "%P",
    minute:                       "%M",
    second:                       "%S",
    time_zone:                    "%z",
    time_zone_offset:             "%:z",
    time_zone_offset_full:        "%::z",
    time_zone_name:               "%Z",

    ## Datetime keys
    date:                         "%B %-d, %Y",
    date_abbr:                    "%b %-d, %Y",
    date_iso:                     "%Y-%m-%d",
    datetime:                     "%B %-d, %Y %H:%M",
    datetime_abbr:                "%b %-d, %Y %H:%M",
    datetime_iso:                 "%Y-%m-%d %H:%M",
    datetime_imperical:           "%B %-d, %Y %I:%M %P",
    datetime_imperical_abbr:      "%b %-d, %Y %I:%M %P",
    datetime_imperical_iso:       "%Y-%m-%d %I:%M %P",
    datetime_tzn:                 "%B %-d, %Y %H:%M %Z",
    datetime_abbr_tzn:            "%b %-d, %Y %H:%M %Z",
    datetime_iso_tzn:             "%Y-%m-%d %H:%M %z",
    datetime_imperical_tzn:       "%B %-d, %Y %I:%M %P %Z",
    datetime_imperical_abbr_tzn:  "%b %-d, %Y %I:%M %P %Z",
    datetime_imperical_iso_tzn:   "%Y-%m-%d %I:%M %P %z",
    day:                          "%B %-d",
    day_abbr:                     "%b %-d",
    day_iso:                      "%m-%d",
    daytime:                      "%B %-d %H:%M",
    daytime_abbr:                 "%b %-d %H:%M",
    daytime_iso:                  "%m-%d %H:%M",
    daytime_imperical:            "%B %-d %I:%M %P",
    daytime_imperical_abbr:       "%b %-d %I:%M %P",
    daytime_imperical_iso:        "%m-%d %I:%M %P",
    time:                         "%H:%M",
    time_zero:                    "%H:%M",
    time_blank:                   "%k:%M",
    time_tz:                      "%H:%M %z",
    time_tzn:                     "%H:%M %Z",
    time_imperical:               "%I:%M %P",
    time_imperical_zero:          "%I:%M %P",
    time_imperical_blank:         "%l:%M %P",
    time_imperical_tz:            "%I:%M %P %z",
    time_imperical_tzn:           "%I:%M %P %Z"
  }

  strftime(format_units[key])
end