Skip to content

Commit e8259d3

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

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,4 +1142,70 @@ 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+
* CFDateFormatter objects format the textual representations of CFDate and CFAbsoluteTime objects, and convert textual representations of dates and times into CFDate and CFAbsoluteTime objects.
1185+
*/
1186+
class CFDateFormatter extends CFTypeRef {
1187+
}
1188+
1189+
/**
1190+
* Creates a new CFDateFormatter object, localized to the given locale, which will format dates to the given date and time styles.
1191+
* @param allocator The allocator to use to allocate memory for the new object.
1192+
* Pass {@code null} or {@code kCFAllocatorDefault} to use the current default allocator.
1193+
* @param locale The locale to use for localization.
1194+
* If {@code null} uses the default system locale.
1195+
* Use {@link #CFLocaleCopyCurrent()} to specify the locale of the current user.
1196+
* @param dateStyle The date style to use when formatting dates.
1197+
* @param timeStyle The time style to use when formatting times.
1198+
* @return A new date formatter, localized to the given locale, which will format dates to the given date and time styles.
1199+
* Returns {@code null} if there was a problem creating the object.
1200+
* <p>
1201+
* This reference must be released with {@link #CFRelease} to avoid leaking references.
1202+
*/
1203+
CFDateFormatter CFDateFormatterCreate(CFAllocatorRef allocator, CFLocale locale, CFIndex dateStyle, CFIndex timeStyle);
1204+
1205+
/**
1206+
* Returns a format string for the given date formatter object.
1207+
* @param formatter The date formatter to examine.
1208+
* @return The format string for {@code formatter}.
1209+
*/
1210+
CFStringRef CFDateFormatterGetFormat(CFDateFormatter formatter);
11451211
}

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

Lines changed: 27 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;
@@ -49,7 +50,9 @@
4950
import com.sun.jna.platform.mac.CoreFoundation.CFAllocatorRef;
5051
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
5152
import com.sun.jna.platform.mac.CoreFoundation.CFDataRef;
53+
import com.sun.jna.platform.mac.CoreFoundation.CFDateFormatter;
5254
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
55+
import com.sun.jna.platform.mac.CoreFoundation.CFLocale;
5356
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
5457
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
5558
import com.sun.jna.platform.mac.CoreFoundation.CFNumberType;
@@ -380,4 +383,28 @@ public void testCFEqual() {
380383
CF.CFRelease(s1_the_same);
381384
CF.CFRelease(s2);
382385
}
386+
387+
@Test
388+
public void testLocaleFormat() {
389+
CFIndex style = CFDateFormatterStyle.noStyle.index();
390+
assertEquals("", getLocaleDateTimeFormat(style));
391+
style = CFDateFormatterStyle.shortStyle.index();
392+
assertTrue(getLocaleDateTimeFormat(style).contains("y"));
393+
}
394+
395+
private static String getLocaleDateTimeFormat(CFIndex style) {
396+
CFLocale locale = CF.CFLocaleCopyCurrent();
397+
try {
398+
CFDateFormatter formatter = CF.CFDateFormatterCreate(null, locale, style, style);
399+
assertNotNull(formatter);
400+
try {
401+
CFStringRef format = CF.CFDateFormatterGetFormat(formatter);
402+
return format.stringValue();
403+
} finally {
404+
CF.CFRelease(formatter);
405+
}
406+
} finally {
407+
CF.CFRelease(locale);
408+
}
409+
}
383410
}

0 commit comments

Comments
 (0)