{"id":5156,"date":"2014-02-28T09:00:10","date_gmt":"2014-02-28T14:00:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5156"},"modified":"2014-02-27T11:02:28","modified_gmt":"2014-02-27T16:02:28","slug":"displaying-time-in-human-readable-form","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/02\/28\/displaying-time-in-human-readable-form\/","title":{"rendered":"Displaying time in human readable form"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22977-convert-seconds-to-human-readable-string-s-\"><tt>seconds2human<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/45753\">Rody Oldenhuis<\/a>.<\/p><p>There was a time when I obsessed over waitbars. Why is that? I'm not sure, but Ned has a few <a href=\"https:\/\/blogs.mathworks.com\/community\/2012\/08\/13\/wait-bars-and-beyond-call-me-when-youre-done\/\">guesses<\/a>. But one of the features that I look for in these <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=waitbar\">waitbar entries<\/a> is the ability to show the remaining time, like some of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?&amp;term=waitbar+time+remaining\">these<\/a>. I have created my own waitbar as well to include the time remaining, and I remember thinking quite a bit about how I display the time. I can easily calculate the time using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tic.html\"><tt>tic<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/toc.html\"><tt>toc<\/tt><\/a>. But what's the best way to display the seconds? It's probably better to say \"3 hours 44 minutes 13 seconds remaining\" than to say \"13453 seconds remaining\". So I had to do some manipulations to have an appropriate display, and many of the entries I highlighted above do that as well.<\/p><p>Rody's <tt>seconds2human<\/tt> does this nicely. You simply pass in the time in seconds, and it will convert it to something human understandable. There's an approximate display:<\/p><pre class=\"codeinput\">seconds2human(13453)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\nAbout 3 hours and 44 minutes.\r\n<\/pre><p>and an accurate display:<\/p><pre class=\"codeinput\">seconds2human(13453,<span class=\"string\">'full'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n3 hours, 44 minutes, 13 seconds.\r\n<\/pre><p>Now, I can just make use of this and not have to worry about decomposing the seconds.<\/p><pre class=\"language-matlab\">hWait = waitbar(0, <span class=\"string\">'Calculating...'<\/span>);\r\ntotal = 1000;\r\n<span class=\"comment\">% Start timer<\/span>\r\ntstart = tic;\r\n<span class=\"keyword\">for<\/span> iter = 1:total\r\n    SomeComputation(iter);\r\n    <span class=\"comment\">% Calculate remaining time<\/span>\r\n    remaining = toc(tstart)*(total-iter)\/iter;\r\n    <span class=\"comment\">% Update waitbar<\/span>\r\n    waitbar(iter\/total,hWait, <span class=\"keyword\">...<\/span>\r\n        [<span class=\"string\">'Time remaining: '<\/span>,seconds2human(remaining)])\r\n<span class=\"keyword\">end<\/span>\r\ndelete(hWait);\r\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_seconds2human\/waitbar1.png\" alt=\"\"> <\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_seconds2human\/waitbar2.png\" alt=\"\"> <\/p><p><b>Comments<\/b><\/p><p>I also want to point out that Rody's code is very well-written. It is well-documented, has error checking, and allows for a vectorized call, i.e. you can pass an array of numbers. Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5156#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22977-convert-seconds-to-human-readable-string-s-#comments\">comment<\/a> for Rody.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_7002b5366a2a4ac38eabc5e6a69015b2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='7002b5366a2a4ac38eabc5e6a69015b2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7002b5366a2a4ac38eabc5e6a69015b2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_7002b5366a2a4ac38eabc5e6a69015b2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p><\/div><!--\r\n7002b5366a2a4ac38eabc5e6a69015b2 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22977-convert-seconds-to-human-readable-string-s-\r\n% |seconds2human|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/45753 Rody\r\n% Oldenhuis>.\r\n%\r\n% There was a time when I obsessed over waitbars. Why is that? I'm not\r\n% sure, but Ned has a few\r\n% <https:\/\/blogs.mathworks.com\/community\/2012\/08\/13\/wait-bars-and-beyond-call-me-when-youre-done\/\r\n% guesses>. But one of the features that I look for in these\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=waitbar\r\n% waitbar entries> is the ability to show the remaining time, like some of\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?&term=waitbar+time+remaining\r\n% these>. I have created my own waitbar as well to include the time\r\n% remaining, and I remember thinking quite a bit about how I display the\r\n% time. I can easily calculate the time using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/tic.html |tic|> and\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/toc.html |toc|>. But what's the\r\n% best way to display the seconds? It's probably better to say \"3 hours 44\r\n% minutes 13 seconds remaining\" than to say \"13453 seconds remaining\". So I\r\n% had to do some manipulations to have an appropriate display, and many of\r\n% the entries I highlighted above do that as well.\r\n%\r\n% Rody's |seconds2human| does this nicely. You simply pass in the time in\r\n% seconds, and it will convert it to something human understandable.\r\n% There's an approximate display:\r\n\r\nseconds2human(13453)\r\n\r\n%%\r\n% and an accurate display:\r\n\r\nseconds2human(13453,'full')\r\n\r\n%%\r\n% Now, I can just make use of this and not have to worry about decomposing\r\n% the seconds.\r\n%\r\n%   hWait = waitbar(0, 'Calculating...');\r\n%   total = 1000;\r\n%   % Start timer\r\n%   tstart = tic;\r\n%   for iter = 1:total\r\n%       SomeComputation(iter);\r\n%       % Calculate remaining time\r\n%       remaining = toc(tstart)*(total-iter)\/iter;\r\n%       % Update waitbar\r\n%       waitbar(iter\/total,hWait, ...\r\n%           ['Time remaining: ',seconds2human(remaining)])\r\n%   end\r\n%   delete(hWait);\r\n%\r\n% <<waitbar1.png>>\r\n%\r\n% <<waitbar2.png>>\r\n%\r\n%%\r\n% *Comments*\r\n%\r\n% I also want to point out that Rody's code is very well-written. It is\r\n% well-documented, has error checking, and allows for a vectorized call,\r\n% i.e. you can pass an array of numbers. Give this a try and let us know\r\n% what you think <https:\/\/blogs.mathworks.com\/pick\/?p=5156#respond here> or\r\n% leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/22977-convert-seconds-to-human-readable-string-s-#comments\r\n% comment> for Rody.\r\n\r\n##### SOURCE END ##### 7002b5366a2a4ac38eabc5e6a69015b2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_seconds2human\/waitbar1.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro's pick this week is seconds2human by Rody Oldenhuis.There was a time when I obsessed over waitbars. Why is that? I'm not sure, but Ned has a few guesses. But one of the features that I look... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/02\/28\/displaying-time-in-human-readable-form\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5156"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5156"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5156\/revisions"}],"predecessor-version":[{"id":5159,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5156\/revisions\/5159"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}