7017
|
1 ## Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006, 2007 Paul Kienzle |
5687
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5687
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5687
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{str} =} datestr (@var{date}, [@var{f}, [@var{p}]]) |
|
21 ## Format the given date/time according to the format @code{f} and return |
|
22 ## the result in @var{str}. @var{date} is a serial date number (see |
6555
|
23 ## @code{datenum}) or a date vector (see @code{datevec}). The value of |
|
24 ## @var{date} may also be a string or cell array of strings. |
5687
|
25 ## |
|
26 ## @var{f} can be an integer which corresponds to one of the codes in |
|
27 ## the table below, or a date format string. |
|
28 ## |
|
29 ## @var{p} is the year at the start of the century in which two-digit years |
|
30 ## are to be interpreted in. If not specified, it defaults to the current |
|
31 ## year minus 50. |
|
32 ## |
|
33 ## For example, the date 730736.65149 (2000-09-07 15:38:09.0934) would be |
|
34 ## formatted as follows: |
|
35 ## |
|
36 ## @multitable @columnfractions 0.1 0.45 0.35 |
|
37 ## @headitem Code @tab Format @tab Example |
|
38 ## @item 0 @tab dd-mmm-yyyy HH:MM:SS @tab 07-Sep-2000 15:38:09 |
|
39 ## @item 1 @tab dd-mmm-yyyy @tab 07-Sep-2000 |
|
40 ## @item 2 @tab mm/dd/yy @tab 09/07/00 |
|
41 ## @item 3 @tab mmm @tab Sep |
|
42 ## @item 4 @tab m @tab S |
|
43 ## @item 5 @tab mm @tab 09 |
|
44 ## @item 6 @tab mm/dd @tab 09/07 |
|
45 ## @item 7 @tab dd @tab 07 |
|
46 ## @item 8 @tab ddd @tab Thu |
|
47 ## @item 9 @tab d @tab T |
|
48 ## @item 10 @tab yyyy @tab 2000 |
|
49 ## @item 11 @tab yy @tab 00 |
|
50 ## @item 12 @tab mmmyy @tab Sep00 |
|
51 ## @item 13 @tab HH:MM:SS @tab 15:38:09 |
|
52 ## @item 14 @tab HH:MM:SS PM @tab 03:38:09 PM |
|
53 ## @item 15 @tab HH:MM @tab 15:38 |
|
54 ## @item 16 @tab HH:MM PM @tab 03:38 PM |
|
55 ## @item 17 @tab QQ-YY @tab Q3-00 |
|
56 ## @item 18 @tab QQ @tab Q3 |
|
57 ## @item 19 @tab dd/mm @tab 13/03 |
|
58 ## @item 20 @tab dd/mm/yy @tab 13/03/95 |
|
59 ## @item 21 @tab mmm.dd.yyyy HH:MM:SS @tab Mar.03.1962 13:53:06 |
|
60 ## @item 22 @tab mmm.dd.yyyy @tab Mar.03.1962 |
|
61 ## @item 23 @tab mm/dd/yyyy @tab 03/13/1962 |
|
62 ## @item 24 @tab dd/mm/yyyy @tab 12/03/1962 |
|
63 ## @item 25 @tab yy/mm/dd @tab 95/03/13 |
|
64 ## @item 26 @tab yyyy/mm/dd @tab 1995/03/13 |
|
65 ## @item 27 @tab QQ-YYYY @tab Q4-2132 |
|
66 ## @item 28 @tab mmmyyyy @tab Mar2047 |
|
67 ## @item 29 @tab yyyymmdd @tab 20470313 |
|
68 ## @item 30 @tab yyyymmddTHHMMSS @tab 20470313T132603 |
|
69 ## @item 31 @tab yyyy-mm-dd HH:MM:SS @tab 1047-03-13 13:26:03 |
|
70 ## @end multitable |
|
71 ## |
|
72 ## If @var{f} is a format string, the following symbols are recognised: |
|
73 ## |
|
74 ## @multitable @columnfractions 0.1 0.7 0.2 |
|
75 ## @headitem Symbol @tab Meaning @tab Example |
|
76 ## @item yyyy @tab Full year @tab 2005 |
|
77 ## @item yy @tab Two-digit year @tab 2005 |
|
78 ## @item mmmm @tab Full month name @tab December |
|
79 ## @item mmm @tab Abbreviated month name @tab Dec |
|
80 ## @item mm @tab Numeric month number (padded with zeros) @tab 01, 08, 12 |
|
81 ## @item m @tab First letter of month name (capitalized) @tab D |
|
82 ## @item dddd @tab Full weekday name @tab Sunday |
|
83 ## @item ddd @tab Abbreviated weekday name @tab Sun |
|
84 ## @item dd @tab Numeric day of month (padded with zeros) @tab 11 |
|
85 ## @item d @tab First letter of weekday name (capitalized) @tab S |
|
86 ## @item HH @tab Hour of day, padded with zeros if PM is set @tab 09:00 |
|
87 ## @item @tab and not padded with zeros otherwise @tab 9:00 AM |
|
88 ## @item MM @tab Minute of hour (padded with zeros) @tab 10:05 |
|
89 ## @item SS @tab Second of minute (padded with zeros) @tab 10:05:03 |
|
90 ## @item PM @tab Use 12-hour time format @tab 11:30 PM |
|
91 ## @end multitable |
|
92 ## |
|
93 ## If @var{f} is not specified or is @code{-1}, then use 0, 1 or 16, |
|
94 ## depending on whether the date portion or the time portion of |
|
95 ## @var{date} is empty. |
|
96 ## |
|
97 ## If @var{p} is nor specified, it defaults to the current year minus 50. |
|
98 ## |
|
99 ## If a matrix or cell array of dates is given, a vector of date strings is |
|
100 ## returned. |
|
101 ## |
|
102 ## @seealso{datenum, datevec, date, clock, now, datetick} |
|
103 ## @end deftypefn |
|
104 |
|
105 ## TODO: parse arbitrary code strings. |
|
106 ## TODO: e.g., for Wednesday 2001-03-05 09:04:06 AM, use |
|
107 ## TODO: yy 01 |
|
108 ## TODO: yyyy 2001 |
|
109 ## TODO: m M |
|
110 ## TODO: mm 03 |
|
111 ## TODO: mmm Mar |
|
112 ## TODO: d W |
|
113 ## TODO: dd 05 |
|
114 ## TODO: ddd Wed |
|
115 ## TODO: HH 09 |
|
116 ## TODO: MM 04 |
|
117 ## TODO: SS 06 |
|
118 ## TODO: PM AM |
|
119 ## TODO: Vectorize. It is particularly easy since all the codes are |
|
120 ## TODO: fixed width. Just generate the parts in separate arrays and |
|
121 ## TODO: concatenate. |
|
122 |
|
123 ## Author: pkienzle <pkienzle@users.sf.net> |
|
124 ## Created: 10 October 2001 (CVS) |
|
125 ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> |
|
126 |
|
127 function retval = datestr (date, f, p) |
|
128 |
|
129 persistent dateform names_mmmm names_mmm names_m names_dddd names_ddd names_d; |
|
130 |
|
131 if (isempty (dateform)) |
|
132 |
|
133 dateform = cell (32, 1); |
|
134 dateform{1} = "dd-mmm-yyyy HH:MM:SS"; |
|
135 dateform{2} = "dd-mmm-yyyy"; |
|
136 dateform{3} = "mm/dd/yy"; |
|
137 dateform{4} = "mmm"; |
|
138 dateform{5} = "m"; |
|
139 dateform{6} = "mm"; |
|
140 dateform{7} = "mm/dd"; |
|
141 dateform{8} = "dd"; |
|
142 dateform{9} = "ddd"; |
|
143 dateform{10} = "d"; |
|
144 dateform{11} = "yyyy"; |
|
145 dateform{12} = "yy"; |
|
146 dateform{13} = "mmmyy"; |
|
147 dateform{14} = "HH:MM:SS"; |
|
148 dateform{15} = "HH:MM:SS PM"; |
|
149 dateform{16} = "HH:MM"; |
|
150 dateform{17} = "HH:MM PM"; |
|
151 dateform{18} = "QQ-YY"; |
|
152 dateform{19} = "QQ"; |
|
153 dateform{20} = "dd/mm"; |
|
154 dateform{21} = "dd/mm/yy"; |
|
155 dateform{22} = "mmm.dd.yyyy HH:MM:SS"; |
|
156 dateform{23} = "mmm.dd.yyyy"; |
|
157 dateform{24} = "mm/dd/yyyy"; |
|
158 dateform{25} = "dd/mm/yyyy"; |
|
159 dateform{26} = "yy/mm/dd"; |
|
160 dateform{27} = "yyyy/mm/dd"; |
|
161 dateform{28} = "QQ-YYYY"; |
|
162 dateform{29} = "mmmyyyy"; |
|
163 dateform{30} = "yyyymmdd"; |
|
164 dateform{31} = "yyyymmddTHHMMSS"; |
|
165 dateform{32} = "yyyy-mm-dd HH:MM:SS"; |
|
166 |
|
167 names_mmmm = {"January"; "February"; "March"; "April"; |
|
168 "May"; "June"; "July"; "August"; |
|
169 "September"; "October"; "November"; "December"}; |
|
170 |
|
171 names_mmm = {"Jan"; "Feb"; "Mar"; "Apr"; "May"; "Jun"; |
|
172 "Jul"; "Aug"; "Sep"; "Oct"; "Nov"; "Dec"}; |
|
173 |
|
174 names_m = {"J"; "F"; "M"; "A"; "M"; "J"; "J"; "A"; "S"; "O"; "N"; "D"}; |
|
175 |
|
176 names_dddd = {"Sunday"; "Monday"; "Tuesday"; "Wednesday"; |
|
177 "Thursday"; "Friday"; "Saturday"}; |
|
178 |
|
179 names_ddd = {"Sun"; "Mon"; "Tue"; "Wed"; "Thu"; "Fri"; "Sat"}; |
|
180 |
|
181 names_d = {"S"; "M"; "T"; "W"; "T"; "F"; "S"}; |
|
182 |
|
183 endif |
|
184 |
|
185 if (nargin < 1 || nargin > 3) |
6046
|
186 print_usage (); |
5687
|
187 endif |
|
188 |
|
189 if (nargin < 2) |
|
190 f = []; |
|
191 endif |
|
192 if (nargin < 3) |
|
193 p = []; |
|
194 endif |
|
195 |
|
196 if (ischar (date)) |
|
197 t = date; |
|
198 date = cell (1); |
|
199 date{1} = t; |
|
200 endif |
|
201 |
|
202 # guess, so we might be wrong |
|
203 if (iscell (date) || columns (date) != 6) |
|
204 v = datevec (date, p); |
|
205 else |
|
206 v = date; |
|
207 endif |
|
208 |
|
209 for i = 1:(rows (v)) |
|
210 |
|
211 if (isempty (f) || f == -1) |
|
212 if (v(i,4:6) == 0) |
|
213 f = 1; |
|
214 #elseif (v(i,1:3) == [0, 1, 1]) |
|
215 elseif (v(i,1:3) == [-1, 12, 31]) |
|
216 f = 16; |
|
217 else |
|
218 f = 0; |
|
219 endif |
|
220 endif |
|
221 |
|
222 if (isnumeric (f)) |
|
223 df = dateform{f + 1}; |
|
224 else |
|
225 df = f; |
|
226 endif |
|
227 |
|
228 ## dates to lowercase (note: we cannot convert MM to mm) |
|
229 df = strrep (df, "YYYY", "yyyy"); |
|
230 df = strrep (df, "YY", "yy"); |
|
231 df = strrep (df, "QQ", "qq"); |
|
232 df = strrep (df, "MMMM", "mmmm"); |
|
233 df = strrep (df, "MMM", "mmm"); |
|
234 #### BEGIN NOT-SO-UGLY HACK #### |
|
235 idx_MM = strfind (df, "MM"); |
|
236 idx_AM = strfind (df, "AM"); |
|
237 idx_PM = strfind (df, "PM"); |
|
238 df = strrep (df, "M", "m"); |
|
239 df([idx_MM, idx_MM + 1, idx_AM + 1, idx_PM + 1]) = "M"; |
|
240 df(idx_AM) = "A"; |
|
241 df(idx_PM) = "P"; |
|
242 #### END NOT-SO-UGLY HACK #### |
|
243 df = strrep (df, "DDDD", "dddd"); |
|
244 df = strrep (df, "DDD", "ddd"); |
|
245 df = strrep (df, "DD", "dd"); |
|
246 df = strrep (df, "D", "d"); |
|
247 ## times to uppercase (also cannot convert mm to MM) |
|
248 df = strrep (df, "hh", "HH"); |
|
249 df = strrep (df, "ss", "SS"); |
|
250 df = strrep (df, "am", "AM"); |
|
251 df = strrep (df, "pm", "PM"); |
|
252 |
|
253 str = df; |
|
254 ## replace date symbols with actual values |
|
255 str = strrep (str, "yyyy", sprintf ("%04d", v(i,1))); |
|
256 str = strrep (str, "yy", sprintf ("%02d", rem (v(i,1), 100))); |
|
257 str = strrep (str, "qq", sprintf ("Q%d", fix ((v(i,2) + 2) / 3))); |
|
258 str = strrep (str, "mmmm", names_mmmm{v(i,2)}); |
|
259 str = strrep (str, "mmm", names_mmm{v(i,2)}); |
|
260 str = strrep (str, "mm", sprintf ("%02d", v(i,2))); |
|
261 str = strrep (str, "m", names_m{v(i,2)}); |
|
262 str = strrep (str, "dddd", names_dddd{weekday (datenum (v(i,1), v(i,2), v(i,3)))}); |
|
263 str = strrep (str, "ddd", names_ddd{weekday (datenum (v(i,1), v(i,2), v(i,3)))}); |
|
264 str = strrep (str, "dd", sprintf ("%02d", v(i,3))); |
|
265 str = strrep (str, "d", names_d{weekday (datenum (v(i,1), v(i,2), v(i,3)))}); |
|
266 ## replace time symbols with actual values |
|
267 if ((any (strfind (str, "AM")) || any (strfind (str, "PM")))) |
|
268 if (v(i,4) > 12) |
|
269 str = strrep (str, "HH", sprintf ("%d", v(i,4) - 12)); |
|
270 else |
|
271 str = strrep (str, "HH", sprintf ("%d", v(i,4))); |
|
272 endif |
|
273 if (v(i,4) < 12) |
|
274 str = strrep (str, "AM", "AM"); |
|
275 str = strrep (str, "PM", "AM"); |
|
276 else |
|
277 str = strrep (str, "AM", "PM"); |
|
278 str = strrep (str, "PM", "PM"); |
|
279 endif |
|
280 else |
|
281 str = strrep (str, "HH", sprintf ("%02d", v(i,4))); |
|
282 endif |
|
283 str = strrep (str, "MM", sprintf ("%02d", v(i,5))); |
|
284 str = strrep (str, "SS", sprintf ("%02d", v(i,6))); |
|
285 |
|
286 if (i == 1) |
|
287 retval = str; |
|
288 else |
|
289 retval = [retval; str]; |
|
290 endif |
|
291 |
|
292 endfor |
|
293 |
|
294 endfunction |
|
295 |
|
296 # simple tests |
|
297 %!shared testtime |
|
298 %! testtime = [2005.0000, 12.0000, 18.0000, 2.0000, 33.0000, 17.3822]; |
|
299 %!assert(datestr(testtime,0),"18-Dec-2005 02:33:17"); |
|
300 %!assert(datestr(testtime,1),"18-Dec-2005"); |
|
301 %!assert(datestr(testtime,2),"12/18/05"); |
|
302 %!assert(datestr(testtime,3),"Dec"); |
|
303 %!assert(datestr(testtime,4),"D"); |
|
304 %!assert(datestr(testtime,5),"12"); |
|
305 %!assert(datestr(testtime,6),"12/18"); |
|
306 %!assert(datestr(testtime,7),"18"); |
|
307 %!assert(datestr(testtime,8),"Sun"); |
|
308 %!assert(datestr(testtime,9),"S"); |
|
309 %!assert(datestr(testtime,10),"2005"); |
|
310 %!assert(datestr(testtime,11),"05"); |
|
311 %!assert(datestr(testtime,12),"Dec05"); |
|
312 %!assert(datestr(testtime,13),"02:33:17"); |
|
313 %!assert(datestr(testtime,14),"2:33:17 AM"); |
|
314 %!assert(datestr(testtime,15),"02:33"); |
|
315 %!assert(datestr(testtime,16),"2:33 AM"); |
|
316 %!assert(datestr(testtime,17),"Q4-05"); |
|
317 %!assert(datestr(testtime,18),"Q4"); |
|
318 %!assert(datestr(testtime,19),"18/12"); |
|
319 %!assert(datestr(testtime,20),"18/12/05"); |
|
320 %!assert(datestr(testtime,21),"Dec.18.2005 02:33:17"); |
|
321 %!assert(datestr(testtime,22),"Dec.18.2005"); |
|
322 %!assert(datestr(testtime,23),"12/18/2005"); |
|
323 %!assert(datestr(testtime,24),"18/12/2005"); |
|
324 %!assert(datestr(testtime,25),"05/12/18"); |
|
325 %!assert(datestr(testtime,26),"2005/12/18"); |
|
326 %!assert(datestr(testtime,27),"Q4-2005"); |
|
327 %!assert(datestr(testtime,28),"Dec2005"); |
|
328 %!assert(datestr(testtime,29),"20051218"); |
|
329 %!assert(datestr(testtime,30),"20051218T023317"); |
|
330 %!assert(datestr(testtime,31),"2005-12-18 02:33:17"); |
|
331 # demos |
|
332 %!demo |
|
333 %! datestr (now ()) |
|
334 %!demo |
|
335 %! datestr (rem (now (), 1)) |
|
336 %!demo |
|
337 %! datestr (floor (now ())) |