Mercurial > octave-nkf
view scripts/time/datestr.m @ 17281:bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Macro handles options ("on") or properties ("position") more elegantly
than @code{"text"}.
* doc/interpreter/macros.texi: Add new @qcode macro.
* doc/interpreter/tips.txi: Add documentation about @qcode macro.
* doc/interpreter/basics.txi, doc/interpreter/container.txi,
doc/interpreter/emacs.txi, doc/interpreter/errors.txi,
doc/interpreter/eval.txi, doc/interpreter/expr.txi,
doc/interpreter/external.txi, doc/interpreter/func.txi,
doc/interpreter/grammar.txi, doc/interpreter/image.txi,
doc/interpreter/install.txi, doc/interpreter/interp.txi,
doc/interpreter/io.txi, doc/interpreter/matrix.txi,
doc/interpreter/numbers.txi, doc/interpreter/oop.txi,
doc/interpreter/package.txi, doc/interpreter/plot.txi,
doc/interpreter/quad.txi, doc/interpreter/sparse.txi,
doc/interpreter/strings.txi, doc/interpreter/system.txi,
doc/interpreter/vectorize.txi, libinterp/corefcn/balance.cc,
libinterp/corefcn/bitfcns.cc, libinterp/corefcn/cellfun.cc,
libinterp/corefcn/conv2.cc, libinterp/corefcn/data.cc,
libinterp/corefcn/debug.cc, libinterp/corefcn/defaults.cc,
libinterp/corefcn/dirfns.cc, libinterp/corefcn/dlmread.cc,
libinterp/corefcn/error.cc, libinterp/corefcn/file-io.cc,
libinterp/corefcn/find.cc, libinterp/corefcn/gammainc.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/help.cc,
libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc,
libinterp/corefcn/load-path.cc, libinterp/corefcn/load-save.cc,
libinterp/corefcn/ls-oct-ascii.cc, libinterp/corefcn/lu.cc,
libinterp/corefcn/luinc.cc, libinterp/corefcn/matrix_type.cc,
libinterp/corefcn/oct-hist.cc, libinterp/corefcn/pager.cc,
libinterp/corefcn/pr-output.cc, libinterp/corefcn/pt-jit.cc,
libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc,
libinterp/corefcn/regexp.cc, libinterp/corefcn/schur.cc,
libinterp/corefcn/sighandlers.cc, libinterp/corefcn/sparse.cc,
libinterp/corefcn/spparms.cc, libinterp/corefcn/str2double.cc,
libinterp/corefcn/svd.cc, libinterp/corefcn/symtab.cc,
libinterp/corefcn/syscalls.cc, libinterp/corefcn/toplev.cc,
libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc,
libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc,
libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/chol.cc,
libinterp/dldfcn/colamd.cc, libinterp/dldfcn/fftw.cc, libinterp/dldfcn/qr.cc,
libinterp/dldfcn/symbfact.cc, libinterp/octave-value/ov-base.cc,
libinterp/octave-value/ov-fcn-handle.cc,
libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc,
libinterp/octave-value/ov-range.cc, libinterp/octave-value/ov-struct.cc,
libinterp/octave-value/ov-usr-fcn.cc, libinterp/parse-tree/oct-parse.in.yy,
libinterp/parse-tree/pt-binop.cc, libinterp/parse-tree/pt-eval.cc,
libinterp/parse-tree/pt-mat.cc, scripts/@ftp/ftp.m,
scripts/deprecated/java_convert_matrix.m, scripts/deprecated/java_debug.m,
scripts/deprecated/java_unsigned_conversion.m, scripts/deprecated/shell_cmd.m,
scripts/general/dblquad.m, scripts/general/display.m,
scripts/general/genvarname.m, scripts/general/idivide.m,
scripts/general/interp1.m, scripts/general/interp2.m,
scripts/general/interp3.m, scripts/general/interpn.m, scripts/general/isa.m,
scripts/general/profexplore.m, scripts/general/profile.m,
scripts/general/quadgk.m, scripts/general/randi.m, scripts/general/structfun.m,
scripts/general/subsindex.m, scripts/general/triplequad.m,
scripts/geometry/griddata.m, scripts/geometry/griddata3.m,
scripts/geometry/griddatan.m, scripts/geometry/voronoi.m, scripts/help/help.m,
scripts/help/lookfor.m, scripts/image/cmpermute.m, scripts/image/colormap.m,
scripts/image/image.m, scripts/image/imagesc.m, scripts/image/imfinfo.m,
scripts/image/imformats.m, scripts/image/imread.m, scripts/image/imshow.m,
scripts/image/imwrite.m, scripts/image/ind2gray.m, scripts/image/lines.m,
scripts/image/rgb2ind.m, scripts/image/spinmap.m, scripts/io/dlmwrite.m,
scripts/io/strread.m, scripts/io/textread.m, scripts/io/textscan.m,
scripts/java/javaclasspath.m, scripts/java/usejava.m,
scripts/miscellaneous/bzip2.m, scripts/miscellaneous/computer.m,
scripts/miscellaneous/copyfile.m, scripts/miscellaneous/debug.m,
scripts/miscellaneous/dos.m, scripts/miscellaneous/edit.m,
scripts/miscellaneous/gzip.m, scripts/miscellaneous/license.m,
scripts/miscellaneous/mkoctfile.m, scripts/miscellaneous/movefile.m,
scripts/miscellaneous/parseparams.m, scripts/miscellaneous/unix.m,
scripts/optimization/fminbnd.m, scripts/optimization/fminsearch.m,
scripts/optimization/fminunc.m, scripts/optimization/fsolve.m,
scripts/optimization/fzero.m, scripts/optimization/glpk.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/optimset.m,
scripts/optimization/pqpnonneg.m, scripts/pkg/pkg.m, scripts/plot/allchild.m,
scripts/plot/ancestor.m, scripts/plot/area.m, scripts/plot/axis.m,
scripts/plot/bar.m, scripts/plot/barh.m, scripts/plot/box.m,
scripts/plot/caxis.m, scripts/plot/cla.m, scripts/plot/clabel.m,
scripts/plot/clf.m, scripts/plot/close.m, scripts/plot/colorbar.m,
scripts/plot/daspect.m, scripts/plot/ezmesh.m, scripts/plot/ezmeshc.m,
scripts/plot/ezsurf.m, scripts/plot/ezsurfc.m, scripts/plot/findall.m,
scripts/plot/findobj.m, scripts/plot/gcbo.m, scripts/plot/gcf.m,
scripts/plot/gco.m, scripts/plot/grid.m, scripts/plot/guihandles.m,
scripts/plot/hdl2struct.m, scripts/plot/hidden.m, scripts/plot/hold.m,
scripts/plot/isonormals.m, scripts/plot/isosurface.m, scripts/plot/legend.m,
scripts/plot/mesh.m, scripts/plot/meshc.m, scripts/plot/meshz.m,
scripts/plot/newplot.m, scripts/plot/orient.m, scripts/plot/pareto.m,
scripts/plot/patch.m, scripts/plot/pbaspect.m, scripts/plot/pcolor.m,
scripts/plot/plot.m, scripts/plot/print.m,
scripts/plot/private/__add_default_menu__.m, scripts/plot/quiver.m,
scripts/plot/quiver3.m, scripts/plot/refreshdata.m, scripts/plot/saveas.m,
scripts/plot/scatter.m, scripts/plot/scatter3.m, scripts/plot/shading.m,
scripts/plot/shrinkfaces.m, scripts/plot/slice.m, scripts/plot/stem.m,
scripts/plot/stem3.m, scripts/plot/struct2hdl.m, scripts/plot/subplot.m,
scripts/plot/surf.m, scripts/plot/surfc.m, scripts/plot/surfl.m,
scripts/plot/tetramesh.m, scripts/plot/uigetfile.m, scripts/plot/uimenu.m,
scripts/plot/uiputfile.m, scripts/plot/waterfall.m, scripts/plot/whitebg.m,
scripts/plot/xlim.m, scripts/plot/ylim.m, scripts/plot/zlim.m,
scripts/polynomial/conv.m, scripts/polynomial/polyout.m,
scripts/polynomial/splinefit.m, scripts/set/ismember.m, scripts/set/powerset.m,
scripts/set/setdiff.m, scripts/set/union.m, scripts/set/unique.m,
scripts/signal/detrend.m, scripts/signal/filter2.m, scripts/signal/freqz.m,
scripts/signal/periodogram.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/sparse/eigs.m, scripts/sparse/svds.m,
scripts/specfun/legendre.m, scripts/special-matrix/gallery.m,
scripts/statistics/base/mean.m, scripts/statistics/base/moment.m,
scripts/statistics/tests/cor_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test.m,
scripts/statistics/tests/kolmogorov_smirnov_test_2.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/prop_test_2.m, scripts/statistics/tests/sign_test.m,
scripts/statistics/tests/t_test.m, scripts/statistics/tests/t_test_2.m,
scripts/statistics/tests/t_test_regression.m,
scripts/statistics/tests/u_test.m, scripts/statistics/tests/var_test.m,
scripts/statistics/tests/welch_test.m,
scripts/statistics/tests/wilcoxon_test.m, scripts/statistics/tests/z_test.m,
scripts/statistics/tests/z_test_2.m, scripts/strings/base2dec.m,
scripts/strings/index.m, scripts/strings/isstrprop.m,
scripts/strings/mat2str.m, scripts/strings/regexptranslate.m,
scripts/strings/rindex.m, scripts/strings/str2num.m, scripts/strings/strcat.m,
scripts/strings/strjust.m, scripts/strings/strmatch.m,
scripts/strings/validatestring.m, scripts/testfun/demo.m,
scripts/testfun/example.m, scripts/testfun/test.m, scripts/time/addtodate.m,
scripts/time/asctime.m, scripts/time/datestr.m, scripts/time/datetick.m,
scripts/time/weekday.m, scripts/ui/errordlg.m, scripts/ui/helpdlg.m,
scripts/ui/inputdlg.m, scripts/ui/listdlg.m, scripts/ui/msgbox.m,
scripts/ui/questdlg.m, scripts/ui/warndlg.m: Use new @qcode macro.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 19 Aug 2013 20:46:38 -0700 |
parents | 3fd6f637c573 |
children | d63878346099 |
line wrap: on
line source
## Copyright (C) 2000-2012 Paul Kienzle ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 3 of the License, or (at ## your option) any later version. ## ## Octave is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, see ## <http://www.gnu.org/licenses/>. ## -*- texinfo -*- ## @deftypefn {Function File} {@var{str} =} datestr (@var{date}) ## @deftypefnx {Function File} {@var{str} =} datestr (@var{date}, @var{f}) ## @deftypefnx {Function File} {@var{str} =} datestr (@var{date}, @var{f}, @var{p}) ## Format the given date/time according to the format @code{f} and return ## the result in @var{str}. @var{date} is a serial date number (see ## @code{datenum}) or a date vector (see @code{datevec}). The value of ## @var{date} may also be a string or cell array of strings. ## ## @var{f} can be an integer which corresponds to one of the codes in ## the table below, or a date format string. ## ## @var{p} is the year at the start of the century in which two-digit years ## are to be interpreted in. If not specified, it defaults to the current ## year minus 50. ## ## For example, the date 730736.65149 (2000-09-07 15:38:09.0934) would be ## formatted as follows: ## ## @multitable @columnfractions 0.1 0.45 0.35 ## @headitem Code @tab Format @tab Example ## @item 0 @tab dd-mmm-yyyy HH:MM:SS @tab 07-Sep-2000 15:38:09 ## @item 1 @tab dd-mmm-yyyy @tab 07-Sep-2000 ## @item 2 @tab mm/dd/yy @tab 09/07/00 ## @item 3 @tab mmm @tab Sep ## @item 4 @tab m @tab S ## @item 5 @tab mm @tab 09 ## @item 6 @tab mm/dd @tab 09/07 ## @item 7 @tab dd @tab 07 ## @item 8 @tab ddd @tab Thu ## @item 9 @tab d @tab T ## @item 10 @tab yyyy @tab 2000 ## @item 11 @tab yy @tab 00 ## @item 12 @tab mmmyy @tab Sep00 ## @item 13 @tab HH:MM:SS @tab 15:38:09 ## @item 14 @tab HH:MM:SS PM @tab 03:38:09 PM ## @item 15 @tab HH:MM @tab 15:38 ## @item 16 @tab HH:MM PM @tab 03:38 PM ## @item 17 @tab QQ-YY @tab Q3-00 ## @item 18 @tab QQ @tab Q3 ## @item 19 @tab dd/mm @tab 07/09 ## @item 20 @tab dd/mm/yy @tab 07/09/00 ## @item 21 @tab mmm.dd,yyyy HH:MM:SS @tab Sep.07,2000 15:38:08 ## @item 22 @tab mmm.dd,yyyy @tab Sep.07,2000 ## @item 23 @tab mm/dd/yyyy @tab 09/07/2000 ## @item 24 @tab dd/mm/yyyy @tab 07/09/2000 ## @item 25 @tab yy/mm/dd @tab 00/09/07 ## @item 26 @tab yyyy/mm/dd @tab 2000/09/07 ## @item 27 @tab QQ-YYYY @tab Q3-2000 ## @item 28 @tab mmmyyyy @tab Sep2000 ## @item 29 @tab yyyy-mm-dd @tab 2000-09-07 ## @item 30 @tab yyyymmddTHHMMSS @tab 20000907T153808 ## @item 31 @tab yyyy-mm-dd HH:MM:SS @tab 2000-09-07 15:38:08 ## @end multitable ## ## If @var{f} is a format string, the following symbols are recognized: ## ## @multitable @columnfractions 0.1 0.7 0.2 ## @headitem Symbol @tab Meaning @tab Example ## @item yyyy @tab Full year @tab 2005 ## @item yy @tab Two-digit year @tab 05 ## @item mmmm @tab Full month name @tab December ## @item mmm @tab Abbreviated month name @tab Dec ## @item mm @tab Numeric month number (padded with zeros) @tab 01, 08, 12 ## @item m @tab First letter of month name (capitalized) @tab D ## @item dddd @tab Full weekday name @tab Sunday ## @item ddd @tab Abbreviated weekday name @tab Sun ## @item dd @tab Numeric day of month (padded with zeros) @tab 11 ## @item d @tab First letter of weekday name (capitalized) @tab S ## @item HH @tab Hour of day, padded with zeros if PM is set @tab 09:00 ## @item @tab and not padded with zeros otherwise @tab 9:00 AM ## @item MM @tab Minute of hour (padded with zeros) @tab 10:05 ## @item SS @tab Second of minute (padded with zeros) @tab 10:05:03 ## @item FFF @tab Milliseconds of second (padded with zeros) @tab 10:05:03.012 ## @item AM @tab Use 12-hour time format @tab 11:30 AM ## @item PM @tab Use 12-hour time format @tab 11:30 PM ## @end multitable ## ## If @var{f} is not specified or is @code{-1}, then use 0, 1 or 16, ## depending on whether the date portion or the time portion of ## @var{date} is empty. ## ## If @var{p} is nor specified, it defaults to the current year minus 50. ## ## If a matrix or cell array of dates is given, a column vector of date strings ## is returned. ## ## @seealso{datenum, datevec, date, now, clock} ## @end deftypefn ## FIXME: parse arbitrary code strings. ## e.g., for Wednesday 2001-03-05 09:04:06 AM, use ## yy 01 ## yyyy 2001 ## m M ## mm 03 ## mmm Mar ## d W ## dd 05 ## ddd Wed ## HH 09 ## MM 04 ## SS 06 ## PM AM ## FIXME: Vectorize. It is particularly easy since all the codes are ## fixed width. Just generate the parts in separate arrays and ## concatenate. ## Author: pkienzle <pkienzle@users.sf.net> ## Created: 10 October 2001 (CVS) ## Adapted-By: William Poetra Yoga Hadisoeseno <williampoetra@gmail.com> function retval = datestr (date, f = [], p = []) persistent dateform names_mmmm names_m names_d; if (isempty (dateform)) dateform = cell (32, 1); dateform{1} = "dd-mmm-yyyy HH:MM:SS"; dateform{2} = "dd-mmm-yyyy"; dateform{3} = "mm/dd/yy"; dateform{4} = "mmm"; dateform{5} = "m"; dateform{6} = "mm"; dateform{7} = "mm/dd"; dateform{8} = "dd"; dateform{9} = "ddd"; dateform{10} = "d"; dateform{11} = "yyyy"; dateform{12} = "yy"; dateform{13} = "mmmyy"; dateform{14} = "HH:MM:SS"; dateform{15} = "HH:MM:SS PM"; dateform{16} = "HH:MM"; dateform{17} = "HH:MM PM"; dateform{18} = "QQ-YY"; dateform{19} = "QQ"; dateform{20} = "dd/mm"; dateform{21} = "dd/mm/yy"; dateform{22} = "mmm.dd,yyyy HH:MM:SS"; dateform{23} = "mmm.dd,yyyy"; dateform{24} = "mm/dd/yyyy"; dateform{25} = "dd/mm/yyyy"; dateform{26} = "yy/mm/dd"; dateform{27} = "yyyy/mm/dd"; dateform{28} = "QQ-YYYY"; dateform{29} = "mmmyyyy"; dateform{30} = "yyyy-mm-dd"; dateform{31} = "yyyymmddTHHMMSS"; dateform{32} = "yyyy-mm-dd HH:MM:SS"; names_m = {"J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"}; names_d = {"S", "M", "T", "W", "T", "F", "S"}; endif if (nargin < 1 || nargin > 3) print_usage (); endif ## Guess input type. We might be wrong. if (ischar (date) || iscellstr (date) || columns (date) != 6) v = datevec (date, p); else v = []; if (columns (date) == 6) ## Make sure that the input really is a datevec. maxdatevec = [Inf, 12, 31, 23, 59, 60]; if (any (max (date, 1) > maxdatevec) || any (date(:,1:5) != floor (date(:,1:5)))) v = datevec (date, p); endif endif if (isempty (v)) v = date; endif endif retval = []; for i = 1 : rows (v) if (isempty (f)) if (v(i,4:6) == 0) f = 1; elseif (v(i,1:3) == [-1, 12, 31]) f = 16; else f = 0; endif endif if (isnumeric (f)) df = dateform{f + 1}; else df = f; endif df_orig = df; df = strrep (df, 'AM', "%p"); df = strrep (df, 'PM', "%p"); if (strcmp (df, df_orig)) ## PM not set. df = strrep (df, "HH", "%H"); else df = strrep (df, "HH", sprintf ("%2d", v(i,4))); endif df = regexprep (df, '[Yy][Yy][Yy][Yy]', "%Y"); df = regexprep (df, '[Yy][Yy]', "%y"); df = regexprep (df, '[Dd][Dd][Dd][Dd]', "%A"); df = regexprep (df, '[Dd][Dd][Dd]', "%a"); df = regexprep (df, '[Dd][Dd]', "%d"); wday = weekday (datenum (v(i,1), v(i,2), v(i,3))); tmp = names_d{wday}; df = regexprep (df, '([^%])[Dd]', sprintf ("$1%s", tmp)); df = regexprep (df, '^[Dd]', sprintf ("%s", tmp)); df = strrep (df, "mmmm", "%B"); df = strrep (df, "mmm", "%b"); df = strrep (df, "mm", "%m"); tmp = names_m{v(i,2)}; pos = regexp (df, '[^%]m') + 1; df(pos) = tmp; df = regexprep (df, '^m', tmp); df = strrep (df, "MM", "%M"); df = regexprep (df, '[Ss][Ss]', "%S"); df = strrep (df, "FFF", sprintf ("%03d", 1000 * (v(i,6) - fix (v(i,6))))); df = strrep (df, 'QQ', sprintf ("Q%d", fix ((v(i,2) + 2) / 3))); vi = v(i,:); tm.year = vi(1) - 1900; tm.mon = vi(2) - 1; tm.mday = vi(3); tm.hour = vi(4); tm.min = vi(5); sec = vi(6); tm.sec = fix (sec); tm.usec = fix ((sec - tm.sec) * 1e6); tm.wday = wday - 1; ## FIXME -- Do we need YDAY and DST? How should they be computed? ## We don't want to use "localtime (mktime (tm))" because that ## doesn't correctly handle dates before 1970-01-01 on some systems. ## tm.yday = ?; ## tm.isdst = ?; str = strftime (df, tm); retval = [retval; str]; endfor endfunction %!demo %! ## Current date and time in default format %! datestr (now ()) %!demo %! ## Current date (integer portion of datenum) %! datestr (fix (now ())) %!demo %! ## Current time (fractional portion of datenum) %! datestr (rem (now (), 1)) %!shared testtime %! testtime = [2005.0000, 12.0000, 18.0000, 2.0000, 33.0000, 17.3822]; %!assert (datestr (testtime,0), "18-Dec-2005 02:33:17") %!assert (datestr (testtime,1), "18-Dec-2005") %!assert (datestr (testtime,2), "12/18/05") %!assert (datestr (testtime,3), "Dec") %!assert (datestr (testtime,4), "D") %!assert (datestr (testtime,5), "12") %!assert (datestr (testtime,6), "12/18") %!assert (datestr (testtime,7), "18") %!assert (datestr (testtime,8), "Sun") %!assert (datestr (testtime,9), "S") %!assert (datestr (testtime,10), "2005") %!assert (datestr (testtime,11), "05") %!assert (datestr (testtime,12), "Dec05") %!assert (datestr (testtime,13), "02:33:17") %!assert (datestr (testtime,14), " 2:33:17 AM") %!assert (datestr (testtime,15), "02:33") %!assert (datestr (testtime,16), " 2:33 AM") %!assert (datestr (testtime,17), "Q4-05") %!assert (datestr (testtime,18), "Q4") %!assert (datestr (testtime,19), "18/12") %!assert (datestr (testtime,20), "18/12/05") %!assert (datestr (testtime,21), "Dec.18,2005 02:33:17") %!assert (datestr (testtime,22), "Dec.18,2005") %!assert (datestr (testtime,23), "12/18/2005") %!assert (datestr (testtime,24), "18/12/2005") %!assert (datestr (testtime,25), "05/12/18") %!assert (datestr (testtime,26), "2005/12/18") %!assert (datestr (testtime,27), "Q4-2005") %!assert (datestr (testtime,28), "Dec2005") %!assert (datestr (testtime,29), "2005-12-18") %!assert (datestr (testtime,30), "20051218T023317") %!assert (datestr (testtime,31), "2005-12-18 02:33:17") %!assert (datestr (testtime+[0 0 3 0 0 0], "dddd"), "Wednesday") ## Test possible bug where input is a vector of datenums that is exactly 6 wide %!assert (datestr ([1944, 6, 6, 6, 30, 0], 0), "06-Jun-1944 06:30:00") ## Test fractional millisecond time extension %!assert (datestr (testtime, "HH:MM:SS:FFF"), "02:33:17:382") %% Test input validation %!error datestr () %!error datestr (1, 2, 3, 4)