2001-09-26 00:40:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-01-21 23:40:32 +00:00
|
|
|
|
2011-05-11 05:25:09 +00:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
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 12:24:15 +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-07-26 00:20:50 +00:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "plstr.h"
|
2002-01-16 03:31:25 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2009-03-16 12:24:15 +00:00
|
|
|
#include "nsTArray.h"
|
1999-01-21 23:40:32 +00:00
|
|
|
|
|
|
|
|
2013-07-19 02:23:32 +00:00
|
|
|
NS_IMPL_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
|
2012-07-30 14:20:58 +00:00
|
|
|
if (nullptr == locale) {
|
2003-10-30 05:04:45 +00:00
|
|
|
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
|
2012-07-30 14:20:58 +00:00
|
|
|
if (nullptr == 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 12:24:15 +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 12:24:15 +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
|
|
|
|
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 12:24:15 +00:00
|
|
|
// Got the locale for the formatter:
|
|
|
|
CFLocaleRef formatterLocale;
|
|
|
|
if (!locale) {
|
|
|
|
formatterLocale = CFLocaleCopyCurrent();
|
|
|
|
} else {
|
2013-08-23 19:51:30 +00:00
|
|
|
CFStringRef localeStr = CFStringCreateWithCharacters(nullptr,
|
|
|
|
mLocale.get(),
|
|
|
|
mLocale.Length());
|
|
|
|
formatterLocale = CFLocaleCreate(nullptr, localeStr);
|
2009-03-16 12:24:15 +00:00
|
|
|
CFRelease(localeStr);
|
2009-03-16 12:21:55 +00:00
|
|
|
}
|
2009-03-16 12:24:15 +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 12:24:15 +00:00
|
|
|
dateStyle = kCFDateFormatterLongStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
break;
|
|
|
|
case kDateFormatShort:
|
2009-03-16 12:24:15 +00:00
|
|
|
dateStyle = kCFDateFormatterShortStyle;
|
1999-02-03 01:36:06 +00:00
|
|
|
break;
|
|
|
|
case kDateFormatYearMonth:
|
2009-03-16 12:21:55 +00:00
|
|
|
case kDateFormatWeekday:
|
2009-03-16 12:24:15 +00:00
|
|
|
dateStyle = kCFDateFormatterNoStyle; // formats handled below
|
2009-03-16 11:59:33 +00:00
|
|
|
break;
|
2009-03-16 12:24:15 +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 12:24:15 +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 12:24:15 +00:00
|
|
|
|
|
|
|
// Create the formatter and fix up its formatting as necessary:
|
|
|
|
CFDateFormatterRef formatter =
|
2013-08-23 19:51:30 +00:00
|
|
|
CFDateFormatterCreate(nullptr, formatterLocale, dateStyle, timeStyle);
|
2009-03-16 12:24:15 +00:00
|
|
|
|
|
|
|
CFRelease(formatterLocale);
|
|
|
|
|
|
|
|
if (dateFormatSelector == kDateFormatYearMonth ||
|
|
|
|
dateFormatSelector == kDateFormatWeekday) {
|
|
|
|
CFStringRef dateFormat =
|
2009-03-20 23:38:47 +00:00
|
|
|
dateFormatSelector == kDateFormatYearMonth ? CFSTR("yyyy/MM ") : CFSTR("EEE ");
|
2009-03-16 12:24:15 +00:00
|
|
|
|
|
|
|
CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
|
2013-08-23 19:51:30 +00:00
|
|
|
CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat);
|
2009-03-16 12:24:15 +00:00
|
|
|
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 12:24:15 +00:00
|
|
|
|
|
|
|
if (timeFormatSelector == kTimeFormatSecondsForce24Hour ||
|
|
|
|
timeFormatSelector == kTimeFormatNoSecondsForce24Hour) {
|
|
|
|
// Replace "h" with "H", and remove "a":
|
|
|
|
CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
|
2013-08-23 19:51:30 +00:00
|
|
|
CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat);
|
2009-03-16 12:24:15 +00:00
|
|
|
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 12:24:15 +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);
|
|
|
|
|
2013-08-23 19:51:30 +00:00
|
|
|
CFStringRef formattedDate = CFDateFormatterCreateStringWithAbsoluteTime(nullptr,
|
|
|
|
formatter,
|
|
|
|
absTime);
|
2009-03-16 12:24:15 +00:00
|
|
|
|
|
|
|
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 12:24:15 +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);
|
|
|
|
}
|
|
|
|
|