001
014
015 package com.liferay.portlet.messageboards.util;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022
023 import java.sql.Connection;
024 import java.sql.DatabaseMetaData;
025 import java.sql.SQLException;
026
027 import java.util.Date;
028
029
032 public class MBMessageDateDigesterUtil {
033
034 public static Date digest(Date date) {
035 DB db = DBFactoryUtil.getDB();
036
037 if (db.isSupportsDateMilliseconds()) {
038 return date;
039 }
040
041 String dbType = db.getType();
042
043 if (dbType.equals(DB.TYPE_MYSQL)) {
044 return _digestMySQLDate(date);
045 }
046
047 return date;
048 }
049
050 private static Date _digestMySQLDate(Date date) {
051 long time = date.getTime();
052
053 long milliseconds = time % 1000;
054
055 if (milliseconds > 0) {
056 Connection connection = null;
057
058 try {
059 connection = DataAccess.getConnection();
060
061 DatabaseMetaData databaseMetaData = connection.getMetaData();
062
063 int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
064 int dbMinorVersion = databaseMetaData.getDatabaseMinorVersion();
065
066 if (((dbMajorVersion == 5) && (dbMinorVersion == 5)) ||
067 (Math.round((double)milliseconds / 1000) == 0)) {
068
069 time -= milliseconds;
070 }
071 else {
072 time += (1000 - milliseconds);
073 }
074
075 return new Date(time);
076 }
077 catch (SQLException se) {
078 _log.error("Unable to to get database version", se);
079 }
080 finally {
081 DataAccess.cleanUp(connection);
082 }
083 }
084
085 return date;
086 }
087
088 private static Log _log = LogFactoryUtil.getLog(
089 MBMessageDateDigesterUtil.class);
090
091 }