Repository: Unleash/unleash-client-java
Pull request #292 (2025-03-10 09:14:14)

fix: current time in constraints
Fixes an issue where current time in the context would cause a toggle to evaluate to false if that current time was a non zulu time zone. 


Patch:
diff --git a/src/main/java/io/getunleash/repository/YggdrasilAdapters.java b/src/main/java/io/getunleash/repository/YggdrasilAdapters.java
index a63d300f..69d7873b 100644
--- a/src/main/java/io/getunleash/repository/YggdrasilAdapters.java
+++ b/src/main/java/io/getunleash/repository/YggdrasilAdapters.java
@@ -69,8 +69,8 @@ public final class YggdrasilAdapters {
         mapped.setRemoteAddress(context.getRemoteAddress().orElse(null));
         mapped.setProperties(context.getProperties());
         mapped.setCurrentTime(
-                DateTimeFormatter.ISO_DATE_TIME.format(
-                        context.getCurrentTime().orElse(ZonedDateTime.now())));
+                DateTimeFormatter.ISO_INSTANT.format(
+                        context.getCurrentTime().orElse(ZonedDateTime.now()).toInstant()));
         return mapped;
     }
 


Test patch:
diff --git a/src/test/java/io/getunleash/DefaultUnleashTest.java b/src/test/java/io/getunleash/DefaultUnleashTest.java
index 2ed49f87..648b3d97 100644
--- a/src/test/java/io/getunleash/DefaultUnleashTest.java
+++ b/src/test/java/io/getunleash/DefaultUnleashTest.java
@@ -132,6 +132,30 @@ class DefaultUnleashTest {
         verify(fallback).isEnabled(any(), any());
     }
 
+    @Test
+    public void not_setting_current_time_falls_back_to_correct_now_instant() {
+
+        ToggleBootstrapProvider bootstrapper =
+                new ToggleBootstrapProvider() {
+
+                    @Override
+                    public Optional<String> read() {
+                        return Optional.of(loadMockFeatures("unleash-repo-v2-advanced.json"));
+                    }
+                };
+
+        UnleashConfig unleashConfigWithFallback =
+                UnleashConfig.builder()
+                        .unleashAPI("http://fakeAPI")
+                        .appName("fakeApp")
+                        .toggleBootstrapProvider(bootstrapper)
+                        .build();
+        sut = new DefaultUnleash(unleashConfigWithFallback);
+
+        boolean enabled = sut.isEnabled("Test.currentTime");
+        assertThat(enabled).isTrue();
+    }
+
     @Test
     public void multiple_instantiations_of_the_same_config_gives_errors() {
         ListAppender<ILoggingEvent> appender = new ListAppender();
diff --git a/src/test/resources/unleash-repo-v2-advanced.json b/src/test/resources/unleash-repo-v2-advanced.json
index 4d1ba69d..e75f3c14 100644
--- a/src/test/resources/unleash-repo-v2-advanced.json
+++ b/src/test/resources/unleash-repo-v2-advanced.json
@@ -44,7 +44,9 @@
                 {
                     "contextName": "single",
                     "operator": "STR_CONTAINS",
-                    "values": ["true"],
+                    "values": [
+                        "true"
+                    ],
                     "inverted": false,
                     "caseInsensitive": true
                 }
@@ -58,7 +60,9 @@
                 {
                     "contextName": "catOrDog",
                     "operator": "STR_CONTAINS",
-                    "values": ["cat"],
+                    "values": [
+                        "cat"
+                    ],
                     "inverted": false,
                     "caseInsensitive": true
                 }
@@ -74,7 +78,10 @@
                 {
                     "name": "default",
                     "segments": [
-                        0,1,2,3
+                        0,
+                        1,
+                        2,
+                        3
                     ]
                 }
             ],
@@ -89,6 +96,24 @@
                 }
             ],
             "createdAt": "2019-01-24T10:41:45.236Z"
+        },
+        {
+            "name": "Test.currentTime",
+            "description": null,
+            "enabled": true,
+            "strategies": [
+                {
+                    "name": "default",
+                    "constraints": [
+                        {
+                            "contextName": "currentTime",
+                            "operator": "DATE_AFTER",
+                            "value": "2022-01-29T13:00:00.000Z"
+                        }
+                    ]
+                }
+            ],
+            "createdAt": "2019-01-24T10:41:45.236Z"
         }
     ]
-}
+}
\ No newline at end of file