Skip to content

Commit f76fd40

Browse files
tore-statsigtore-statsig
andauthored
fix: normalize timestamps for eval comparison (#109)
* fix: normalize timestamps for eval comparison * fix: on operator --------- Co-authored-by: tore-statsig <tore@statsig>
1 parent a6e7993 commit f76fd40

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/EvaluationUtils.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,20 @@ public static function arrayContainsAll($value, $target): bool
263263
}
264264
return true;
265265
}
266+
267+
public static function getEpochMs($d): ?int
268+
{
269+
if ($d === null) {
270+
return null;
271+
}
272+
273+
$epoch = (int)$d;
274+
275+
// If it's in seconds, scale up to ms
276+
if ($epoch < 10000000000) { // < 1e10 = definitely seconds
277+
$epoch *= 1000;
278+
}
279+
280+
return $epoch;
281+
}
266282
}

src/Evaluator.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,18 @@ function evalCondition($user_obj, $condition): ConfigEvaluation
351351
case 'neq':
352352
return new ConfigEvaluation($value !== $target);
353353
case 'before':
354-
return new ConfigEvaluation($value < $target);
354+
$first = Utils::getEpochMs($value);
355+
$second = Utils::getEpochMs($target);
356+
return new ConfigEvaluation($first !== null && $second !== null && $first < $second);
355357
case 'after':
356-
return new ConfigEvaluation($value > $target);
358+
$first = Utils::getEpochMs($value);
359+
$second = Utils::getEpochMs($target);
360+
return new ConfigEvaluation($first !== null && $second !== null && $first > $second);
357361
case 'on':
358-
$a = date('Y-m-d', $value);
359-
$b = date('Y-m-d', $target);
362+
$first = Utils::getEpochMs($value);
363+
$second = Utils::getEpochMs($target);
364+
$a = date('Y-m-d', $first);
365+
$b = date('Y-m-d', $second);
360366
return new ConfigEvaluation($a == $b);
361367
case 'array_contains_any':
362368
if (!is_array($value)) {

0 commit comments

Comments
 (0)