All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 local strformat = string.format
2 local floor = math.floor
3 local function idiv ( n , d )
8 abday = { "Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" } ;
9 day = { "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" } ;
10 abmon = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" } ;
11 mon = { "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December" } ;
12 am_pm = { "AM" , "PM" } ;
15 --- ISO-8601 week logic
16 -- ISO 8601 weekday as number with Monday as 1 (1-7)
17 local function iso_8601_weekday ( wday )
24 local iso_8601_week do
25 -- Years that have 53 weeks according to ISO-8601
26 local long_years = { }
28 4, 9, 15, 20, 26, 32, 37, 43, 48, 54, 60, 65, 71, 76, 82,
29 88, 93, 99, 105, 111, 116, 122, 128, 133, 139, 144, 150, 156, 161, 167,
30 172, 178, 184, 189, 195, 201, 207, 212, 218, 224, 229, 235, 240, 246, 252,
31 257, 263, 268, 274, 280, 285, 291, 296, 303, 308, 314, 320, 325, 331, 336,
32 342, 348, 353, 359, 364, 370, 376, 381, 387, 392, 398
34 long_years [ v ] = true
36 local function is_long_year ( year )
37 return long_years [ year % 400 ]
39 function iso_8601_week ( self )
40 local wday = iso_8601_weekday ( self.wday )
41 local n = self.yday - wday
42 local year = self.year
45 if is_long_year ( year ) then
46 return year , 53 , wday
48 return year , 52 , wday
50 elseif n >= 361 and not is_long_year ( year ) then
51 return year + 1 , 1 , wday
53 return year , idiv ( n + 10 , 7 ) , wday
60 function t:a ( locale )
61 return "%s" , locale.abday [ self.wday ]
63 function t:A ( locale )
64 return "%s" , locale.day [ self.wday ]
66 function t:b ( locale )
67 return "%s" , locale.abmon [ self.month ]
69 function t:B ( locale )
70 return "%s" , locale.mon [ self.month ]
72 function t:c ( locale )
73 return "%.3s %.3s%3d %.2d:%.2d:%.2d %d" ,
74 locale.abday [ self.wday ] , locale.abmon [ self.month ] ,
75 self.day , self.hour , self.min , self.sec , self.year
79 return "%02d" , idiv ( self.year , 100 )
82 return "%02d" , self.day
84 -- Short MM/DD/YY date, equivalent to %m/%d/%y
86 return "%02d/%02d/%02d" , self.month , self.day , self.year % 100
89 return "%2d" , self.day
91 -- Short YYYY-MM-DD date, equivalent to %Y-%m-%d
93 return "%d-%02d-%02d" , self.year , self.month , self.day
95 -- Week-based year, last two digits (00-99)
97 return "%02d" , iso_8601_week ( self ) % 100
101 return "%d" , iso_8601_week ( self )
105 return "%02d" , self.hour
108 return "%02d" , (self.hour-1) % 12 + 1
111 return "%03d" , self.yday
114 return "%02d" , self.month
117 return "%02d" , self.min
119 -- New-line character ('\n')
123 function t:p ( locale )
124 return self.hour < 12 and locale.am_pm[1] or locale.am_pm[2]
126 -- TODO: should respect locale
127 function t:r ( locale )
128 return "%02d:%02d:%02d %s" ,
129 (self.hour-1) % 12 + 1 , self.min , self.sec ,
130 self.hour < 12 and locale.am_pm[1] or locale.am_pm[2]
132 -- 24-hour HH:MM time, equivalent to %H:%M
134 return "%02d:%02d" , self.hour , self.min
137 return "%d" , self:timestamp ( )
140 return "%02d" , self.sec
142 -- Horizontal-tab character ('\t')
146 -- ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
148 return "%02d:%02d:%02d" , self.hour , self.min , self.sec
151 return "%d" , iso_8601_weekday ( self.wday )
153 -- Week number with the first Sunday as the first day of week one (00-53)
155 return "%02d" , idiv ( self.yday - self.wday + 7 , 7 )
157 -- ISO 8601 week number (00-53)
159 return "%02d" , select ( 2 , iso_8601_week ( self ) )
161 -- Weekday as a decimal number with Sunday as 0 (0-6)
163 return "%d" , self.wday - 1
165 -- Week number with the first Monday as the first day of week one (00-53)
167 return "%02d" , idiv ( self.yday - iso_8601_weekday ( self.wday ) + 7 , 7 )
169 -- TODO make t.x and t.X respect locale
173 return "%02d" , self.year % 100
176 return "%d" , self.year
185 -- A literal '%' character.
186 t["%"] = function ( self )
190 local function strftime ( format_string , timetable )
191 return ( string.gsub ( format_string , "%%([EO]?)(.)" , function ( locale_modifier , specifier )
192 local func = t [ specifier ]
194 return strformat ( func ( timetable , c_locale ) )
196 error ( "invalid conversation specifier '%"..locale_modifier..specifier.."'" , 3 )
201 local function asctime ( timetable )
202 -- Equivalent to the format string "%c\n"
203 return strformat ( t.c ( timetable , c_locale ) ) .. "\n"
207 strftime = strftime ;