Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release (5.18.0)
Features
--------
* [#1671](https://github.com/java-native-access/jna/pull/1671): Add `isRISCV` to `c.s.j.Platform` - [@Glavo](https://github.com/Glavo).
* [#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).

Bug Fixes
---------
Expand Down
66 changes: 66 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java
Original file line number Diff line number Diff line change
Expand Up @@ -1142,4 +1142,70 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, CFIndex c
* @return The type identifier for the {@code CFString} opaque type.
*/
CFTypeID CFStringGetTypeID();

/**
* The CFLocale opaque type provides support for obtaining available locales, obtaining localized locale names, and converting among locale data formats.
*/
class CFLocale extends CFTypeRef {
}

/**
* Returns a copy of the logical locale for the current user.
* @return The logical locale for the current user that is formed from the settings for the current user’s
* chosen system locale overlaid with any custom settings the user has specified in System Preferences.
* May return a retained cached object, not a new object.
* <p>
* This reference must be released with {@link #CFRelease} to avoid leaking references.
*/
CFLocale CFLocaleCopyCurrent();

/**
* CFDateFormatter objects format the textual representations of CFDate and CFAbsoluteTime objects, and convert textual representations of dates and times into CFDate and CFAbsoluteTime objects.
*/
class CFDateFormatter extends CFTypeRef {
}

/**
* Enum of values used for {@link CFDateFormatterStyle} in {@link #CFDateFormatterCreate}.
* Use {@link CFDateFormatterStyle#index} for the expected integer value corresponding to the C-style enum.
*/
enum CFDateFormatterStyle {
kCFDateFormatterNoStyle,
kCFDateFormatterShortStyle,
kCFDateFormatterMediumStyle,
kCFDateFormatterLongStyle,
kCFDateFormatterFullStyle;

/**
* Style for the type of {@link CFDateFormatterStyle} stored.
*
* @return a {@link CFIndex} representing the enum ordinal.
*/
public CFIndex index() {
return new CFIndex(this.ordinal());
}
}

/**
* Creates a new CFDateFormatter object, localized to the given locale, which will format dates to the given date and time styles.
* @param allocator The allocator to use to allocate memory for the new object.
* Pass {@code null} or {@code kCFAllocatorDefault} to use the current default allocator.
* @param locale The locale to use for localization.
* If {@code null} uses the default system locale.
* Use {@link #CFLocaleCopyCurrent()} to specify the locale of the current user.
* @param dateStyle The date style to use when formatting dates.
* @param timeStyle The time style to use when formatting times.
* @return A new date formatter, localized to the given locale, which will format dates to the given date and time styles.
* Returns {@code null} if there was a problem creating the object.
* <p>
* This reference must be released with {@link #CFRelease} to avoid leaking references.
*/
CFDateFormatter CFDateFormatterCreate(CFAllocatorRef allocator, CFLocale locale, CFIndex dateStyle, CFIndex timeStyle);

/**
* Returns a format string for the given date formatter object.
* @param formatter The date formatter to examine.
* @return The format string for {@code formatter}.
*/
CFStringRef CFDateFormatterGetFormat(CFDateFormatter formatter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Random;

import com.sun.jna.platform.mac.CoreFoundation.CFDateFormatterStyle;
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -49,7 +50,9 @@
import com.sun.jna.platform.mac.CoreFoundation.CFAllocatorRef;
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
import com.sun.jna.platform.mac.CoreFoundation.CFDataRef;
import com.sun.jna.platform.mac.CoreFoundation.CFDateFormatter;
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
import com.sun.jna.platform.mac.CoreFoundation.CFLocale;
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
import com.sun.jna.platform.mac.CoreFoundation.CFNumberType;
Expand Down Expand Up @@ -380,4 +383,29 @@ public void testCFEqual() {
CF.CFRelease(s1_the_same);
CF.CFRelease(s2);
}

@Test
public void testLocaleFormat() {
CFIndex style = CFDateFormatterStyle.kCFDateFormatterNoStyle.index();
assertEquals("", getLocaleDateTimeFormat(style));
style = CFDateFormatterStyle.kCFDateFormatterShortStyle.index();
assertTrue(getLocaleDateTimeFormat(style).contains("y"));
}

private static String getLocaleDateTimeFormat(CFIndex style) {
CFLocale locale = CF.CFLocaleCopyCurrent();
try {
CFDateFormatter formatter = CF.CFDateFormatterCreate(null, locale, style, style);
assertNotNull(formatter);
try {
CFStringRef format = CF.CFDateFormatterGetFormat(formatter);
assertNotNull(format);
return format.stringValue();
} finally {
CF.CFRelease(formatter);
}
} finally {
CF.CFRelease(locale);
}
}
}
Loading