Skip to content

Commit

Permalink
Use system's default locale if no input for locale is provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoborges committed Jul 10, 2023
1 parent 8787996 commit 1b7d2b0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public class TimeSkill {
public String date(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
// Example: Sunday, 12 January, 2025
return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR)
Expand All @@ -90,8 +89,7 @@ public String date(
public String time(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
// Example: 09:15:07 PM
return DateTimeFormatter.ofPattern("hh:mm:ss a")
Expand All @@ -110,8 +108,7 @@ public String time(
public String utcNow(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a")
.withLocale(parseLocale(locale))
Expand All @@ -129,8 +126,7 @@ public String utcNow(
public String today(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return date(locale);
}
Expand All @@ -148,8 +144,7 @@ public String today(
public String now(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a")
.withLocale(parseLocale(locale))
Expand All @@ -167,8 +162,7 @@ public String now(
public String year(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("yyyy")
.withLocale(parseLocale(locale))
Expand All @@ -186,8 +180,7 @@ public String year(
public String month(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("MMMM")
.withLocale(parseLocale(locale))
Expand All @@ -205,8 +198,7 @@ public String month(
public String monthNumber(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("MM")
.withLocale(parseLocale(locale))
Expand All @@ -224,8 +216,7 @@ public String monthNumber(
public String day(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("d")
.withLocale(parseLocale(locale))
Expand All @@ -243,8 +234,7 @@ public String day(
public String dayOfWeek(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("EEEE")
.withLocale(parseLocale(locale))
Expand All @@ -262,8 +252,7 @@ public String dayOfWeek(
public String hour(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("h a")
.withLocale(parseLocale(locale))
Expand All @@ -281,8 +270,7 @@ public String hour(
public String hourNumber(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("HH")
.withLocale(parseLocale(locale))
Expand All @@ -305,8 +293,7 @@ public static String daysAgo(
String days,
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
int offsetDays = Integer.parseInt(days);
return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR)
Expand All @@ -329,8 +316,7 @@ public static String dateMatchingLastDayName(
String dayName,
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
ZonedDateTime currentDate = ZonedDateTime.now();
for (int i = 1; i <= 7; i++) {
Expand Down Expand Up @@ -358,8 +344,7 @@ public static String dateMatchingLastDayName(
public String minute(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("mm")
.withLocale(parseLocale(locale))
Expand All @@ -377,8 +362,7 @@ public String minute(
public String second(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("ss")
.withLocale(parseLocale(locale))
Expand All @@ -398,8 +382,7 @@ public String second(
public String timeZoneOffset(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
return DateTimeFormatter.ofPattern("XXX")
.withLocale(parseLocale(locale))
Expand All @@ -417,8 +400,7 @@ public String timeZoneOffset(
public String timeZoneName(
@SKFunctionParameters(
name = "locale",
description = "Locale to use when formatting the date",
defaultValue = "en_US")
description = "Locale to use when formatting the date")
String locale) {
ZoneId zoneId = ZoneId.systemDefault();
return zoneId.getDisplayName(TextStyle.FULL, parseLocale(locale));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.Locale;
import java.util.Map;

import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute;
import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters;

/**
* Locale parser to support Java 8 and Java 9+ due to JEP 252.
*
Expand Down Expand Up @@ -34,7 +37,9 @@ public static final Locale parseLocale(String locale) {

Locale parsedLocale = null;

if (locale.indexOf("-") > -1) {
if (locale == null || "".equals(locale.trim()) || locale == SKFunctionParameters.NO_DEFAULT_VALUE) {
return Locale.getDefault();
} else if (locale.indexOf("-") > -1) {
parsedLocale = Locale.forLanguageTag(locale);
} else if (locale.indexOf("_") > -1) {
String[] parts = locale.split("_");
Expand Down

0 comments on commit 1b7d2b0

Please sign in to comment.