2001-09-26 00:40:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:21:17 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-01-21 23:40:32 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* 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/
|
1999-01-21 23:40:32 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* 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.
|
1999-01-21 23:40:32 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 00:40:45 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* Contributor(s):
|
2000-02-02 22:24:56 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-26 00:40:45 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:21:17 +00:00
|
|
|
* either of 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"),
|
2001-09-26 00:40:45 +00:00
|
|
|
* 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
|
2004-04-18 14:21:17 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 00:40:45 +00:00
|
|
|
* 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
|
2004-04-18 14:21:17 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 00:40:45 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-01-21 23:40:32 +00:00
|
|
|
|
1999-02-03 01:36:06 +00:00
|
|
|
#include "nsIServiceManager.h"
|
1999-01-21 23:40:32 +00:00
|
|
|
#include "nsDateTimeFormatMac.h"
|
2009-03-16 11:59:33 +00:00
|
|
|
#include <CoreFoundation/CFDateFormatter.h>
|
1999-05-04 20:42:58 +00:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsLocaleCID.h"
|
1999-09-10 18:34:06 +00:00
|
|
|
#include "nsILocaleService.h"
|
1999-05-04 20:42:58 +00:00
|
|
|
#include "nsIMacLocale.h"
|
1999-07-26 00:20:50 +00:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "plstr.h"
|
1999-09-10 18:34:06 +00:00
|
|
|
#include "prmem.h"
|
2002-01-16 03:31:25 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2009-03-16 11:59:33 +00:00
|
|
|
#include "nsTArray.h"
|
1999-01-21 23:40:32 +00:00
|
|
|
|
|
|
|
|
2003-09-07 22:24:21 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDateTimeFormatMac, nsIDateTimeFormat)
|
1999-01-21 23:40:32 +00:00
|
|
|
|
1999-09-10 18:34:06 +00:00
|
|
|
nsresult nsDateTimeFormatMac::Initialize(nsILocale* locale)
|
|
|
|
{
|
2003-10-30 05:04:45 +00:00
|
|
|
nsAutoString localeStr;
|
|
|
|
nsAutoString category(NS_LITERAL_STRING("NSILOCALE_TIME"));
|
1999-09-10 18:34:06 +00:00
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
// use cached info if match with stored locale
|
2003-10-30 05:04:45 +00:00
|
|
|
if (nsnull == locale) {
|
|
|
|
if (!mLocale.IsEmpty() &&
|
2002-03-19 07:26:32 +00:00
|
|
|
mLocale.Equals(mAppLocale, nsCaseInsensitiveStringComparator())) {
|
1999-09-10 18:34:06 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-10-30 05:04:45 +00:00
|
|
|
res = locale->GetCategory(category, localeStr);
|
|
|
|
if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
|
2002-01-16 03:31:25 +00:00
|
|
|
if (!mLocale.IsEmpty() &&
|
2003-10-30 05:04:45 +00:00
|
|
|
mLocale.Equals(localeStr,
|
2002-01-16 03:31:25 +00:00
|
|
|
nsCaseInsensitiveStringComparator())) {
|
1999-09-10 18:34:06 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-08 20:44:12 +00:00
|
|
|
// get application locale
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsILocaleService> localeService =
|
2001-08-17 05:52:48 +00:00
|
|
|
do_GetService(NS_LOCALESERVICE_CONTRACTID, &res);
|
2000-08-08 20:44:12 +00:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
2003-10-30 05:04:45 +00:00
|
|
|
nsCOMPtr<nsILocale> appLocale;
|
|
|
|
res = localeService->GetApplicationLocale(getter_AddRefs(appLocale));
|
1999-09-10 18:34:06 +00:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
2003-10-30 05:04:45 +00:00
|
|
|
res = appLocale->GetCategory(category, localeStr);
|
|
|
|
if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
|
|
|
|
mAppLocale = localeStr; // cache app locale name
|
1999-09-10 18:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-08-08 20:44:12 +00:00
|
|
|
|
|
|
|
// use app default if no locale specified
|
2003-10-30 05:04:45 +00:00
|
|
|
if (nsnull == locale) {
|
2000-08-08 20:44:12 +00:00
|
|
|
mUseDefaultLocale = true;
|
|
|
|
}
|
1999-09-10 18:34:06 +00:00
|
|
|
else {
|
2000-08-08 20:44:12 +00:00
|
|
|
mUseDefaultLocale = false;
|
2003-10-30 05:04:45 +00:00
|
|
|
res = locale->GetCategory(category, localeStr);
|
1999-09-10 18:34:06 +00:00
|
|
|
}
|
2000-08-08 20:44:12 +00:00
|
|
|
|
2003-10-30 06:43:37 +00:00
|
|
|
if (NS_SUCCEEDED(res) && !localeStr.IsEmpty()) {
|
2003-10-30 05:04:45 +00:00
|
|
|
mLocale.Assign(localeStr); // cache locale name
|
1999-09-10 18:34:06 +00:00
|
|
|
}
|
2001-02-22 23:27:53 +00:00
|
|
|
|
1999-09-10 18:34:06 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-02-01 23:40:02 +00:00
|
|
|
// performs a locale sensitive date formatting operation on the time_t parameter
|
1999-02-03 01:36:06 +00:00
|
|
|
nsresult nsDateTimeFormatMac::FormatTime(nsILocale* locale,
|
1999-01-21 23:40:32 +00:00
|
|
|
const nsDateFormatSelector dateFormatSelector,
|
|
|
|
const nsTimeFormatSelector timeFormatSelector,
|
|
|
|
const time_t timetTime,
|
2006-05-22 13:20:59 +00:00
|
|
|
nsAString& stringOut)
|
1999-01-21 23:40:32 +00:00
|
|
|
{
|
2009-03-16 11:59:33 +00:00
|
|
|
struct tm tmTime;
|
|
|
|
return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, localtime_r(&timetTime, &tmTime), stringOut);
|
2002-07-25 00:28:29 +00:00
|
|
|
}
|
|
|
|
|
1999-01-21 23:40:32 +00:00
|
|
|
// performs a locale sensitive date formatting operation on the struct tm parameter
|
1999-02-03 01:36:06 +00:00
|
|
|
nsresult nsDateTimeFormatMac::FormatTMTime(nsILocale* locale,
|
1999-02-01 23:40:02 +00:00
|
|
|
const nsDateFormatSelector dateFormatSelector,
|
|
|
|
const nsTimeFormatSelector timeFormatSelector,
|
|
|
|
const struct tm* tmTime,
|
2006-05-22 13:20:59 +00:00
|
|
|
nsAString& stringOut)
|
1999-01-21 23:40:32 +00:00
|
|
|
{
|
2009-03-16 11:59:33 +00:00
|
|
|
nsresult res = NS_OK;
|
1999-02-03 01:36:06 +00:00
|
|
|
|
1999-09-10 18:34:06 +00:00
|
|
|
// set up locale data
|
|
|
|
(void) Initialize(locale);
|
|
|
|
|
1999-02-03 19:28:38 +00:00
|
|
|
// return, nothing to format
|
1999-02-03 01:36:06 +00:00
|
|
|
if (dateFormatSelector == kDateFormatNone && timeFormatSelector == kTimeFormatNone) {
|
2006-05-22 13:20:59 +00:00
|
|
|
stringOut.Truncate();
|
1999-02-03 01:36:06 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-05-04 20:42:58 +00:00
|
|
|
|
2006-05-22 13:20:59 +00:00
|
|
|
// set the default string, in case for API/conversion errors
|
|
|
|
CopyASCIItoUTF16(nsDependentCString(asctime(tmTime)), stringOut);
|
1999-02-03 01:36:06 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(tmTime->tm_mon >= 0, "tm is not set correctly");
|
1999-07-16 23:52:06 +00:00
|
|
|
NS_ASSERTION(tmTime->tm_mday >= 1, "tm is not set correctly");
|
1999-02-03 01:36:06 +00:00
|
|
|
NS_ASSERTION(tmTime->tm_hour >= 0, "tm is not set correctly");
|
|
|
|
NS_ASSERTION(tmTime->tm_min >= 0, "tm is not set correctly");
|
|
|
|
NS_ASSERTION(tmTime->tm_sec >= 0, "tm is not set correctly");
|
|
|
|
NS_ASSERTION(tmTime->tm_wday >= 0, "tm is not set correctly");
|
|
|
|
|
2009-03-16 11:59:33 +00:00
|
|
|
// Got the locale for the formatter:
|
|
|
|
CFLocaleRef formatterLocale;
|
|
|
|
if (!locale) {
|
|
|
|
formatterLocale = CFLocaleCopyCurrent();
|
|
|
|
} else {
|
|
|
|
CFStringRef localeStr = CFStringCreateWithCharacters(NULL, mLocale.get(), mLocale.Length());
|
|
|
|
formatterLocale = CFLocaleCreate(NULL, localeStr);
|
|
|
|
CFRelease(localeStr);
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
2009-03-16 11:59:33 +00:00
|
|
|
|
|
|
|
// Get the date style for the formatter:
|
|
|
|
CFDateFormatterStyle dateStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
switch (dateFormatSelector) {
|
|
|
|
case kDateFormatLong:
|
2009-03-16 11:59:33 +00:00
|
|
|
dateStyle = kCFDateFormatterLongStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
break;
|
|
|
|
case kDateFormatShort:
|
2009-03-16 11:59:33 +00:00
|
|
|
dateStyle = kCFDateFormatterShortStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
break;
|
|
|
|
case kDateFormatYearMonth:
|
|
|
|
case kDateFormatWeekday:
|
2009-03-16 11:59:33 +00:00
|
|
|
dateStyle = kCFDateFormatterNoStyle; // formats handled below
|
1999-02-03 01:36:06 +00:00
|
|
|
break;
|
2009-03-16 11:59:33 +00:00
|
|
|
case kDateFormatNone:
|
|
|
|
dateStyle = kCFDateFormatterNoStyle;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown nsDateFormatSelector");
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
dateStyle = kCFDateFormatterNoStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 11:59:33 +00:00
|
|
|
// Get the time style for the formatter:
|
|
|
|
CFDateFormatterStyle timeStyle;
|
|
|
|
switch (timeFormatSelector) {
|
|
|
|
case kTimeFormatSeconds:
|
|
|
|
case kTimeFormatSecondsForce24Hour: // 24 hour part fixed below
|
|
|
|
timeStyle = kCFDateFormatterMediumStyle;
|
|
|
|
break;
|
|
|
|
case kTimeFormatNoSeconds:
|
|
|
|
case kTimeFormatNoSecondsForce24Hour: // 24 hour part fixed below
|
|
|
|
timeStyle = kCFDateFormatterShortStyle;
|
|
|
|
break;
|
|
|
|
case kTimeFormatNone:
|
|
|
|
timeStyle = kCFDateFormatterNoStyle;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown nsTimeFormatSelector");
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
timeStyle = kCFDateFormatterNoStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
2009-03-16 11:59:33 +00:00
|
|
|
|
|
|
|
// Create the formatter and fix up its formatting as necessary:
|
|
|
|
CFDateFormatterRef formatter =
|
|
|
|
CFDateFormatterCreate(NULL, formatterLocale, dateStyle, timeStyle);
|
|
|
|
|
|
|
|
CFRelease(formatterLocale);
|
|
|
|
|
|
|
|
if (dateFormatSelector == kDateFormatYearMonth ||
|
|
|
|
dateFormatSelector == kDateFormatWeekday) {
|
|
|
|
CFStringRef dateFormat =
|
|
|
|
dateFormatSelector == kDateFormatYearMonth ? CFSTR("yy/MM ") : CFSTR("EEE ");
|
|
|
|
|
|
|
|
CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
|
|
|
|
CFMutableStringRef newFormat = CFStringCreateMutableCopy(NULL, 0, oldFormat);
|
|
|
|
CFStringInsert(newFormat, 0, dateFormat);
|
|
|
|
CFDateFormatterSetFormat(formatter, newFormat);
|
|
|
|
CFRelease(newFormat); // note we don't own oldFormat
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
2009-03-16 11:59:33 +00:00
|
|
|
|
|
|
|
if (timeFormatSelector == kTimeFormatSecondsForce24Hour ||
|
|
|
|
timeFormatSelector == kTimeFormatNoSecondsForce24Hour) {
|
|
|
|
// Replace "h" with "H", and remove "a":
|
|
|
|
CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
|
|
|
|
CFMutableStringRef newFormat = CFStringCreateMutableCopy(NULL, 0, oldFormat);
|
|
|
|
CFIndex replaceCount = CFStringFindAndReplace(newFormat,
|
|
|
|
CFSTR("h"), CFSTR("H"),
|
|
|
|
CFRangeMake(0, CFStringGetLength(newFormat)),
|
|
|
|
0);
|
|
|
|
NS_ASSERTION(replaceCount == 1, "Unexpected number of \"h\" occurrences");
|
|
|
|
replaceCount = CFStringFindAndReplace(newFormat,
|
|
|
|
CFSTR("a"), CFSTR(""),
|
|
|
|
CFRangeMake(0, CFStringGetLength(newFormat)),
|
|
|
|
0);
|
|
|
|
NS_ASSERTION(replaceCount == 1, "Unexpected number of \"a\" occurrences");
|
|
|
|
CFDateFormatterSetFormat(formatter, newFormat);
|
|
|
|
CFRelease(newFormat); // note we don't own oldFormat
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
2009-03-16 11:59:33 +00:00
|
|
|
|
|
|
|
// Now get the formatted date:
|
|
|
|
CFGregorianDate date;
|
|
|
|
date.second = tmTime->tm_sec;
|
|
|
|
date.minute = tmTime->tm_min;
|
|
|
|
date.hour = tmTime->tm_hour;
|
|
|
|
date.day = tmTime->tm_mday; // Mac is 1-based, tm is 1-based
|
|
|
|
date.month = tmTime->tm_mon + 1; // Mac is 1-based, tm is 0-based
|
|
|
|
date.year = tmTime->tm_year + 1900;
|
|
|
|
|
|
|
|
CFTimeZoneRef timeZone = CFTimeZoneCopySystem(); // tmTime is in local time
|
|
|
|
CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime(date, timeZone);
|
|
|
|
CFRelease(timeZone);
|
|
|
|
|
|
|
|
CFStringRef formattedDate = CFDateFormatterCreateStringWithAbsoluteTime(NULL, formatter, absTime);
|
|
|
|
|
|
|
|
CFIndex stringLen = CFStringGetLength(formattedDate);
|
|
|
|
|
|
|
|
nsAutoTArray<UniChar, 256> stringBuffer;
|
|
|
|
if (stringBuffer.SetLength(stringLen + 1)) {
|
|
|
|
CFStringGetCharacters(formattedDate, CFRangeMake(0, stringLen), stringBuffer.Elements());
|
|
|
|
stringOut.Assign(stringBuffer.Elements(), stringLen);
|
1999-02-03 01:36:06 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 11:59:33 +00:00
|
|
|
CFRelease(formattedDate);
|
|
|
|
CFRelease(formatter);
|
|
|
|
|
1999-02-03 01:36:06 +00:00
|
|
|
return res;
|
1999-01-21 23:40:32 +00:00
|
|
|
}
|
1999-07-14 16:53:17 +00:00
|
|
|
|
|
|
|
// performs a locale sensitive date formatting operation on the PRTime parameter
|
|
|
|
nsresult nsDateTimeFormatMac::FormatPRTime(nsILocale* locale,
|
|
|
|
const nsDateFormatSelector dateFormatSelector,
|
|
|
|
const nsTimeFormatSelector timeFormatSelector,
|
|
|
|
const PRTime prTime,
|
2006-05-22 13:20:59 +00:00
|
|
|
nsAString& stringOut)
|
1999-07-14 16:53:17 +00:00
|
|
|
{
|
|
|
|
PRExplodedTime explodedTime;
|
|
|
|
PR_ExplodeTime(prTime, PR_LocalTimeParameters, &explodedTime);
|
|
|
|
|
|
|
|
return FormatPRExplodedTime(locale, dateFormatSelector, timeFormatSelector, &explodedTime, stringOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
// performs a locale sensitive date formatting operation on the PRExplodedTime parameter
|
|
|
|
nsresult nsDateTimeFormatMac::FormatPRExplodedTime(nsILocale* locale,
|
|
|
|
const nsDateFormatSelector dateFormatSelector,
|
|
|
|
const nsTimeFormatSelector timeFormatSelector,
|
|
|
|
const PRExplodedTime* explodedTime,
|
2006-05-22 13:20:59 +00:00
|
|
|
nsAString& stringOut)
|
1999-07-14 16:53:17 +00:00
|
|
|
{
|
|
|
|
struct tm tmTime;
|
2002-02-05 01:41:13 +00:00
|
|
|
memset( &tmTime, 0, sizeof(tmTime) );
|
1999-07-26 00:20:50 +00:00
|
|
|
|
1999-07-15 23:15:31 +00:00
|
|
|
tmTime.tm_yday = explodedTime->tm_yday;
|
|
|
|
tmTime.tm_wday = explodedTime->tm_wday;
|
|
|
|
tmTime.tm_year = explodedTime->tm_year;
|
|
|
|
tmTime.tm_year -= 1900;
|
1999-07-14 16:53:17 +00:00
|
|
|
tmTime.tm_mon = explodedTime->tm_month;
|
|
|
|
tmTime.tm_mday = explodedTime->tm_mday;
|
|
|
|
tmTime.tm_hour = explodedTime->tm_hour;
|
|
|
|
tmTime.tm_min = explodedTime->tm_min;
|
|
|
|
tmTime.tm_sec = explodedTime->tm_sec;
|
|
|
|
|
|
|
|
return FormatTMTime(locale, dateFormatSelector, timeFormatSelector, &tmTime, stringOut);
|
|
|
|
}
|
|
|
|
|