1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsILocaleService.h"
7 #include "nsDateTimeFormatCID.h"
8 #include "nsIDateTimeFormat.h"
9 #include "nsIScriptableDateFormat.h"
11 #include "nsServiceManagerUtils.h"
13 static NS_DEFINE_CID(kLocaleServiceCID
, NS_LOCALESERVICE_CID
);
14 static NS_DEFINE_CID(kDateTimeFormatCID
, NS_DATETIMEFORMAT_CID
);
16 class nsScriptableDateFormat
: public nsIScriptableDateFormat
{
20 NS_IMETHOD
FormatDateTime(const char16_t
*locale
,
21 nsDateFormatSelector dateFormatSelector
,
22 nsTimeFormatSelector timeFormatSelector
,
29 char16_t
**dateTimeString
);
31 NS_IMETHOD
FormatDate(const char16_t
*locale
,
32 nsDateFormatSelector dateFormatSelector
,
36 char16_t
**dateString
)
37 {return FormatDateTime(locale
, dateFormatSelector
, kTimeFormatNone
,
38 year
, month
, day
, 0, 0, 0, dateString
);}
40 NS_IMETHOD
FormatTime(const char16_t
*locale
,
41 nsTimeFormatSelector timeFormatSelector
,
45 char16_t
**timeString
)
46 {return FormatDateTime(locale
, kDateFormatNone
, timeFormatSelector
,
47 1999, 1, 1, hour
, minute
, second
, timeString
);}
49 nsScriptableDateFormat() {}
54 virtual ~nsScriptableDateFormat() {}
57 NS_IMPL_ISUPPORTS(nsScriptableDateFormat
, nsIScriptableDateFormat
)
59 NS_IMETHODIMP
nsScriptableDateFormat::FormatDateTime(
60 const char16_t
*aLocale
,
61 nsDateFormatSelector dateFormatSelector
,
62 nsTimeFormatSelector timeFormatSelector
,
69 char16_t
**dateTimeString
)
71 // We can't have a valid date with the year, month or day
72 // being lower than 1.
73 if (year
< 1 || month
< 1 || day
< 1)
74 return NS_ERROR_INVALID_ARG
;
77 nsAutoString
localeName(aLocale
);
78 *dateTimeString
= nullptr;
80 nsCOMPtr
<nsILocale
> locale
;
81 // re-initialise locale pointer only if the locale was given explicitly
82 if (!localeName
.IsEmpty()) {
84 nsCOMPtr
<nsILocaleService
> localeService(do_GetService(kLocaleServiceCID
, &rv
));
85 NS_ENSURE_SUCCESS(rv
, rv
);
87 rv
= localeService
->NewLocale(localeName
, getter_AddRefs(locale
));
88 NS_ENSURE_SUCCESS(rv
, rv
);
91 nsCOMPtr
<nsIDateTimeFormat
> dateTimeFormat(do_CreateInstance(kDateTimeFormatCID
, &rv
));
92 NS_ENSURE_SUCCESS(rv
, rv
);
97 memset(&tmTime
, 0, sizeof(tmTime
));
98 tmTime
.tm_year
= year
- 1900;
99 tmTime
.tm_mon
= month
- 1;
100 tmTime
.tm_mday
= day
;
101 tmTime
.tm_hour
= hour
;
102 tmTime
.tm_min
= minute
;
103 tmTime
.tm_sec
= second
;
104 tmTime
.tm_yday
= tmTime
.tm_wday
= 0;
105 tmTime
.tm_isdst
= -1;
106 timetTime
= mktime(&tmTime
);
108 if ((time_t)-1 != timetTime
) {
109 rv
= dateTimeFormat
->FormatTime(locale
, dateFormatSelector
, timeFormatSelector
,
110 timetTime
, mStringOut
);
113 // if mktime fails (e.g. year <= 1970), then try NSPR.
116 sprintf(string
, "%.2d/%.2d/%d %.2d:%.2d:%.2d", month
, day
, year
, hour
, minute
, second
);
117 if (PR_SUCCESS
!= PR_ParseTimeString(string
, false, &prtime
))
118 return NS_ERROR_INVALID_ARG
;
120 rv
= dateTimeFormat
->FormatPRTime(locale
, dateFormatSelector
, timeFormatSelector
,
123 if (NS_SUCCEEDED(rv
))
124 *dateTimeString
= ToNewUnicode(mStringOut
);
130 NS_NewScriptableDateFormat(nsISupports
* aOuter
, REFNSIID aIID
, void** aResult
)
133 return NS_ERROR_NO_AGGREGATION
;
135 nsScriptableDateFormat
* result
= new nsScriptableDateFormat();
137 return NS_ERROR_OUT_OF_MEMORY
;
140 nsresult rv
= result
->QueryInterface(aIID
, aResult
);