home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / calendar_windows_latest.xpi / components / calDateTimeFormatter.js < prev    next >
Encoding:
Text File  |  2006-01-08  |  9.2 KB  |  240 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is OEone Calendar Code, released October 31st, 2001.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  *  OEone Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *  Garth Smedley <garths@oeone.com>
  23.  *  Mike Potter <mikep@oeone.com>
  24.  *  Eric Belhaire <belhaire@ief.u-psud.fr>
  25.  *  Michiel van Leeuwen <mvl@exedo.nl>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat;
  42.  
  43. function calDateTimeFormatter() {
  44.     var strBundleService = 
  45.         Components.classes["@mozilla.org/intl/stringbundle;1"]
  46.                   .getService(Components.interfaces.nsIStringBundleService);
  47.     this.mDateStringBundle =  strBundleService.createBundle("chrome://calendar/locale/dateFormat.properties");
  48.  
  49.     this.mDateService = 
  50.         Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
  51.                   .getService(nsIScriptableDateFormat);
  52.  
  53.     // If LONG FORMATTED DATE is same as short formatted date,
  54.     // then OS has poor extended/long date config, so use workaround.
  55.     this.mUseLongDateService = true;
  56.     var probeDate = 
  57.         Components.classes["@mozilla.org/calendar/datetime;1"]
  58.                   .createInstance(Components.interfaces.calIDateTime);
  59.     probeDate.jsDate = new Date(2002,3,4);
  60.     try { 
  61.         var longProbeString = this.formatDateLong(probeDate);
  62.         // On Unix extended/long date format may be created using %Ex instead
  63.         // of %x. Some systems may not support it and return "Ex" or same as
  64.         // short string. In that case, don't use long date service, use a
  65.         // workaround hack instead.
  66.         if (longProbeString == null ||
  67.             longProbeString.length < 4 ||
  68.             longProbeString == probeString)
  69.            this.mUseLongDateService = false;
  70.     } catch (e) {
  71.         this.mUseLongDateService = false;
  72.     }
  73.  
  74. }
  75.  
  76. calDateTimeFormatter.prototype.QueryInterface =
  77. function QueryInterface(aIID) {
  78.     if (!aIID.equals(Components.interfaces.nsISupports) &&
  79.         !aIID.equals(Components.interfaces.calIDateTimeFormatter)) {
  80.         throw Components.results.NS_ERROR_NO_INTERFACE;
  81.     }
  82.  
  83.     return this;
  84. };
  85.  
  86. calDateTimeFormatter.prototype.formatDate =
  87. function formatDate(aDate) {
  88.     // Format the date using user's format preference (long or short)
  89.     var format;
  90.     var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
  91.                                 .getService(Components.interfaces.nsIPrefBranch);
  92.     try {     
  93.         format = prefBranch.getIntPref("calendar.date.format");
  94.     } catch(e) {
  95.         format = 0;
  96.     }
  97.  
  98.     if (format == 0)
  99.         return this.formatDateLong(aDate);
  100.     else
  101.         return this.formatDateShort(aDate);
  102. };
  103.  
  104. calDateTimeFormatter.prototype.formatDateShort =
  105. function formatDateShort(aDate) {
  106.     return this.mDateService.FormatDate("",
  107.                                         nsIScriptableDateFormat.dateFormatShort,
  108.                                         aDate.year,
  109.                                         aDate.month + 1,
  110.                                         aDate.day);
  111. };
  112.  
  113. calDateTimeFormatter.prototype.formatDateLong =
  114. function formatDateLong(aDate) {
  115.     if (this.mUseLongDateService) {
  116.         return this.mDateService.FormatDate("",
  117.                                             nsIScriptableDateFormat.dateFormatLong,
  118.                                             aDate.year,
  119.                                             aDate.month + 1,
  120.                                             aDate.day);
  121.     } else {
  122.         // HACK We are probably on Linux and want a string in long format.
  123.         // dateService.dateFormatLong on Linux may return a short string, so
  124.         // build our own.
  125.         return this.shortDayName(aDate.weekday) + " " +
  126.                aDate.day + " " +
  127.                this.shortMonthName(aDate.month) + " " +
  128.                aDate.year;
  129.     }
  130. };
  131.  
  132. calDateTimeFormatter.prototype.formatDateWithoutYear =
  133. function formatDateWithoutYear(aDate) {
  134.     // XXX Using a hardcoded format, because nsIScriptableDateFormat doesn't
  135.     //     have a way to leave out the year.
  136.     return this.shortMonthName(aDate.month) + " " + aDate.day;
  137. };
  138.  
  139.  
  140. calDateTimeFormatter.prototype.formatTime =
  141. function formatTime(aDate) {
  142.     return this.mDateService.FormatTime("",
  143.                                         nsIScriptableDateFormat.timeFormatNoSeconds,
  144.                                         aDate.hour,
  145.                                         aDate.minute,
  146.                                         0);
  147. };
  148.  
  149. calDateTimeFormatter.prototype.formatDateTime =
  150. function formatDateTime(aDate) {
  151.     var formattedDate = this.formatDate(aDate);
  152.     var formattedTime = this.formatTime(aDate);
  153.  
  154.     var timeBeforeDate;
  155.     var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
  156.                                 .getService(Components.interfaces.nsIPrefBranch);
  157.     try {     
  158.         timeBeforeDate = prefBranch.getBoolPref("calendar.date.formatTimeBeforeDate");
  159.     } catch(e) {
  160.         timeBeforeDate = 0;
  161.     }
  162.  
  163.     if (timeBeforeDate)
  164.         return formattedTime+" "+formattedDate;
  165.     else
  166.         return formattedDate+" "+formattedTime;
  167. };
  168.  
  169. calDateTimeFormatter.prototype.formatInterval =
  170. function formatInterval(aStartDate, aEndDate, aStartString, aEndString) {
  171.     endDate = aEndDate.clone();
  172.     // EndDate is exclusive. For all-day events, we ened to substract one day,
  173.     // to get into a format that's understandable.
  174.     if (aStartDate.isDate) {
  175.         endDate.day -= 1;
  176.         endDate.normalize();
  177.     }
  178.  
  179.     testdate = aStartDate.clone();
  180.     testdate.isDate = true;
  181.     testdate.normalize();
  182.     var sameDay = (testdate.compare(endDate) == 0);
  183.     
  184.     if (aStartDate.isDate) {
  185.         // All-day interval, so we should leave out the time part
  186.         if (sameDay) {
  187.             // Start and end on the same day: only give return the start
  188.             // date.
  189.             // "5 Jan 2006"
  190.             aStartString.value = this.formatDate(aStartDate);
  191.             aEndString.value = "";
  192.         } else {
  193.             // Spanning multiple days, return both dates
  194.             // "5 Jan 2006 - 7 Jan 2006"
  195.             aStartString.value = this.formatDate(aStartDate);
  196.             aEndString.value = this.formatDate(endDate);
  197.         }
  198.     } else {
  199.         // non-allday, so need to return date and time
  200.         if (sameDay) {
  201.             // End is on the same day as start, so we can leave out the
  202.             // end date (but still include end time)
  203.             // "5 Jan 2006 13:00 - 17:00"
  204.             aStartString.value = this.formatDate(aStartDate)+" "+this.formatTime(aStartDate);
  205.             aEndString.value = this.formatTime(endDate);
  206.         } else {
  207.             // Spanning multiple days, so need to include date and time
  208.             // for start and end
  209.             // "5 Jan 2006 13:00 - 7 Jan 2006 9:00"
  210.             aStartString.value = this.formatDateTime(aStartDate);
  211.             aEndString.value = this.formatDateTime(endDate);
  212.         }
  213.     }
  214.     return 1;
  215. };
  216.  
  217. calDateTimeFormatter.prototype.monthName =
  218. function monthName(aMonthIndex) {
  219.     var oneBasedMonthIndex = aMonthIndex + 1;
  220.     return this.mDateStringBundle.GetStringFromName("month." + oneBasedMonthIndex + ".name" );
  221. };
  222.  
  223. calDateTimeFormatter.prototype.shortMonthName =
  224. function shortMonthName(aMonthIndex) {
  225.     var oneBasedMonthIndex = aMonthIndex + 1;
  226.     return this.mDateStringBundle.GetStringFromName("month." + oneBasedMonthIndex + ".Mmm" );
  227. };
  228.  
  229. calDateTimeFormatter.prototype.dayName =
  230. function monthName(aDayIndex) {
  231.     var oneBasedDayIndex = aDayIndex + 1;
  232.     return this.mDateStringBundle.GetStringFromName("day." + oneBasedDayIndex + ".name" );
  233. };
  234.  
  235. calDateTimeFormatter.prototype.shortDayName =
  236. function shortMonthName(aDayIndex) {
  237.     var oneBasedDayIndex = aDayIndex + 1;
  238.     return this.mDateStringBundle.GetStringFromName("day." + oneBasedDayIndex + ".Mmm" );
  239. };
  240.