Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIXED Preorder date is incorrect #649 #699

Merged
merged 1 commit into from Oct 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_out/data/language/english.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@

<str name="#IF_COMPLETEPREORDER">Pre Load Complete</str>

<str name="#IF_PRELOADLAUNCH">{0} has been {2} and cannot be launched at this time. It will be available to play in {1} days.</str>
<str name="#IF_PRELOADLAUNCH">{0} has been {2} and cannot be launched at this time. It will be available to play in {1} {4} on {3}.</str>
<str name="#IF_PRELOADLAUNCH_TITLE">{0}</str>
<str name="#IF_PRELOADLAUNCH_PREORDERED">preordered</str>
<str name="#IF_PRELOADLAUNCH_PRELOADED">preloaded</str>
Expand Down
5 changes: 3 additions & 2 deletions cmake/modules/BuildBoost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ else()
set(BOOST_EXTRA_BUILD_OPTS "")
endif()

set(BOOST_BJAM_LIBS_STATIC --with-chrono --with-filesystem --with-thread --with-system)
set(BOOST_BJAM_LIBS ${BOOST_BJAM_LIBS_STATIC} --with-date_time --with-test)
set(BOOST_BJAM_LIBS_STATIC --with-chrono --with-filesystem --with-date_time --with-thread --with-system)
set(BOOST_BJAM_LIBS ${BOOST_BJAM_LIBS_STATIC} --with-test)

if (WIN32 AND NOT MINGW)
if (MSVC10)
Expand Down Expand Up @@ -118,6 +118,7 @@ if (WIN32)
set_property(TARGET boost_s PROPERTY FOLDER "ThirdParty")

set(Boost_CHRONO_LIBRARY_S "${Boost_LIBRARY_DIR}/libboost_chrono-${BOOST_LIB_ADD_STRING}.${BOOST_SUFFIX}")
set(Boost_DATE_TIME_LIBRARY_S "${Boost_LIBRARY_DIR}/libboost_date_time-${BOOST_LIB_ADD_STRING}.${BOOST_SUFFIX}")
set(Boost_FILESYSTEM_LIBRARY_S "${Boost_LIBRARY_DIR}/libboost_filesystem-${BOOST_LIB_ADD_STRING}.${BOOST_SUFFIX}")
set(Boost_SYSTEM_LIBRARY_S "${Boost_LIBRARY_DIR}/libboost_system-${BOOST_LIB_ADD_STRING}.${BOOST_SUFFIX}")
set(Boost_THREAD_LIBRARY_S "${Boost_LIBRARY_DIR}/libboost_thread-${BOOST_LIB_ADD_STRING}.${BOOST_SUFFIX}")
Expand Down
26 changes: 26 additions & 0 deletions src/include/util/UtilMisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,37 @@ namespace MISC
//!
bool isValidEmail(const char* email);

//! Checks that a Desura date/time is a valid ISO 8601 date/time and fixes if not
//!
//! @param date/time desura date/time string
//! return fixed date/time string (same as original if all okay)
//!
const std::string fixDateTimeString(const char* dateTime);

//! Converts a Desura date/time string to a locale specific format for display
//! E.G. 20131205T123456 -> 12/05/13 in US
//!
//! @param dateTime date/time string
//! return locale formatted string
//!
const std::string dateTimeToDisplay(const char* dateTime);

//! Gets the diff between 2 date/times
//!
//! @param dateTimeFirst date time input in the format YYYYMMDDHHMMSS
//! @param dateTimeLast date time input in the format YYYYMMDDHHMMSS
//! @param days out number of days between
//! @param hours out number of hours between
//! @param dtIsUTC flag true/false if time is UTC or not
//!
void getTimeDiff(const char* dateTimeFirst, const char* dateTimeLast, uint32 &days, uint32 &hours, bool dtIsUTC);

//! Gets the time diff from the input datetime to now
//!
//! @param dateTime date time input in the format YYYYMMDDHHMMSS
//! @param days out number of days between
//! @param hours out number of hours between
//! @param dtIsUTC flag true/false if time is UTC or not
//!
void getTimeDiffFromNow(const char* dateTime, uint32 &days, uint32 &hours, bool dtIsUTC = false);

Expand Down
7 changes: 6 additions & 1 deletion src/shared/uicore/code/InternalLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,16 @@ void InternalLink::showPreorderPrompt(DesuraId id, bool isPreload)

uint32 days;
uint32 hours;
std::string time_available = UTIL::MISC::dateTimeToDisplay(str);

UTIL::MISC::getTimeDiffFromNow(str, days, hours, true);

