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 0183166c7d25..d146ecb80ac8 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 @@ -60,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 static ZonedDateTime now() { + return ZonedDateTime.now(ZoneId.systemDefault()); + } + /** * Get the current date. * @@ -76,7 +85,7 @@ public String date( // Example: Sunday, 12 January, 2025 return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR) .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + .format(now()); } /** @@ -95,7 +104,7 @@ public String time( // Example: 09:15:07 PM return DateTimeFormatter.ofPattern("hh:mm:ss a") .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + .format(now()); } /** @@ -113,7 +122,7 @@ public String utcNow( String locale) { return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a") .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now().withZoneSameInstant(ZoneOffset.UTC)); + .format(now().withZoneSameInstant(ZoneOffset.UTC)); } /** @@ -149,7 +158,7 @@ public String now( String locale) { return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR + " h:mm a") .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + .format(now()); } /** @@ -165,9 +174,7 @@ public String year( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("yyyy") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("yyyy").withLocale(parseLocale(locale)).format(now()); } /** @@ -183,9 +190,7 @@ public String month( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("MMMM") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("MMMM").withLocale(parseLocale(locale)).format(now()); } /** @@ -201,9 +206,7 @@ public String monthNumber( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("MM") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("MM").withLocale(parseLocale(locale)).format(now()); } /** @@ -219,9 +222,7 @@ public String day( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("d") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("d").withLocale(parseLocale(locale)).format(now()); } /** @@ -237,9 +238,7 @@ public String dayOfWeek( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("EEEE") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("EEEE").withLocale(parseLocale(locale)).format(now()); } /** @@ -255,9 +254,7 @@ public String hour( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("h a") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("h a").withLocale(parseLocale(locale)).format(now()); } /** @@ -273,9 +270,7 @@ public String hourNumber( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("HH") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("HH").withLocale(parseLocale(locale)).format(now()); } /** @@ -303,7 +298,7 @@ public String daysAgo( int offsetDays = Integer.parseInt(days); return DateTimeFormatter.ofPattern(DAY_MONTH_DAY_YEAR) .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now().minusDays(offsetDays)); + .format(now().minusDays(offsetDays)); } /** @@ -325,7 +320,7 @@ public String dateMatchingLastDayName( name = "locale", description = "Locale to use when formatting the date") String locale) { - ZonedDateTime currentDate = ZonedDateTime.now(); + ZonedDateTime currentDate = now(); for (int i = 1; i <= 7; i++) { currentDate = currentDate.minusDays(1); String currentDayName = @@ -353,9 +348,7 @@ public String minute( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("mm") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("mm").withLocale(parseLocale(locale)).format(now()); } /** @@ -371,9 +364,7 @@ public String second( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("ss") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("ss").withLocale(parseLocale(locale)).format(now()); } /** @@ -391,9 +382,7 @@ public String timeZoneOffset( name = "locale", description = "Locale to use when formatting the date") String locale) { - return DateTimeFormatter.ofPattern("XXX") - .withLocale(parseLocale(locale)) - .format(ZonedDateTime.now()); + return DateTimeFormatter.ofPattern("XXX").withLocale(parseLocale(locale)).format(now()); } /** 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 9546f5e181df..d7302c6851db 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 @@ -28,7 +28,7 @@ public void testDate() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.date(defaultLocale); assertEquals("Sunday, January 12, 2025", result); @@ -40,7 +40,7 @@ public void testTime() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.time(defaultLocale); assertEqualsIgnoreCase("09:15:07 AM", result); @@ -52,7 +52,7 @@ public void testToday() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.today(defaultLocale); assertEquals("Sunday, January 12, 2025", result); @@ -64,7 +64,7 @@ public void testNow() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.now(defaultLocale); assertEqualsIgnoreCase("Sunday, January 12, 2025 9:15 AM", result); @@ -76,7 +76,7 @@ public void testYear() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.year(defaultLocale); assertEquals("2025", result); @@ -88,7 +88,7 @@ public void testMonth() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.month(defaultLocale); assertEquals("January", result); @@ -100,7 +100,7 @@ public void testMonthNumber() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.monthNumber(defaultLocale); assertEquals("01", result); @@ -112,7 +112,7 @@ public void testDay() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.day(defaultLocale); assertEquals("12", result); @@ -124,7 +124,7 @@ public void testDayOfWeek() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.dayOfWeek(defaultLocale); assertEquals("Sunday", result); @@ -140,7 +140,7 @@ public void testHour() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.hour(defaultLocale); assertEqualsIgnoreCase("9 AM", result); @@ -152,7 +152,7 @@ public void testHourNumber() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.hourNumber(defaultLocale); assertEquals("09", result); @@ -164,7 +164,7 @@ public void testMinute() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.minute(defaultLocale); assertEquals("15", result); @@ -176,7 +176,7 @@ public void testSecond() { TimeSkill timeSkill = new TimeSkill(); try (MockedStatic mocked = mockStatic(ZonedDateTime.class)) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); String result = timeSkill.second(defaultLocale); assertEquals("07", result); @@ -194,7 +194,7 @@ public void daysAgo() { .useConstructor() .outerInstance(mockDateTime) .defaultAnswer(CALLS_REAL_METHODS))) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); assertEquals("Thursday, January 9, 2025", timeSkill.daysAgo("3", defaultLocale)); assertEquals("Tuesday, January 7, 2025", timeSkill.daysAgo("5", defaultLocale)); @@ -212,7 +212,7 @@ public void testDateMatchingLastDayName() { .useConstructor() .outerInstance(mockDateTime) .defaultAnswer(CALLS_REAL_METHODS))) { - mocked.when(ZonedDateTime::now).thenReturn(mockDateTime); + mocked.when(TimeSkill::now).thenReturn(mockDateTime); assertEquals( "Sunday, January 5, 2025",