Skip to content

Commit d7144a9

Browse files
committed
Add CoreFoundation methods for getting current locale date format
Signed-off-by: Daniel Widdis <[email protected]>
1 parent ec70f94 commit d7144a9

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Next Release (5.18.0)
88
Features
99
--------
1010
* [#1671](https://github.com/java-native-access/jna/pull/1671): Add `isRISCV` to `c.s.j.Platform` - [@Glavo](https://github.com/Glavo).
11+
* [#1672](https://github.com/java-native-access/jna/pull/1672): Add `CFLocale`, `CFDateFormatterStyle`, `CFLocaleCopyCurrent`, `CFDateFormatterCreate` and `CFDateFormatterGetFormat` to `c.s.j.p.mac.CoreFoundation` - [@dbwiddis](https://github.com/dbwiddis).
1112

1213
Bug Fixes
1314
---------

contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,64 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, CFIndex c
11421142
* @return The type identifier for the {@code CFString} opaque type.
11431143
*/
11441144
CFTypeID CFStringGetTypeID();
1145+
1146+
/**
1147+
* The CFLocale opaque type provides support for obtaining available locales, obtaining localized locale names, and converting among locale data formats.
1148+
*/
1149+
class CFLocale extends CFTypeRef {
1150+
}
1151+
1152+
/**
1153+
* Enum of values used for {@link CFDateFormatterStyle} in {@link #CFDateFormatterCreate}.
1154+
* Use {@link CFDateFormatterStyle#style} for the expected integer value corresponding to the C-style enum.
1155+
*/
1156+
enum CFDateFormatterStyle {
1157+
noStyle,
1158+
shortStyle,
1159+
mediumStyle,
1160+
longStyle,
1161+
fullStyle;
1162+
1163+
/**
1164+
* Style for the type of {@link CFDateFormatterStyle} stored.
1165+
*
1166+
* @return a {@link CFIndex} representing the enum ordinal.
1167+
*/
1168+
public CFIndex index() {
1169+
return new CFIndex(this.ordinal());
1170+
}
1171+
}
1172+
1173+
/**
1174+
* Returns a copy of the logical locale for the current user.
1175+
* @return The logical locale for the current user that is formed from the settings for the current user’s
1176+
* chosen system locale overlaid with any custom settings the user has specified in System Preferences.
1177+
* May return a retained cached object, not a new object.
1178+
* <p>
1179+
* This reference must be released with {@link #CFRelease} to avoid leaking references.
1180+
*/
1181+
CFLocale CFLocaleCopyCurrent();
1182+
1183+
/**
1184+
* Creates a new CFDateFormatter object, localized to the given locale, which will format dates to the given date and time styles.
1185+
* @param allocator The allocator to use to allocate memory for the new object.
1186+
* Pass {@code null} or {@code kCFAllocatorDefault} to use the current default allocator.
1187+
* @param locale The locale to use for localization.
1188+
* If {@code null} uses the default system locale.
1189+
* Use {@link #CFLocaleCopyCurrent()} to specify the locale of the current user.
1190+
* @param dateStyle The date style to use when formatting dates.
1191+
* @param timeStyle The time style to use when formatting times.
1192+
* @return A new date formatter, localized to the given locale, which will format dates to the given date and time styles.
1193+
* Returns {@code null} if there was a problem creating the object.
1194+
* <p>
1195+
* This reference must be released with {@link #CFRelease} to avoid leaking references.
1196+
*/
1197+
CFTypeRef CFDateFormatterCreate(CFAllocatorRef allocator, CFLocale locale, CFIndex dateStyle, CFIndex timeStyle);
1198+
1199+
/**
1200+
* Returns a format string for the given date formatter object.
1201+
* @param formatter The date formatter to examine.
1202+
* @return The format string for {@code formatter}.
1203+
*/
1204+
CFStringRef CFDateFormatterGetFormat(CFTypeRef formatter);
11451205
}

contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
5151
import com.sun.jna.platform.mac.CoreFoundation.CFDataRef;
5252
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
53+
import com.sun.jna.platform.mac.CoreFoundation.CFLocale;
5354
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
5455
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
5556
import com.sun.jna.platform.mac.CoreFoundation.CFNumberType;
@@ -380,4 +381,28 @@ public void testCFEqual() {
380381
CF.CFRelease(s1_the_same);
381382
CF.CFRelease(s2);
382383
}
384+
385+
@Test
386+
void testLocaleFormat() {
387+
CFIndex style = CFDateFormatterStyle.noStyle.index();
388+
assertEquals("", getLocaleDateTimeFormat(style));
389+
style = CFDateFormatterStyle.shortStyle.index();
390+
assertTrue(getLocaleDateTimeFormat(style).contains("y"));
391+
}
392+
393+
private static String getLocaleDateTimeFormat(CFIndex style) {
394+
CFLocale locale = CF.CFLocaleCopyCurrent();
395+
try {
396+
CFTypeRef formatter = CF.CFDateFormatterCreate(null, locale, style, style);
397+
assertNotNull(formatter);
398+
try {
399+
CFStringRef format = CF.CFDateFormatterGetFormat(formatter);
400+
return format.stringValue();
401+
} finally {
402+
CF.CFRelease(formatter);
403+
}
404+
} finally {
405+
CF.CFRelease(locale);
406+
}
407+
}
383408
}

0 commit comments

Comments
 (0)