From 96f4e38e38754ccf32a460f5661c32a929728c53 Mon Sep 17 00:00:00 2001 From: fgerlits Date: Wed, 29 Nov 2006 18:59:52 +0000 Subject: [PATCH] fixed #2055 --- .../db/include/LiveSupport/Db/Conversion.h | 13 ++++++-- campcaster/src/modules/db/src/Conversion.cxx | 20 +++++++---- .../scheduler/src/PostgresqlSchedule.cxx | 33 ++++++++++++------- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/campcaster/src/modules/db/include/LiveSupport/Db/Conversion.h b/campcaster/src/modules/db/include/LiveSupport/Db/Conversion.h index 26c415071..d0811ee2f 100644 --- a/campcaster/src/modules/db/include/LiveSupport/Db/Conversion.h +++ b/campcaster/src/modules/db/include/LiveSupport/Db/Conversion.h @@ -83,13 +83,22 @@ class Conversion public: /** - * Convert a boost::ptime to a odbc::Timestamp. + * Constants to specify whether we round time values up or down. + */ + typedef enum { roundDown, + roundUp, + roundNearest } RoundingType; + + /** + * Convert a boost::ptime to a odbc::Timestamp, rounding down. * * @param ptime the boost ptime to convert. * @return an odbc::Timestamp, holding the same time. */ static Ptr::Ref - ptimeToTimestamp(Ptr::Ref ptime) throw (); + ptimeToTimestamp(Ptr::Ref ptime, + RoundingType round = roundDown) + throw (); /** * Convert an odbc::Timestamp to a boost::ptime. diff --git a/campcaster/src/modules/db/src/Conversion.cxx b/campcaster/src/modules/db/src/Conversion.cxx index 19d2c8410..45ee60693 100644 --- a/campcaster/src/modules/db/src/Conversion.cxx +++ b/campcaster/src/modules/db/src/Conversion.cxx @@ -56,18 +56,26 @@ using namespace LiveSupport::Db; * Convert a boost::ptime to an odbc::Timestamp *----------------------------------------------------------------------------*/ Ptr::Ref -Conversion :: ptimeToTimestamp(Ptr::Ref ptime) +Conversion :: ptimeToTimestamp(Ptr::Ref ptime, + RoundingType round) throw () { - gregorian::date date = ptime->date(); - posix_time::time_duration hours = ptime->time_of_day(); + posix_time::ptime newPtime = *ptime; + if (round == roundUp && newPtime.time_of_day().fractional_seconds() != 0) { + newPtime += posix_time::seconds(1); + } else if (round == roundNearest) { + newPtime += posix_time::microseconds(500000); + } + + gregorian::date date = newPtime.date(); + posix_time::time_duration time = newPtime.time_of_day(); Ptr::Ref timestamp(new odbc::Timestamp(date.year(), date.month(), date.day(), - hours.hours(), - hours.minutes(), - hours.seconds())); + time.hours(), + time.minutes(), + time.seconds())); return timestamp; } diff --git a/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx b/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx index 24c000171..71b8a742f 100644 --- a/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx +++ b/campcaster/src/products/scheduler/src/PostgresqlSchedule.cxx @@ -322,12 +322,12 @@ PostgresqlSchedule :: isTimeframeAvailable( Ptr::Ref timestamp; Ptr::Ref pstmt(conn->prepareStatement( isTimeframaAvailableStmt)); - timestamp = Conversion::ptimeToTimestamp(from); + timestamp = Conversion::ptimeToTimestamp(from, Conversion::roundDown); pstmt->setTimestamp(1, *timestamp); pstmt->setTimestamp(2, *timestamp); pstmt->setTimestamp(5, *timestamp); - timestamp = Conversion::ptimeToTimestamp(to); + timestamp = Conversion::ptimeToTimestamp(to, Conversion::roundUp); pstmt->setTimestamp(3, *timestamp); pstmt->setTimestamp(4, *timestamp); pstmt->setTimestamp(6, *timestamp); @@ -370,11 +370,13 @@ PostgresqlSchedule :: schedulePlaylist( pstmt->setLong(1, id->getId()); pstmt->setLong(2, playlist->getId()->getId()); - timestamp = Conversion::ptimeToTimestamp(playtime); + timestamp = Conversion::ptimeToTimestamp(playtime, + Conversion::roundNearest); pstmt->setTimestamp(3, *timestamp); ends.reset(new ptime((*playtime) + *(playlist->getPlaylength()))); - timestamp = Conversion::ptimeToTimestamp(ends); + timestamp = Conversion::ptimeToTimestamp(ends, + Conversion::roundUp); pstmt->setTimestamp(4, *timestamp); result = pstmt->executeUpdate() == 1; @@ -416,10 +418,12 @@ PostgresqlSchedule :: storeScheduleEntry( pstmt->setLong(1, scheduleEntry->getId()->getId()); pstmt->setLong(2, scheduleEntry->getPlaylistId()->getId()); - timestamp = Conversion::ptimeToTimestamp(scheduleEntry->getStartTime()); + timestamp = Conversion::ptimeToTimestamp(scheduleEntry->getStartTime(), + Conversion::roundDown); pstmt->setTimestamp(3, *timestamp); - timestamp = Conversion::ptimeToTimestamp(scheduleEntry->getEndTime()); + timestamp = Conversion::ptimeToTimestamp(scheduleEntry->getEndTime(), + Conversion::roundUp); pstmt->setTimestamp(4, *timestamp); result = pstmt->executeUpdate() == 1; @@ -456,9 +460,11 @@ PostgresqlSchedule :: getScheduleEntries( Ptr::Ref timestamp; Ptr::Ref pstmt(conn->prepareStatement( getScheduleEntriesStmt)); - timestamp = Conversion::ptimeToTimestamp(fromTime); + timestamp = Conversion::ptimeToTimestamp(fromTime, + Conversion::roundDown); pstmt->setTimestamp(1, *timestamp); - timestamp = Conversion::ptimeToTimestamp(toTime); + timestamp = Conversion::ptimeToTimestamp(toTime, + Conversion::roundUp); pstmt->setTimestamp(2, *timestamp); Ptr::Ref rs(pstmt->executeQuery()); @@ -565,7 +571,7 @@ PostgresqlSchedule :: getCurrentlyPlaying(void) throw () Ptr::Ref timestamp; Ptr::Ref pstmt(conn->prepareStatement( getCurrentlyPlayingStmt)); - timestamp = Conversion::ptimeToTimestamp(now); + timestamp = Conversion::ptimeToTimestamp(now, Conversion::roundNearest); pstmt->setTimestamp(1, *timestamp); pstmt->setTimestamp(2, *timestamp); @@ -611,7 +617,8 @@ PostgresqlSchedule :: getNextEntry(Ptr::Ref fromTime) Ptr::Ref timestamp; Ptr::Ref pstmt(conn->prepareStatement( getNextEntryStmt)); - timestamp = Conversion::ptimeToTimestamp(fromTime); + timestamp = Conversion::ptimeToTimestamp(fromTime, + Conversion::roundDown); pstmt->setTimestamp(1, *timestamp); Ptr::Ref rs(pstmt->executeQuery()); @@ -781,10 +788,12 @@ PostgresqlSchedule :: reschedule(Ptr::Ref entryId, Ptr::Ref pstmt(conn->prepareStatement( reschedulePlaylistStmt)); - timestamp = Conversion::ptimeToTimestamp(playtime); + timestamp = Conversion::ptimeToTimestamp(playtime, + Conversion::roundNearest); pstmt->setTimestamp(1, *timestamp); - timestamp = Conversion::ptimeToTimestamp(ends); + timestamp = Conversion::ptimeToTimestamp(ends, + Conversion::roundUp); pstmt->setTimestamp(2, *timestamp); pstmt->setLong(3, entryId->getId());