diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/annotations/SKFunctionParameters.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/annotations/SKFunctionParameters.java index 6ef93d6bb5..95e5bcb961 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/annotations/SKFunctionParameters.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/annotations/SKFunctionParameters.java @@ -19,5 +19,11 @@ public @interface SKFunctionParameters { String defaultValue() default NO_DEFAULT_VALUE; + /** + * If required is false, no value is set, and no default value is provided, null will be passed + * as this argument. + */ + boolean required() default true; + Class> type() default String.class; } diff --git a/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/coreskills/TimeSkill.java b/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/coreskills/TimeSkill.java index 8efa98212d..2a2976ca29 100644 --- a/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/coreskills/TimeSkill.java +++ b/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/coreskills/TimeSkill.java @@ -2,6 +2,9 @@ package com.microsoft.semantickernel.coreskills; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; +import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute; +import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters; +import com.microsoft.semantickernel.util.LocaleParser; import java.time.ZoneId; import java.time.ZoneOffset; @@ -57,6 +60,15 @@ public class TimeSkill { public static final String DAY_MONTH_DAY_YEAR = "EEEE, MMMM d, yyyy"; + /** + * Get the current date and time for the system default timezone. + * + * @return a ZonedDateTime object with the current date and time. + */ + public ZonedDateTime now() { + return ZonedDateTime.now(ZoneId.systemDefault()); + } + /** * Get the current date. * @@ -65,9 +77,16 @@ public class TimeSkill { * @return The current date. */ @DefineSKFunction(name = "date", description = "Get the current date") - public String date() { + public String date( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { // Example: Sunday, 12 January, 2025 - return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR).format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR) + .withLocale(parseLocale(locale)) + .format(now()); } /** @@ -78,9 +97,16 @@ public class TimeSkill { * @return The current time. */ @DefineSKFunction(name = "time", description = "Get the current time") - public String time() { + public String time( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { // Example: 09:15:07 PM - return DateTimeFormatter.ofPattern("hh:mm:ss a").format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("hh:mm:ss a") + .withLocale(parseLocale(locale)) + .format(now()); } /** @@ -91,9 +117,15 @@ public class TimeSkill { * @return The current UTC date and time. */ @DefineSKFunction(name = "utcNow", description = "Get the current UTC date and time") - public String utcNow() { + public String utcNow( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a") - .format(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC)); + .withLocale(parseLocale(locale)) + .format(now().withZoneSameInstant(ZoneOffset.UTC)); } /** @@ -104,8 +136,13 @@ public class TimeSkill { * @return The current date. */ @DefineSKFunction(name = "today", description = "Get the current date") - public String today() { - return date(); + public String today( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return date(locale); } /** @@ -118,9 +155,15 @@ public class TimeSkill { @DefineSKFunction( name = "now", description = "Get the current date and time in the local time zone") - public String now() { + public String now( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a") - .format(ZonedDateTime.now()); + .withLocale(parseLocale(locale)) + .format(now()); } /** @@ -131,8 +174,13 @@ public class TimeSkill { * @return The current year. */ @DefineSKFunction(name = "year", description = "Get the current year") - public String year() { - return DateTimeFormatter.ofPattern("yyyy").format(ZonedDateTime.now()); + public String year( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("yyyy").withLocale(parseLocale(locale)).format(now()); } /** @@ -143,8 +191,13 @@ public class TimeSkill { * @return The current month name. */ @DefineSKFunction(name = "month", description = "Get the current month name") - public String month() { - return DateTimeFormatter.ofPattern("MMMM").format(ZonedDateTime.now()); + public String month( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("MMMM").withLocale(parseLocale(locale)).format(now()); } /** @@ -155,8 +208,13 @@ public class TimeSkill { * @return The current month number. */ @DefineSKFunction(name = "monthNumber", description = "Get the current month number") - public String monthNumber() { - return DateTimeFormatter.ofPattern("MM").format(ZonedDateTime.now()); + public String monthNumber( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("MM").withLocale(parseLocale(locale)).format(now()); } /** @@ -167,8 +225,13 @@ public class TimeSkill { * @return The current day of the month. */ @DefineSKFunction(name = "day", description = "Get the current day of the month") - public String day() { - return DateTimeFormatter.ofPattern("d").format(ZonedDateTime.now()); + public String day( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("d").withLocale(parseLocale(locale)).format(now()); } /** @@ -179,8 +242,13 @@ public class TimeSkill { * @return The current day of the week. */ @DefineSKFunction(name = "dayOfWeek", description = "Get the current day of the week") - public String dayOfWeek() { - return DateTimeFormatter.ofPattern("EEEE").format(ZonedDateTime.now()); + public String dayOfWeek( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("EEEE").withLocale(parseLocale(locale)).format(now()); } /** @@ -191,8 +259,13 @@ public class TimeSkill { * @return The current clock hour. */ @DefineSKFunction(name = "hour", description = "Get the current clock hour") - public String hour() { - return DateTimeFormatter.ofPattern("h a").format(ZonedDateTime.now()); + public String hour( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("h a").withLocale(parseLocale(locale)).format(now()); } /** @@ -203,8 +276,13 @@ public class TimeSkill { * @return The current clock 24-hour number. */ @DefineSKFunction(name = "hourNumber", description = "Get the current clock 24-hour number") - public String hourNumber() { - return DateTimeFormatter.ofPattern("HH").format(ZonedDateTime.now()); + public String hourNumber( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("HH").withLocale(parseLocale(locale)).format(now()); } /** @@ -219,10 +297,21 @@ public class TimeSkill { @DefineSKFunction( name = "daysAgo", description = "Get the date of offset from today by a provided number of days") - public static String daysAgo(String days) { + public String daysAgo( + @SKFunctionInputAttribute + @SKFunctionParameters( + name = "input", + description = "Number of days to offset from today.") + String days, + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { int offsetDays = Integer.parseInt(days); return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR) - .format(ZonedDateTime.now().minusDays(offsetDays)); + .withLocale(parseLocale(locale)) + .format(now().minusDays(offsetDays)); } /** @@ -236,16 +325,25 @@ public class TimeSkill { @DefineSKFunction( name = "dateMatchingLastDayName", description = "Get the date of the last day matching the supplied week day name") - public static String dateMatchingLastDayName(String dayName) { - ZonedDateTime currentDate = ZonedDateTime.now(); - Locale systemLocale = Locale.getDefault(); + public String dateMatchingLastDayName( + @SKFunctionInputAttribute + @SKFunctionParameters(name = "input", description = "Week name day.") + String dayName, + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + ZonedDateTime currentDate = now(); for (int i = 1; i <= 7; i++) { currentDate = currentDate.minusDays(1); String currentDayName = - currentDate.getDayOfWeek().getDisplayName(TextStyle.FULL, systemLocale); + currentDate.getDayOfWeek().getDisplayName(TextStyle.FULL, parseLocale(locale)); if (currentDayName.equalsIgnoreCase(dayName)) { - return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR).format(currentDate); + return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR) + .withLocale(parseLocale(locale)) + .format(currentDate); } } throw new IllegalArgumentException("dayName is not recognized"); @@ -259,8 +357,13 @@ public class TimeSkill { * @return The minutes on the current hour. */ @DefineSKFunction(name = "minute", description = "Get the minutes on the current hour") - public String minute() { - return DateTimeFormatter.ofPattern("mm").format(ZonedDateTime.now()); + public String minute( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("mm").withLocale(parseLocale(locale)).format(now()); } /** @@ -271,8 +374,13 @@ public class TimeSkill { * @return The seconds on the current minute. */ @DefineSKFunction(name = "second", description = "Get the seconds on the current minute") - public String second() { - return DateTimeFormatter.ofPattern("ss").format(ZonedDateTime.now()); + public String second( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("ss").withLocale(parseLocale(locale)).format(now()); } /** @@ -285,8 +393,13 @@ public class TimeSkill { @DefineSKFunction( name = "timeZoneOffset", description = "Get the local time zone offset from UTC") - public String timeZoneOffset() { - return DateTimeFormatter.ofPattern("XXX").format(ZonedDateTime.now()); + public String timeZoneOffset( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { + return DateTimeFormatter.ofPattern("XXX").withLocale(parseLocale(locale)).format(now()); } /** @@ -297,8 +410,25 @@ public class TimeSkill { * @return The local time zone name. */ @DefineSKFunction(name = "timeZoneName", description = "Get the local time zone name") - public String timeZoneName() { + public String timeZoneName( + @SKFunctionParameters( + name = "locale", + description = "Locale to use when formatting the date", + required = false) + String locale) { ZoneId zoneId = ZoneId.systemDefault(); - return zoneId.getDisplayName(TextStyle.FULL, Locale.getDefault()); + return zoneId.getDisplayName(TextStyle.FULL, parseLocale(locale)); + } + + /** + * Parse the locale string into a Locale object. + * + *
By default, it parses using the LocaleParser utility class. + * + * @param locale string + * @return a locale object + */ + protected Locale parseLocale(String locale) { + return LocaleParser.parseLocale(locale); } } diff --git a/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/util/LocaleParser.java b/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/util/LocaleParser.java new file mode 100644 index 0000000000..c377b3fe17 --- /dev/null +++ b/java/semantickernel-core-skills/src/main/java/com/microsoft/semantickernel/util/LocaleParser.java @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft. All rights reserved. +package com.microsoft.semantickernel.util; + +import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters; + +import java.util.Locale; + +/** + * Locale parser to support Java 8 and Java 9+ due to JEP 252. + * + *
Supports parsing strings containing languageCode_countryCode and languageCode-countryCode + * styles. + */ +public final class LocaleParser { + + private LocaleParser() {} + + /** + * Parses the locale. If the input is "en-US", it will return a Locale based on the language + * tag. If the input is "en_US", it will return a Locale based on the language and country code. + * Otherwise, it will return a Locale based on the input. + * + *
'en' 'US' used as example above.
+ *
+ * @param locale the locale in String text.
+ * @return
+ */
+ public static final Locale parseLocale(String locale) {
+ Locale parsedLocale = null;
+
+ if (locale == null
+ || "".equals(locale.trim())
+ || SKFunctionParameters.NO_DEFAULT_VALUE.equals(locale)) {
+ return Locale.getDefault();
+ } else if (locale.indexOf("-") > -1) {
+ parsedLocale = Locale.forLanguageTag(locale);
+ } else if (locale.indexOf("_") > -1) {
+ String[] parts = locale.split("_");
+ parsedLocale = new Locale(parts[0], parts[1]);
+ } else {
+ parsedLocale = new Locale(locale);
+ }
+
+ return parsedLocale;
+ }
+}
diff --git a/java/semantickernel-core-skills/src/test/java/com/microsoft/semantickernel/coreskills/TimeSkillTest.java b/java/semantickernel-core-skills/src/test/java/com/microsoft/semantickernel/coreskills/TimeSkillTest.java
index 8ceb15c677..713ce55991 100644
--- a/java/semantickernel-core-skills/src/test/java/com/microsoft/semantickernel/coreskills/TimeSkillTest.java
+++ b/java/semantickernel-core-skills/src/test/java/com/microsoft/semantickernel/coreskills/TimeSkillTest.java
@@ -5,8 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.MockedStatic;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -14,209 +14,133 @@ import java.util.Locale;
@SuppressWarnings("ReturnValueIgnored")
public class TimeSkillTest {
+
+ private static final String defaultLocale = "en-US";
+
private static final ZonedDateTime mockDateTime;
static {
mockDateTime = ZonedDateTime.of(2025, 1, 12, 9, 15, 7, 0, ZoneId.systemDefault());
}
+ private TimeSkill timeSkill;
+
+ @BeforeEach
+ public void setUp() {
+ timeSkill = spy(new TimeSkill());
+ when(timeSkill.now()).thenReturn(mockDateTime);
+ }
+
@Test
public void testDate() {
- TimeSkill timeSkill = new TimeSkill();
-
- try (MockedStatic