Skip to content

Commit 86a2ff1

Browse files
authored
Merge pull request #1672 from dbwiddis/dateformat
Add CoreFoundation methods for getting current locale date format
2 parents ec70f94 + 8a9dc39 commit 86a2ff1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-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`, `CFLocaleCopyCurrent`, `CFCFDateFormatter`, `CFDateFormatterStyle`, `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+
* Returns a copy of the logical locale for the current user.
1154+
* @return The logical locale for the current user that is formed from the settings for the current user’s
1155+
* chosen system locale overlaid with any custom settings the user has specified in System Preferences.
1156+
* May return a retained cached object, not a new object.
1157+
* <p>
1158+
* This reference must be released with {@link #CFRelease} to avoid leaking references.
1159+
*/
1160+
CFLocale CFLocaleCopyCurrent();
1161+
1162+
/**
1163+
* CFDateFormatter objects format the textual representations of CFDate and CFAbsoluteTime objects, and convert textual representations of dates and times into CFDate and CFAbsoluteTime objects.
1164+
*/
1165+
class CFDateFormatter extends CFTypeRef {
1166+
}
1167+
1168+
/**
1169+
* Enum of values used for {@link CFDateFormatterStyle} in {@link #CFDateFormatterCreate}.
1170+
* Use {@link CFDateFormatterStyle#index} for the expected integer value corresponding to the C-style enum.
1171+
*/
1172+
enum CFDateFormatterStyle {
1173+
kCFDateFormatterNoStyle,
1174+
kCFDateFormatterShortStyle,
1175+
kCFDateFormatterMediumStyle,
1176+
kCFDateFormatterLongStyle,
1177+
kCFDateFormatterFullStyle;
1178+
1179+
/**
1180+
* Style for the type of {@link CFDateFormatterStyle} stored.
1181+
*
1182+
* @return a {@link CFIndex} representing the enum ordinal.
1183+
*/
1184+
public CFIndex index() {
1185+
return new CFIndex(this.ordinal());
1186+
}
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: 28 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,29 @@ 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.kCFDateFormatterNoStyle.index();
390+
assertEquals("", getLocaleDateTimeFormat(style));
391+
style = CFDateFormatterStyle.kCFDateFormatterShortStyle.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+
assertNotNull(format);
403+
return format.stringValue();
404+
} finally {
405+
CF.CFRelease(formatter);
406+
}
407+
} finally {
408+
CF.CFRelease(locale);
409+
}
410+
}
383411
}

0 commit comments

Comments
 (0)