bug 319790: add a way to calculate the weeknumber, given a calIDateTime. r=jminta

This commit is contained in:
mvl%exedo.nl 2006-02-07 18:47:06 +00:00
parent 3de084e41b
commit efc0ad16fe
7 changed files with 193 additions and 6 deletions

View File

@ -186,18 +186,20 @@
var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService);
var props = sbs.createBundle("chrome://calendar/locale/calendar.properties");
var weekString = props.GetStringFromName("Week");
for (var i = -2; i < 3; i++) {
var date = aDate.clone();
// Add/subtract i number of weeks
date.day = aDate.day + i*7;
date.normalize();
//XXX This isn't quite right, but it's close. We need
// to get some better dateUtils into calDateTime
var weekno = Math.floor ((date.yearday - date.weekday) / 7);
var weekformatter =
Components.classes["@mozilla.org/calendar/weektitle-service;1"]
.getService(Components.interfaces.calIWeekTitleService);
var weekno = weekformatter.getWeekTitle(date);
nameArray.push(weekString + weekno);
var weekTitle =
props.formatStringFromName('WeekTitle', [weekno], 1);
nameArray.push(weekTitle);
}
document.getAnonymousElementByAttribute(this, "anonid", "nav-control").setNames(nameArray);
]]></body>

View File

@ -69,6 +69,7 @@ XPIDLSRCS = calIAlarmService.idl \
calIImportExport.idl \
calITodo.idl \
calIDateTimeFormatter.idl \
calIWeekTitleService.idl \
$(NULL)
EXPORTS = calBaseCID.h

View File

@ -0,0 +1,65 @@
/* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Oracle Corporation code.
*
* The Initial Developer of the Original Code is
* Michiel van Leeuwen <mvl@exedo.nl>
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
interface calIDateTime;
/**
* This interface will calculate a week title from a given datetime. This
* will depends on the users preferences.
* Extensions might override the default implementation, in order to
* generate week titles aimed at special cases (like weeknumbers for a
* schoolyear)
*/
[scriptable, uuid(6bcf8c57-f066-4eba-ab48-950c9fffd9ff)]
interface calIWeekTitleService: nsISupports
{
/**
* Return the week title. It's meant to be displayed.
* (Usually, will return a weeknumber, but might return a string like Q1W4)
*
* @param dateTime
* The dateTime to get the weektitle for
* @returns
* A string, representing the week title. Will usually be the
* week number. Every week (7 days) should get a different string,
* but the switch from one week to the next isn't necessarily
* on sunday.
*/
AString getWeekTitle (in calIDateTime dataTime);
};

View File

@ -86,6 +86,7 @@ EXTRA_COMPONENTS = \
calItemModule.js \
calTodo.js \
calDateTimeFormatter.js \
calWeekTitleService.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -124,7 +124,12 @@ const componentData =
{cid: Components.ID("{4123da9a-f047-42da-a7d0-cc4175b9f36a}"),
contractid: "@mozilla.org/calendar/datetime-formatter;1",
script: "calDateTimeFormatter.js",
constructor: "calDateTimeFormatter"}
constructor: "calDateTimeFormatter"},
{cid: Components.ID("{6877bbdd-f336-46f5-98ce-fe86d0285cc1}"),
contractid: "@mozilla.org/calendar/weektitle-service;1",
script: "calWeekTitleService.js",
constructor: "calWeekTitleService"}
];
var calItemModule = {

View File

@ -0,0 +1,112 @@
/* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Oracle Corporation code.
*
* The Initial Developer of the Original Code is
* Michiel van Leeuwen <mvl@exedo.nl>
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function calWeekTitleService() {
}
calWeekTitleService.prototype.QueryInterface =
function QueryInterface(aIID) {
if (!aIID.equals(Components.interfaces.nsISupports) &&
!aIID.equals(Components.interfaces.calIWeekTitleService)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
};
calWeekTitleService.prototype.getWeekTitle =
function getWeekTitle(aDateTime) {
/**
* This implementation is based on the ISO 8601 standard.
* ISO 8601 defines week one as the first week with at least 4
* days, and defines Monday as the first day of the week.
* Equivalently, the week one is the week with the first Thursday.
*
* This implementation uses the second definition, because it
* enables the user to set a different start-day of the week
* (Sunday instead of Monday is a common setting). If the first
* definition was used, all week-numbers could be off by one
* depending on the week start day. (For example, if weeks start
* on Sunday, a year that starts on Thursday has only 3 days
* [Thu-Sat] in that week, so it would be part of the last week of
* the previous year, but if weeks start on Monday, the year would
* have four days [Thu-Sun] in that week, so it would be counted
* as week 1.)
*/
// The week number is the number of days since the start of week
// one, divided by 7, plus 1 (so the first week is W1, not W0).
// Week one is the week containing the first Thursday.
// Thus, the week number of any day is the same as the number of days
// between the thursday of that week and the thursday of week one,
// divided by 7, plus 1. (This takes care of days at end/start of
// year which may be part of last/first week in other year.)
// The Thursday of a week is the Thursday that follows the first day
// of the week.
//
// The week number of a day is the same as the week number of
// the first day of the week. (This takes care of days near the
// start of the year, which may be part of the week counted in
// the previous year.) So we need the start weekDay.
const SUNDAY = 0;
var startWeekday = SUNDAY; // default to monday per ISO8601 standard.
try {
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
startWeekDay = prefBranch.getIntPref("calendar.week.start");
} catch (e) {}
// The number of days since the start of the week.
// Notice that the result of the substraction might be negative.
// We correct for that by adding 7, and then using the remainder operator.
var sinceStartOfWeek = (aDateTime.weekday - startWeekday + 7) % 7;
// The number of days to thursday is the difference between Thursday
// and the start day of the week (again corrected for negative values)
const THURSDAY = 4;
var startToThursday = (THURSDAY - startWeekday + 7) % 7;
// The yearday number of the thursday this week
var thisWeeksThursday = aDateTime.yearday - sinceStartOfWeek + startToThursday;
// For the first few days of the year, we might still be in week 52.
if (thisWeeksThursday < 0)
thisWeeksThursday += 52*7;
var weekNumber = Math.floor(thisWeeksThursday/7)+1;
return weekNumber;
};

View File

@ -129,6 +129,7 @@ defaulteventalarmunit=minutes
defaulttodoalarmunit=minutes
Week=Week
WeekTitle=Week %1$S
None=None
# Alarm