Skip to content

Commit 77697b5

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

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-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#index} 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.List;
4040
import java.util.Random;
4141

42+
import com.sun.jna.platform.mac.CoreFoundation.CFDateFormatterStyle;
4243
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
4344
import org.junit.Assert;
4445
import org.junit.Test;
@@ -50,6 +51,7 @@
5051
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
5152
import com.sun.jna.platform.mac.CoreFoundation.CFDataRef;
5253
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
54+
import com.sun.jna.platform.mac.CoreFoundation.CFLocale;
5355
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
5456
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
5557
import com.sun.jna.platform.mac.CoreFoundation.CFNumberType;
@@ -380,4 +382,28 @@ public void testCFEqual() {
380382
CF.CFRelease(s1_the_same);
381383
CF.CFRelease(s2);
382384
}
385+
386+
@Test
387+
public void testLocaleFormat() {
388+
CFIndex style = CFDateFormatterStyle.noStyle.index();
389+
assertEquals("", getLocaleDateTimeFormat(style));
390+
style = CFDateFormatterStyle.shortStyle.index();
391+
assertTrue(getLocaleDateTimeFormat(style).contains("y"));
392+
}
393+
394+
private static String getLocaleDateTimeFormat(CFIndex style) {
395+
CFLocale locale = CF.CFLocaleCopyCurrent();
396+
try {
397+
CFTypeRef formatter = CF.CFDateFormatterCreate(null, locale, style, style);
398+
assertNotNull(formatter);
399+
try {
400+
CFStringRef format = CF.CFDateFormatterGetFormat(formatter);
401+
return format.stringValue();
402+
} finally {
403+
CF.CFRelease(formatter);
404+
}
405+
} finally {
406+
CF.CFRelease(locale);
407+
}
408+
}
383409
}

0 commit comments

Comments
 (0)