diff --git a/src/Format.cpp b/src/Format.cpp new file mode 100644 index 000000000..71a0e9eff --- /dev/null +++ b/src/Format.cpp @@ -0,0 +1,35 @@ + +#include +#include +#include "Format.h" + +std::string format::UnixtimeToDate(time_t unixtime, std::string dateFormat) +{ + struct tm * timeData; + char buffer[128]; + + timeData = localtime(&unixtime); + + strftime(buffer, 128, dateFormat.c_str(), timeData); + return std::string(buffer); +} + +std::string format::UnixtimeToDateMini(time_t unixtime) +{ + time_t currentTime = time(NULL); + struct tm currentTimeData = *localtime(¤tTime); + struct tm timeData = *localtime(&unixtime); + + if(currentTimeData.tm_year != timeData.tm_year) + { + return UnixtimeToDate(unixtime, "%b %Y"); + } + else if(currentTimeData.tm_mon != timeData.tm_mon || currentTimeData.tm_mday != timeData.tm_mday) + { + return UnixtimeToDate(unixtime, "%d %B"); + } + else + { + return UnixtimeToDate(unixtime, "%H:%M:%S"); + } +} diff --git a/src/Format.h b/src/Format.h new file mode 100644 index 000000000..1df883f4d --- /dev/null +++ b/src/Format.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace format +{ + template std::string NumberToString(T number) + { + std::stringstream ss; + ss << number; + return ss.str(); + } + + template T StringToNumber(const std::string & text) + { + std::stringstream ss(text); + T number; + return (ss >> number)?number:0; + } + + std::string UnixtimeToDate(time_t unixtime, std::string dateFomat = "%d %b %Y"); + std::string UnixtimeToDateMini(time_t unixtime); +} \ No newline at end of file diff --git a/src/Misc.h b/src/Misc.h index f3f25a9b9..cfac84133 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -83,20 +83,6 @@ void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v); void OpenURI(std::string uri); -template std::string NumberToString(T number) -{ - std::stringstream ss; - ss << number; - return ss.str(); -} - -template T StringToNumber(const std::string & text) -{ - std::stringstream ss(text); - T number; - return (ss >> number)?number:0; -} - void membwand(void * dest, void * src, size_t destsize, size_t srcsize); // a b // c d diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index b0736b48e..838f733ec 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -12,6 +12,7 @@ #include "interface/Slider.h" #include "search/Thumbnail.h" #include "simulation/SaveRenderer.h" +#include "Format.h" #include "QuickOption.h" GameView::GameView(): @@ -603,10 +604,10 @@ void GameView::NotifyColourSelectorColourChanged(GameModel * sender) { std::string intR, intG, intB, intA; - intR = NumberToString(sender->GetColourSelectorColour().Red); - intG = NumberToString(sender->GetColourSelectorColour().Green); - intB = NumberToString(sender->GetColourSelectorColour().Blue); - intA = NumberToString(sender->GetColourSelectorColour().Alpha); + intR = format::NumberToString(sender->GetColourSelectorColour().Red); + intG = format::NumberToString(sender->GetColourSelectorColour().Green); + intB = format::NumberToString(sender->GetColourSelectorColour().Blue); + intA = format::NumberToString(sender->GetColourSelectorColour().Alpha); colourRSlider->SetValue(sender->GetColourSelectorColour().Red); colourRSlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(255, 0, 0)); @@ -1318,10 +1319,10 @@ void GameView::changeColourSlider() void GameView::changeColourText() { c->SetColour(ui::Colour( - std::min(255U, StringToNumber(colourRValue->GetText())), - std::min(255U, StringToNumber(colourGValue->GetText())), - std::min(255U, StringToNumber(colourBValue->GetText())), - std::min(255U, StringToNumber(colourAValue->GetText()))) + std::min(255U, format::StringToNumber(colourRValue->GetText())), + std::min(255U, format::StringToNumber(colourGValue->GetText())), + std::min(255U, format::StringToNumber(colourBValue->GetText())), + std::min(255U, format::StringToNumber(colourAValue->GetText()))) ); } diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index f6c74b4c8..d134568de 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -15,6 +15,7 @@ #include "interface/Window.h" #include "interface/Textbox.h" #include "Style.h" +#include "Format.h" #include "search/Thumbnail.h" #include "client/Client.h" #include "interface/ScrollPanel.h" @@ -316,7 +317,7 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender) votesUp = save->votesUp; votesDown = save->votesDown; saveNameLabel->SetText(save->name); - authorDateLabel->SetText("\bgAuthor:\bw " + save->userName + " \bgDate:\bw "); + authorDateLabel->SetText("\bgAuthor:\bw " + save->userName + " \bgDate:\bw " + format::UnixtimeToDateMini(save->date)); saveDescriptionLabel->SetText(save->Description); if(save->Favourite) favButton->Enabled = false;