Skip to content

Commit

Permalink
fix formating of datetime for negative underline value
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Dec 13, 2024
1 parent fa01f42 commit 35ec785
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/studio/kdb/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ public StringBuilder format(StringBuilder builder, KFormatContext context) {
}

public Timestamp toTimestamp() {
return new Timestamp(((long) (.5 + 8.64e7 * (value + 10957))));
return new Timestamp(Math.round(8.64e7 * (value + 10957)));
}
}

Expand Down Expand Up @@ -1415,7 +1415,7 @@ public static KTimespan duration(double duration, KType unitType) {
if (! NS_IN_TYPES.containsKey(unitType))
throw new IllegalArgumentException(unitType.toString() + " is not supported");

return new KTimespan((long) (duration * NS_IN_TYPES.get(unitType)));
return new KTimespan(Math.round (duration * NS_IN_TYPES.get(unitType)));
}

public double toUnitValue(KType unitType) {
Expand All @@ -1429,7 +1429,7 @@ public static KTimespan duration(double duration, ChronoUnit unit) {
if (! NS_IN_UNITS.containsKey(unit))
throw new IllegalArgumentException("Unit " + unit.toString() + " is not supported");

return new KTimespan((long) (duration * NS_IN_UNITS.get(unit)) );
return new KTimespan(Math.round (duration * NS_IN_UNITS.get(unit)) );

}

Expand Down
3 changes: 1 addition & 2 deletions src/studio/kdb/KFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.text.ParsePosition;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class KFormat extends NumberFormat {

Expand Down Expand Up @@ -58,7 +57,7 @@ public StringBuffer format(double value, StringBuffer toAppendTo, FieldPosition
if (hasFraction) {
LocalDateTime dateTime = ((K.Month)kValue).toLocalDateTime();

LocalDateTime next = dateTime.plus((int)Math.signum(fraction), ChronoUnit.MONTHS);
LocalDateTime next = dateTime.plusMonths((int)Math.signum(fraction));
long days = Duration.between(dateTime, next).toDays();
long ns = (long) (Math.abs(fraction) * days * K.NS_IN_DAY);
kValue = K.KTimestamp.of(dateTime).add(new K.KTimespan(ns));
Expand Down
2 changes: 1 addition & 1 deletion test/studio/kdb/KTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void testSecondToString() throws Exception {

@Test
public void testDatetimeToString() throws Exception {
check(new K.KDatetime(-123456.789), "1661.12.26T05:03:50.401", "1661.12.26T05:03:50.401");
check(new K.KDatetime(-123456.789), "1661.12.26T05:03:50.400", "1661.12.26T05:03:50.400");
check(new K.KDatetime(123.456), "2000.05.03T10:56:38.400", "2000.05.03T10:56:38.400");
check(new K.KDatetime(Double.NEGATIVE_INFINITY), "-0wz", "-0wz");
check(new K.KDatetime(Double.POSITIVE_INFINITY), "0wz", "0wz");
Expand Down

0 comments on commit 35ec785

Please sign in to comment.