Skip to content

Commit

Permalink
proper zoned types
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoborges committed Jul 11, 2023
1 parent 9467c14 commit b07f4cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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 =
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testDate() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> 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);
Expand All @@ -40,7 +40,7 @@ public void testTime() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> 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);
Expand All @@ -52,7 +52,7 @@ public void testToday() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> 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);
Expand All @@ -64,7 +64,7 @@ public void testNow() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> 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);
Expand All @@ -76,7 +76,7 @@ public void testYear() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.year(defaultLocale);
assertEquals("2025", result);
Expand All @@ -88,7 +88,7 @@ public void testMonth() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.month(defaultLocale);
assertEquals("January", result);
Expand All @@ -100,7 +100,7 @@ public void testMonthNumber() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.monthNumber(defaultLocale);
assertEquals("01", result);
Expand All @@ -112,7 +112,7 @@ public void testDay() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.day(defaultLocale);
assertEquals("12", result);
Expand All @@ -124,7 +124,7 @@ public void testDayOfWeek() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.dayOfWeek(defaultLocale);
assertEquals("Sunday", result);
Expand All @@ -140,7 +140,7 @@ public void testHour() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> 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);
Expand All @@ -152,7 +152,7 @@ public void testHourNumber() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.hourNumber(defaultLocale);
assertEquals("09", result);
Expand All @@ -164,7 +164,7 @@ public void testMinute() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.minute(defaultLocale);
assertEquals("15", result);
Expand All @@ -176,7 +176,7 @@ public void testSecond() {
TimeSkill timeSkill = new TimeSkill();

try (MockedStatic<ZonedDateTime> mocked = mockStatic(ZonedDateTime.class)) {
mocked.when(ZonedDateTime::now).thenReturn(mockDateTime);
mocked.when(TimeSkill::now).thenReturn(mockDateTime);

String result = timeSkill.second(defaultLocale);
assertEquals("07", result);
Expand All @@ -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));
Expand All @@ -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",
Expand Down

0 comments on commit b07f4cf

Please sign in to comment.