home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / sbTimeFormatter.jsm < prev    next >
Text File  |  2012-06-08  |  4KB  |  128 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. //
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. //
  13. // Software distributed under the License is distributed
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  15. // express or implied. See the GPL for the specific language
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc.,
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. // END SONGBIRD GPL
  24. //
  25. */
  26.  
  27. EXPORTED_SYMBOLS = ["TimeFormatter"];
  28.  
  29. const Cc = Components.classes;
  30. const Ci = Components.interfaces;
  31. const Cr = Components.results
  32.  
  33. var TimeFormatter = {
  34.   _cfg: {
  35.     localeBundlePath: "chrome://songbird/locale/songbird.properties"
  36.   },
  37.  
  38.  
  39.   /*
  40.    * formatSingle
  41.    *
  42.    *   ==> aTime                Time in seconds to format.
  43.    *
  44.    *   <==                      Formatted time string with single unit.
  45.    *
  46.    *   This function returns a formatted version of the time value specified in
  47.    * seconds by aTime.  The formatted string contains a single unit of seconds,
  48.    * minutes, or hours.
  49.    *
  50.    * Example:
  51.    *
  52.    *   formatSingle(32) returns "32 seconds"
  53.    *   formatSingle(66) returns "1.1 minutes"
  54.    */
  55.  
  56.   formatSingle: function(aTime) {
  57.     /* Don't allow negative times. */
  58.     if (aTime < 0) {
  59.       throw Cr.NS_ERROR_INVALID_ARG;
  60.     }
  61.  
  62.     /* Get the format time value and unit. */
  63.     var formatTime;
  64.     var formatTimeUnit;
  65.     if (aTime >= 3600) {
  66.       formatTime = (aTime * 10) / 3600;
  67.       formatTime = Math.floor(formatTime);
  68.       formatTime = formatTime / 10;
  69.       if (formatTime != 1)
  70.         formatTimeUnit = "timeformatter.hours";
  71.       else
  72.         formatTimeUnit = "timeformatter.hour";
  73.     } else if (aTime >= 60) {
  74.       formatTime = (aTime * 10) / 60;
  75.       formatTime = Math.floor(formatTime);
  76.       formatTime = formatTime / 10;
  77.       if (formatTime != 1)
  78.         formatTimeUnit = "timeformatter.minutes";
  79.       else
  80.         formatTimeUnit = "timeformatter.minute";
  81.     } else {
  82.       formatTime = Math.floor(aTime);
  83.       if (formatTime != 1)
  84.         formatTimeUnit = "timeformatter.seconds";
  85.       else
  86.         formatTimeUnit = "timeformatter.second";
  87.     }
  88.  
  89.     /* Produce the formatted time string. */
  90.     var stringBundleService = Cc["@mozilla.org/intl/stringbundle;1"]
  91.                                       .getService(Ci.nsIStringBundleService);
  92.     var bundle = stringBundleService.createBundle(this._cfg.localeBundlePath);
  93.     formatTime = bundle.formatStringFromName(formatTimeUnit, [ formatTime ], 1);
  94.  
  95.     return formatTime;
  96.   },
  97.  
  98.   /*
  99.    * formatHMS
  100.    *
  101.    *   ==> aTime                Time (duration) in seconds to format.
  102.    *
  103.    *   <==                      Formatted time string in HH:MM:SS format
  104.    *
  105.    *   Formats a duration to HH:MM:SS format, No wrapping is done, so this isn't
  106.    * simply a 24-hour time display.
  107.    *
  108.    * Example:
  109.    *
  110.    *   formatHMS(32) returns "00:32"
  111.    *   formatHMS(66) returns "01:06"
  112.    *   formatHMS(495732) returns "137:42:12"
  113.    */
  114.   formatHMS: function formatHMS(aTime) {
  115.     function number(x) { return typeof(x) == "number"; }
  116.     function pad(n) { return (n < 10 ? "0" : "") + n; }
  117.  
  118.     if (aTime <= 0)
  119.       return "00:00";
  120.  
  121.     let hours = Math.floor(aTime / 3600);
  122.     let minutes = Math.floor((aTime - hours * 3600) / 60);
  123.     let seconds = Math.floor((aTime - hours * 3600 - minutes * 60));
  124.     return [hours || null, minutes, seconds].filter(number).map(pad).join(":");
  125.   },
  126. }
  127.  
  128.