Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.cloud.utils.component.ComponentMethodInterceptable;
import com.cloud.utils.crypt.DBEncryptionUtil;
import com.cloud.utils.db.SearchCriteria.SelectType;
import com.cloud.utils.db.datetime.SqlDateTimeUtil;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.Ip;
import com.cloud.utils.net.NetUtils;
Expand Down Expand Up @@ -1720,7 +1721,7 @@ protected void insertElementCollection(T entity, Attribute idAttribute, ID id, M
if (d == null) {
pstmt.setNull(1, Types.TIMESTAMP);
} else {
pstmt.setTimestamp(1, new Timestamp(d.getTime()), gmtCalendar());
pstmt.setTimestamp(1, SqlDateTimeUtil.toWholeSecondTimestamp(d.getTime()), gmtCalendar());
}
} else {
pstmt.setObject(1, en.nextElement());
Expand Down Expand Up @@ -1831,9 +1832,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina
if (attr.is(Attribute.Flag.Date)) {
pstmt.setDate(j, new java.sql.Date(date.getTime()), gmtCalendar());
} else if (attr.is(Attribute.Flag.TimeStamp)) {
pstmt.setTimestamp(j, new Timestamp(date.getTime()), gmtCalendar());
pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(date.getTime()), gmtCalendar());
} else if (attr.is(Attribute.Flag.Time)) {
pstmt.setTime(j, new java.sql.Time(date.getTime()), gmtCalendar());
pstmt.setTime(j, new java.sql.Time(SqlDateTimeUtil.truncateToSeconds(date.getTime())), gmtCalendar());
}
} else if (attr.field.getType() == Calendar.class) {
final Calendar cal = (Calendar)value;
Expand All @@ -1844,9 +1845,9 @@ protected void prepareAttribute(final int j, final PreparedStatement pstmt, fina
if (attr.is(Attribute.Flag.Date)) {
pstmt.setDate(j, new java.sql.Date(cal.getTimeInMillis()), gmtCalendar());
} else if (attr.is(Attribute.Flag.TimeStamp)) {
pstmt.setTimestamp(j, new Timestamp(cal.getTimeInMillis()), gmtCalendar());
pstmt.setTimestamp(j, SqlDateTimeUtil.toWholeSecondTimestamp(cal.getTimeInMillis()), gmtCalendar());
} else if (attr.is(Attribute.Flag.Time)) {
pstmt.setTime(j, new Time(cal.getTimeInMillis()), gmtCalendar());
pstmt.setTime(j, new Time(SqlDateTimeUtil.truncateToSeconds(cal.getTimeInMillis())), gmtCalendar());
}
} else if (attr.field.getType().isEnum()) {
final Enumerated enumerated = attr.field.getAnnotation(Enumerated.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.db.datetime;

import java.sql.Timestamp;

/**
* Helpers for binding dates to DATETIME/TIME columns, which have no fractional seconds.
* The MySQL driver sends milliseconds to the server, which rounds rather than truncates them
* (e.g. 10:59:59.999 is stored as 11:00:00), so values are cut to whole seconds before binding.
*/
public final class SqlDateTimeUtil {

private SqlDateTimeUtil() {
}

/**
* Drops the sub-second part of an epoch-millis value, rounding towards the past.
*/
public static long truncateToSeconds(long millis) {
return Math.floorDiv(millis, 1000L) * 1000L;
}

public static Timestamp toWholeSecondTimestamp(long millis) {
return new Timestamp(truncateToSeconds(millis));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.db.datetime;

import java.sql.Timestamp;

import org.junit.Assert;
import org.junit.Test;

public class SqlDateTimeUtilTest {

@Test
public void truncateToSecondsDropsMillisInsteadOfRounding() {
Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_999L));
Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_000L));
Assert.assertEquals(1_795_000_859_000L, SqlDateTimeUtil.truncateToSeconds(1_795_000_859_001L));
}

@Test
public void truncateToSecondsFloorsNegativeEpochValues() {
Assert.assertEquals(-1000L, SqlDateTimeUtil.truncateToSeconds(-1L));
}

@Test
public void toWholeSecondTimestampHasNoFractionalPart() {
Timestamp ts = SqlDateTimeUtil.toWholeSecondTimestamp(1_795_000_859_999L);

Assert.assertEquals(1_795_000_859_000L, ts.getTime());
Assert.assertEquals(0, ts.getNanos());
}
}
Loading