gcString title(Managers::GetString("#IF_PRELOADLAUNCH_TITLE"), item->getName());
gcString msg(Managers::GetString("#IF_PRELOADLAUNCH"), item->getName(), days, Managers::GetString(isPreload?"#IF_PRELOADLAUNCH_PRELOADED":"#IF_PRELOADLAUNCH_PREORDERED"));
gcString msg(Managers::GetString("#IF_PRELOADLAUNCH"), item->getName(),
days,
Managers::GetString(isPreload?"#IF_PRELOADLAUNCH_PRELOADED":"#IF_PRELOADLAUNCH_PREORDERED"),
time_available,
Managers::GetString(days == 1 ? "#IF_PRELOADLAUNCH_WORD_DAY":"#IF_PRELOADLAUNCH_WORD_DAYS"));

PreloadButtonHelper pobh(id);

Expand Down
1 change: 1 addition & 0 deletions src/shared/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include_directories(

file(GLOB Sources code/util/gcBuff_test.cpp
code/util/MD5_test.cpp
code/util/util_misc.cpp
code/util_fs/util_fs_copyFile.cpp
code/util_fs/util_fs_copyFolder.cpp
code/util_fs/util_fs_getAllFiles.cpp
Expand Down
128 changes: 128 additions & 0 deletions src/shared/unittests/code/util/util_misc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <gtest/gtest.h>

#include "Common.h"
#include "util/UtilMisc.h"

namespace UnitTest
{
TEST(UtilDateTime, fixDateTimeString_valid_string)
{
std::string input_string("20131025T123456");
std::string expected_result(input_string);

std::string s = UTIL::MISC::fixDateTimeString(input_string.c_str());

ASSERT_STREQ(expected_result.c_str(), s.c_str());
}

TEST(UtilDateTime, fixDateTimeString_missing_t)
{
std::string input_string("20150410345612");
std::string expected_result("20150410T345612");

std::string s = UTIL::MISC::fixDateTimeString(input_string.c_str());

ASSERT_STREQ(expected_result.c_str(), s.c_str());
}

TEST(UtilDateTime, fixDateTimeString_short_string)
{
std::string input_string("20110708");
std::string expected_result("20110708T000000");

std::string s = UTIL::MISC::fixDateTimeString(input_string.c_str());

ASSERT_STREQ(expected_result.c_str(), s.c_str());
}

TEST(UtilDateTime, fixDateTimeString_very_short_string)
{
std::string input_string("201007");
std::string expected_result("");

std::string s = UTIL::MISC::fixDateTimeString(input_string.c_str());

ASSERT_STREQ(expected_result.c_str(), s.c_str());
}

TEST(UtilDateTime, fixDateTimeString_simple_day_diff)
{
std::string dateTime1("20100101T000000");
std::string dateTime2("20100102T000000");

unsigned int days = 9999;
unsigned int hours = 1000;
bool dtIsUTC = false;

UTIL::MISC::getTimeDiff(dateTime1.c_str(), dateTime2.c_str(), days, hours, dtIsUTC);

int expected_days = 1;
int expected_hours = 0;

ASSERT_EQ(expected_days, days);
ASSERT_EQ(expected_hours, hours);
}

TEST(UtilDateTime, fixDateTimeString_less_simple_day_diff)
{
std::string dateTime1("20100430T000000");
std::string dateTime2("20100502T000000");

unsigned int days = 9999;
unsigned int hours = 1000;
bool dtIsUTC = false;

UTIL::MISC::getTimeDiff(dateTime1.c_str(), dateTime2.c_str(), days, hours, dtIsUTC);

int expected_days = 2;
int expected_hours = 0;

ASSERT_EQ(expected_days, days);
ASSERT_EQ(expected_hours, hours);
}

TEST(UtilDateTime, fixDateTimeString_less_simple_day_diff_invalid_dates)
{
std::string dateTime1("20100731000000");
std::string dateTime2("20100805000000");

unsigned int days = 9999;
unsigned int hours = 1000;
bool dtIsUTC = false;

UTIL::MISC::getTimeDiff(dateTime1.c_str(), dateTime2.c_str(), days, hours, dtIsUTC);

int expected_days = 5;
int expected_hours = 0;

ASSERT_EQ(expected_days, days);
ASSERT_EQ(expected_hours, hours);
}

TEST(UtilDateTime, fixDateTimeString_less_simple_day_diff_short_dates)
{
std::string dateTime1("20101130");
std::string dateTime2("20101220");

unsigned int days = 9999;
unsigned int hours = 1000;

bool dtIsUTC = false;
UTIL::MISC::getTimeDiff(dateTime1.c_str(), dateTime2.c_str(), days, hours, dtIsUTC);

int expected_days = 20;
int expected_hours = 0;

ASSERT_EQ(expected_days, days);
ASSERT_EQ(expected_hours, hours);

dtIsUTC = true;
UTIL::MISC::getTimeDiff(dateTime1.c_str(), dateTime2.c_str(), days, hours, dtIsUTC);

expected_days = 20;
expected_hours = 0;

ASSERT_EQ(expected_days, days);
ASSERT_EQ(expected_hours, hours);
}
}
4 changes: 3 additions & 1 deletion src/static/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ target_link_libraries(util
${BZIP2_LIBRARIES}
${SQLITE3X_LIBRARIES}
${Boost_FILESYSTEM_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${PLATFORM_LIBRARIES}
)
Expand All @@ -96,7 +97,8 @@ if (WIN32)
${BZIP2_LIBRARIES_S}
${SQLITE3X_LIBRARIES_S}
${Boost_FILESYSTEM_LIBRARY_S}
${Boost_SYSTEM_LIBRARY_S}
${Boost_SYSTEM_LIBRARY_S}
${Boost_DATE_TIME_LIBRARY_S}
${PLATFORM_LIBRARIES}
)

Expand Down
92 changes: 83 additions & 9 deletions src/static/util/code/UtilMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,39 +355,113 @@ bool isValidEmail(const char* email)
return true;
}

void getTimeDiffFromNow(const char* dateTime, uint32 &days, uint32 &hours, bool dtIsUTC)
// Boost gets sad if input dateTime strings are not valid ISO 8601 format so try to fix them
const std::string fixDateTimeString(const char* dateTime)
{
std::string date_str(dateTime);

// if date portion is too short, give up and return empty string
if ( date_str.length() < 8 )
return std::string();

if ( date_str.find("T") == std::string::npos )
{
// grab time portion if it exists
std::string time_part = date_str.substr(8);

// if string does not contain 'T', add it in the right place
date_str = date_str.substr(0,8);
date_str += "T";

// add back in the time
date_str += time_part;
}

// the length of a correctly formatted string
const unsigned int correct_length = std::string("20130101T123456").length();

// pad with '0's as required
while( date_str.length() < correct_length )
{
date_str += "0";
};

return date_str;
}

const std::string getNowAsDateTime()
{
bpt::ptime now(bpt::second_clock::local_time());

return bpt::to_iso_string(now);
}

const std::string dateTimeToDisplay(const char* dateTime)
{
std::string date_time_str = fixDateTimeString(dateTime);
bpt::ptime dt(bpt::from_iso_string(date_time_str));

// ostringstream owns the facet - no need to delete
bpt::time_facet *facet = new bpt::time_facet("%x");
std::ostringstream codec("");
codec.imbue(std::locale(codec.getloc(), facet));
codec << dt;
return codec.str();
}

void getTimeDiff(const char* dateTimeFirst, const char* dateTimeLast, uint32 &days, uint32 &hours, bool dtIsUTC)
{
days = 0;
hours = 0;

if (!dateTime)
if ( ! dateTimeFirst || ! dateTimeLast )
return;

std::string date_str_first = fixDateTimeString(dateTimeFirst);
std::string date_str_last = fixDateTimeString(dateTimeLast);

try
{
bpt::ptime t(bpt::from_iso_string(dateTime));
bpt::ptime now(bpt::second_clock::universal_time());
// convert first date/time to Boost UTC
bpt::ptime tt_first(bpt::from_iso_string(date_str_first));
tt_first = boost::date_time::local_adjustor<bpt::ptime, 0, bpt::no_dst>::utc_to_local(tt_first);

t = boost::date_time::local_adjustor<bpt::ptime, 0, bpt::no_dst>::utc_to_local(t);
if (dtIsUTC)
tt_first = boost::date_time::c_local_adjustor<bpt::ptime>::utc_to_local(tt_first);

// convert second date/time to Boost UTC
bpt::ptime tt_last(bpt::from_iso_string(date_str_last));
tt_last = boost::date_time::local_adjustor<bpt::ptime, 0, bpt::no_dst>::utc_to_local(tt_last);

if (dtIsUTC)
t = boost::date_time::c_local_adjustor<bpt::ptime>::utc_to_local(t);
tt_last = boost::date_time::c_local_adjustor<bpt::ptime>::utc_to_local(tt_last);

bpt::time_duration diff = t - now;
// pimp off boost for difference
bpt::time_duration diff = tt_last - tt_first;

// TODO - we might want to return this as negative values and use in calling func
if (diff.is_negative())
return;

days = diff.hours()/24;
hours = diff.hours() - days*24;
// get days and hours
days = diff.hours() / 24;
hours = diff.hours() - days * 24;
}
catch (...)
{
return;
}
}

void getTimeDiffFromNow(const char* dateTime, uint32 &days, uint32 &hours, bool dtIsUTC)
{
days = 0;
hours = 0;

std::string now_str = getNowAsDateTime();

return getTimeDiff(now_str.c_str(), dateTime, days, hours, dtIsUTC);
}

uint32 RSHash_CSTR(const char* buff, size_t size)
{
Expand Down