|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: formatternative |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 //#include <avkon.rsg> |
|
20 #include <memory> |
|
21 #include <stringresourcereader.h> |
|
22 |
|
23 #include "com_nokia_mj_impl_utils_Formatter.h" |
|
24 #include "com_nokia_mj_impl_utils_ResourceLoader.h" |
|
25 #include "javajniutils.h" |
|
26 #include "logger.h" |
|
27 |
|
28 enum EDateTimeFormat |
|
29 { |
|
30 DATE_LONG = 1, |
|
31 DATE_SHORT = 2, |
|
32 /*TIME_SHORT = 3, |
|
33 TIME_LONG = 4, |
|
34 DATETIME_SHORT = 5, |
|
35 DATETIME_LONG = 6*/ |
|
36 }; |
|
37 |
|
38 const TUint JavaUpperTimeFor1970 = 14474675; |
|
39 const TUint JavaLowerTimeFor1970 = 254771200; |
|
40 |
|
41 // const TInt KMaxDateTimeStringSize = 50; |
|
42 const TInt KMaxDateFormatSize = 30; |
|
43 const TInt KMaxNumberFormatSize = 40; |
|
44 |
|
45 // _LIT( KAvkonResFile, "z:\\resource\\avkon.rsc" ); |
|
46 |
|
47 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_Formatter__1formatInteger |
|
48 (JNIEnv *aJni, jobject, jint aNumber) |
|
49 { |
|
50 JELOG2(EUtils); |
|
51 TReal64 realNumber = aNumber; |
|
52 std::auto_ptr<HBufC> numberString(HBufC::New(KMaxNumberFormatSize)); |
|
53 if (numberString.get() == 0) |
|
54 { |
|
55 return 0; |
|
56 } |
|
57 TPtr numberPtr = numberString->Des(); |
|
58 |
|
59 TRealFormat numberFormat; |
|
60 |
|
61 TInt error = numberPtr.Num(realNumber, numberFormat); |
|
62 if (error < KErrNone) |
|
63 { |
|
64 WLOG2(EUtils, |
|
65 "Cannot format %d to current locale. Error: %d", aNumber, error); |
|
66 } |
|
67 |
|
68 return aJni->NewString( |
|
69 (const jchar*)numberPtr.Ptr(), numberPtr.Length()); |
|
70 } |
|
71 |
|
72 JNIEXPORT jstring JNICALL Java_com_nokia_mj_impl_utils_Formatter__1formatDate |
|
73 (JNIEnv * aJni, jobject, jlong timeInMillis) |
|
74 { |
|
75 std::auto_ptr<HBufC> dateString(HBufC::New(KMaxDateFormatSize)); |
|
76 if (dateString.get() == 0) |
|
77 { |
|
78 return 0; |
|
79 } |
|
80 TPtr datePtr(dateString->Des()); |
|
81 TBuf<KMaxDateFormatSize> dateStringBuf; |
|
82 |
|
83 // Java Date object is calculated by millisecs from 1.1.1970 0:00:00 GMT |
|
84 // Need conversion for Symbian TTime |
|
85 TInt64 timeNum = *reinterpret_cast<TInt64*>(&timeInMillis); |
|
86 TInt64 timeBeginNum = |
|
87 MAKE_TINT64(JavaUpperTimeFor1970, JavaLowerTimeFor1970); |
|
88 |
|
89 TTime timeBegin(timeBeginNum); |
|
90 TTimeIntervalMicroSeconds delta(timeNum * 1000); |
|
91 TTime time = timeBegin + delta; |
|
92 |
|
93 _LIT(KTestFormat, "%/0%1%/1%2%/2%3%/3"); |
|
94 TRAP_IGNORE(time.FormatL(dateStringBuf, KTestFormat)); |
|
95 datePtr.Append(dateStringBuf); |
|
96 |
|
97 return aJni->NewString( |
|
98 (const jchar*)datePtr.Ptr(), datePtr.Length()); |
|
99 } |
|
100 |
|
101 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_utils_ResourceLoader__1getLocaleId |
|
102 (JNIEnv *, jobject) |
|
103 |
|
104 { |
|
105 return (jint)User::Language(); |
|
106 } |
|
107 |