From 968f72f303f020dfd0ab44fd1ae2f62d9c061b98 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 15 Nov 2012 21:35:37 +0000 Subject: [PATCH] Fixed missing seconds in time format. Added new basic class for date/time format. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@5825 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- retroshare-gui/src/gui/ForumsDialog.cpp | 17 ++-- retroshare-gui/src/gui/FriendsDialog.cpp | 3 +- retroshare-gui/src/gui/MessagesDialog.cpp | 8 +- .../src/gui/channels/ChannelDetails.cpp | 4 +- retroshare-gui/src/gui/chat/ChatStyle.cpp | 5 +- .../src/gui/connect/ConfCertDialog.cpp | 4 +- retroshare-gui/src/gui/feeds/ChanMsgItem.cpp | 3 +- retroshare-gui/src/gui/feeds/ChatMsgItem.cpp | 4 +- retroshare-gui/src/gui/feeds/ForumMsgItem.cpp | 6 +- retroshare-gui/src/gui/feeds/MsgItem.cpp | 7 +- retroshare-gui/src/gui/feeds/PeerItem.cpp | 4 +- retroshare-gui/src/gui/feeds/SecurityItem.cpp | 4 +- .../src/gui/forums/ForumDetails.cpp | 4 +- .../src/gui/msgs/MessageComposer.cpp | 10 +-- retroshare-gui/src/gui/msgs/MessageWidget.cpp | 7 +- retroshare-gui/src/retroshare-gui.pro | 2 + retroshare-gui/src/rshare.cpp | 35 +++----- retroshare-gui/src/rshare.h | 9 +-- retroshare-gui/src/util/DateTime.cpp | 81 +++++++++++++++++++ retroshare-gui/src/util/DateTime.h | 55 +++++++++++++ 20 files changed, 195 insertions(+), 77 deletions(-) create mode 100644 retroshare-gui/src/util/DateTime.cpp create mode 100644 retroshare-gui/src/util/DateTime.h diff --git a/retroshare-gui/src/gui/ForumsDialog.cpp b/retroshare-gui/src/gui/ForumsDialog.cpp index 97bf6b1c1..cfb6a0c3e 100644 --- a/retroshare-gui/src/gui/ForumsDialog.cpp +++ b/retroshare-gui/src/gui/ForumsDialog.cpp @@ -42,7 +42,7 @@ #include "channels/ShareKey.h" #include "notifyqt.h" #include "util/HandleRichText.h" -#include "rshare.h" +#include "util/DateTime.h" #include #include @@ -1522,17 +1522,14 @@ static QString buildReplyHeader(const ForumMsgInfo &msgInfo) link.createMessage(msgInfo.srcId, ""); QString from = link.toHtml(); - QDateTime qtime; - qtime.setTime_t(msgInfo.ts); - QString header = QString("-----%1-----").arg(QApplication::translate("ForumsDialog", "Original Message")); header += QString("
%1: %2
").arg(QApplication::translate("ForumsDialog", "From"), from); - header += QString("
%1: %2
").arg(QApplication::translate("ForumsDialog", "Sent"), Rshare::customLongDate(msgInfo.ts)); + header += QString("
%1: %2
").arg(QApplication::translate("ForumsDialog", "Sent"), DateTime::formatLongDateTime(msgInfo.ts)); header += QString("%1: %2

").arg(QApplication::translate("ForumsDialog", "Subject"), QString::fromStdWString(msgInfo.title)); header += "
"; - header += QApplication::translate("ForumsDialog", "On %1, %2 wrote:").arg(qtime.toString(Qt::DefaultLocaleShortDate), from); + header += QApplication::translate("ForumsDialog", "On %1, %2 wrote:").arg(DateTime::formatDateTime(msgInfo.ts), from); return header; } @@ -1846,14 +1843,14 @@ void ForumsFillThread::run() else qtime.setTime_t(tit->ts); - text = qtime.toString(Qt::DefaultLocaleShortDate); + text = DateTime::formatDateTime(qtime); sort = qtime.toString("yyyyMMdd_hhmmss"); if (useChildTS) { qtime.setTime_t(tit->ts); text += " / "; - text += qtime.toString(Qt::DefaultLocaleShortDate); + text += DateTime::formatDateTime(qtime); sort += "_" + qtime.toString("yyyyMMdd_hhmmss"); } item->setText(COLUMN_THREAD_DATE, text); @@ -1963,14 +1960,14 @@ void ForumsFillThread::run() else qtime.setTime_t(mit->ts); - text = qtime.toString(Qt::DefaultLocaleShortDate); + text = DateTime::formatDateTime(qtime); sort = qtime.toString("yyyyMMdd_hhmmss"); if (useChildTS) { qtime.setTime_t(mit->ts); text += " / "; - text += qtime.toString(Qt::DefaultLocaleShortDate); + text += DateTime::formatDateTime(qtime); sort += "_" + qtime.toString("yyyyMMdd_hhmmss"); } child->setText(COLUMN_THREAD_DATE, text); diff --git a/retroshare-gui/src/gui/FriendsDialog.cpp b/retroshare-gui/src/gui/FriendsDialog.cpp index 821986b07..0d29b6caf 100644 --- a/retroshare-gui/src/gui/FriendsDialog.cpp +++ b/retroshare-gui/src/gui/FriendsDialog.cpp @@ -50,6 +50,7 @@ #include "settings/rsharesettings.h" #include "util/misc.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include "chat/CreateLobbyDialog.h" #include "FriendRecommendDialog.h" @@ -706,7 +707,7 @@ void FriendsDialog::setChatInfo(QString info, QColor color) ui.msgText->clear(); nbLines = 1; } - ui.msgText->append("" + QTime::currentTime().toString(Qt::DefaultLocaleShortDate) + " - " + info + ""); + ui.msgText->append("" + DateTime::formatTime(QTime::currentTime()) + " - " + info + ""); } void FriendsDialog::on_actionClear_Chat_History_triggered() diff --git a/retroshare-gui/src/gui/MessagesDialog.cpp b/retroshare-gui/src/gui/MessagesDialog.cpp index 03708d4ab..0095f24dc 100644 --- a/retroshare-gui/src/gui/MessagesDialog.cpp +++ b/retroshare-gui/src/gui/MessagesDialog.cpp @@ -34,6 +34,7 @@ #include "common/TagDefs.h" #include "common/PeerDefs.h" #include "common/RSItemDelegate.h" +#include "util/DateTime.h" #include #include @@ -1051,14 +1052,11 @@ void MessagesDialog::insertMessages() //if the mail is on same date show only time. if (qdatetime.daysTo(QDateTime::currentDateTime()) == 0) { - QTime qtime = qdatetime.time(); - QVariant varTime(qtime); - item[COLUMN_DATE]->setData(varTime, Qt::DisplayRole); + item[COLUMN_DATE]->setData(DateTime::formatTime(qdatetime.time()), Qt::DisplayRole); } else { - QVariant varDateTime(qdatetime); - item[COLUMN_DATE]->setData(varDateTime, Qt::DisplayRole); + item[COLUMN_DATE]->setData(DateTime::formatDateTime(qdatetime), Qt::DisplayRole); } // for sorting item[COLUMN_DATE]->setData(qdatetime, ROLE_SORT); diff --git a/retroshare-gui/src/gui/channels/ChannelDetails.cpp b/retroshare-gui/src/gui/channels/ChannelDetails.cpp index 83f6bf31d..e45622c5d 100644 --- a/retroshare-gui/src/gui/channels/ChannelDetails.cpp +++ b/retroshare-gui/src/gui/channels/ChannelDetails.cpp @@ -19,7 +19,7 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ #include "ChannelDetails.h" -#include "rshare.h" +#include "util/DateTime.h" #include #include @@ -95,7 +95,7 @@ void ChannelDetails::loadChannel() // Set Last Channel Post Date if (ci.lastPost) { - ui.postline->setText(Rshare::customLongDate(ci.lastPost)); + ui.postline->setText(DateTime::formatLongDateTime(ci.lastPost)); } // Set Channel ID diff --git a/retroshare-gui/src/gui/chat/ChatStyle.cpp b/retroshare-gui/src/gui/chat/ChatStyle.cpp index f7528f48d..879e40ac7 100644 --- a/retroshare-gui/src/gui/chat/ChatStyle.cpp +++ b/retroshare-gui/src/gui/chat/ChatStyle.cpp @@ -105,6 +105,7 @@ #include "ChatStyle.h" #include "gui/settings/rsharesettings.h" #include "gui/notifyqt.h" +#include "util/DateTime.h" #include @@ -323,8 +324,8 @@ QString ChatStyle::formatMessage(enumFormatMessage type, const QString &name, co #endif QString formatMsg = style.replace("%name%", name) - .replace("%date%", timestamp.date().toString(Qt::DefaultLocaleShortDate)) - .replace("%time%", timestamp.time().toString(Qt::DefaultLocaleShortDate)) + .replace("%date%", DateTime::formatDate(timestamp.date())) + .replace("%time%", DateTime::formatTime(timestamp.time())) #ifdef COLORED_NICKNAMES .replace("%color%", color.name()) #endif diff --git a/retroshare-gui/src/gui/connect/ConfCertDialog.cpp b/retroshare-gui/src/gui/connect/ConfCertDialog.cpp index c67a6cbb2..6f7c2770b 100644 --- a/retroshare-gui/src/gui/connect/ConfCertDialog.cpp +++ b/retroshare-gui/src/gui/connect/ConfCertDialog.cpp @@ -40,7 +40,7 @@ #include "gui/notifyqt.h" #include "gui/common/AvatarDefs.h" #include "gui/MainWindow.h" -#include "rshare.h" +#include "util/DateTime.h" static QMap instances; @@ -165,7 +165,7 @@ void ConfCertDialog::load() ui.loc->setText(QString::fromUtf8(detail.location.c_str())); // Dont Show a timestamp in RS calculate the day - ui.lastcontact->setText(Rshare::customLongDate(detail.lastConnect)); + ui.lastcontact->setText(DateTime::formatLongDateTime(detail.lastConnect)); /* set retroshare version */ std::map::iterator vit; diff --git a/retroshare-gui/src/gui/feeds/ChanMsgItem.cpp b/retroshare-gui/src/gui/feeds/ChanMsgItem.cpp index 22a3c0856..ab9ca4fe0 100644 --- a/retroshare-gui/src/gui/feeds/ChanMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/ChanMsgItem.cpp @@ -32,6 +32,7 @@ #include "util/misc.h" #include "gui/RetroShareLink.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include @@ -188,7 +189,7 @@ void ChanMsgItem::updateItemStatic() msgLabel->setText(RsHtml().formatText(NULL, QString::fromStdWString(cmi.msg), RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS)); msgFrame->setVisible(!cmi.msg.empty()); - datetimelabel->setText(Rshare::customLongDate(cmi.ts)); + datetimelabel->setText(DateTime::formatLongDateTime(cmi.ts)); filelabel->setText(QString("(%1 %2) %3").arg(cmi.count).arg(tr("Files")).arg(misc::friendlyUnit(cmi.size))); diff --git a/retroshare-gui/src/gui/feeds/ChatMsgItem.cpp b/retroshare-gui/src/gui/feeds/ChatMsgItem.cpp index bcf913c5f..cbbe24674 100644 --- a/retroshare-gui/src/gui/feeds/ChatMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/ChatMsgItem.cpp @@ -27,9 +27,9 @@ #include "retroshare-gui/RsAutoUpdatePage.h" #include "gui/msgs/MessageComposer.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include "gui/common/AvatarDefs.h" #include "gui/settings/rsharesettings.h" -#include "rshare.h" #include "gui/notifyqt.h" @@ -138,7 +138,7 @@ void ChatMsgItem::insertChat(const std::string &message) std::cerr << "ChatMsgItem::insertChat(): " << msg << std::endl; #endif - timestampLabel->setText(Rshare::customLongDate(QDateTime::currentDateTime())); + timestampLabel->setText(DateTime::formatLongDateTime(QDateTime::currentDateTime())); QString formatMsg = QString::fromUtf8(message.c_str()); diff --git a/retroshare-gui/src/gui/feeds/ForumMsgItem.cpp b/retroshare-gui/src/gui/feeds/ForumMsgItem.cpp index 22b65b472..32ca0c34a 100644 --- a/retroshare-gui/src/gui/feeds/ForumMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/ForumMsgItem.cpp @@ -32,10 +32,10 @@ #include "gui/forums/CreateForumMsg.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include "gui/common/AvatarDefs.h" #include "gui/notifyqt.h" #include "gui/ForumsDialog.h" -#include "rshare.h" //#include "gui/settings/rsharesettings.h" /**** @@ -161,7 +161,7 @@ void ForumMsgItem::updateItemStatic() prevSubLabel->setText(link.toHtml()); prevMsgLabel->setText(RsHtml().formatText(NULL, ForumsDialog::messageFromInfo(msg), RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS)); - timestamplabel->setText(Rshare::customLongDate(msg.ts)); + timestamplabel->setText(DateTime::formatLongDateTime(msg.ts)); nextFrame->hide(); } @@ -183,7 +183,7 @@ void ForumMsgItem::updateItemStatic() nextSubLabel->setText(link.toHtml()); nextMsgLabel->setText(RsHtml().formatText(NULL, ForumsDialog::messageFromInfo(msg), RSHTML_FORMATTEXT_EMBED_SMILEYS | RSHTML_FORMATTEXT_EMBED_LINKS)); - timestamplabel->setText(Rshare::customLongDate(msg.ts)); + timestamplabel->setText(DateTime::formatLongDateTime(msg.ts)); prevSHLabel->setText(tr("In Reply to") + ": "); diff --git a/retroshare-gui/src/gui/feeds/MsgItem.cpp b/retroshare-gui/src/gui/feeds/MsgItem.cpp index e731424e0..4705706fd 100644 --- a/retroshare-gui/src/gui/feeds/MsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/MsgItem.cpp @@ -27,12 +27,13 @@ #include "SubFileItem.h" #include "gui/msgs/MessageComposer.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include "gui/common/AvatarDefs.h" #include "gui/notifyqt.h" #include #include -#include "rshare.h" + /**** * #define DEBUG_ITEM 1 ****/ @@ -92,9 +93,7 @@ void MsgItem::updateItemStatic() srcName = QString::fromUtf8(rsPeers->getPeerName(mi.srcId).c_str()); } - { - timestampLabel->setText(Rshare::customLongDate(mi.ts)); - } + timestampLabel->setText(DateTime::formatLongDateTime(mi.ts)); if (!mIsHome) { diff --git a/retroshare-gui/src/gui/feeds/PeerItem.cpp b/retroshare-gui/src/gui/feeds/PeerItem.cpp index f384ffa4e..5df812e15 100644 --- a/retroshare-gui/src/gui/feeds/PeerItem.cpp +++ b/retroshare-gui/src/gui/feeds/PeerItem.cpp @@ -28,7 +28,7 @@ #include "gui/msgs/MessageComposer.h" #include "gui/common/StatusDefs.h" #include "gui/common/AvatarDefs.h" -#include "rshare.h" +#include "util/DateTime.h" #include "gui/notifyqt.h" @@ -118,7 +118,7 @@ void PeerItem::updateItemStatic() { /* set peer name */ peerNameLabel->setText(QString::fromUtf8(details.name.c_str())); - lastLabel-> setText(Rshare::customLongDate(details.lastConnect)); + lastLabel-> setText(DateTime::formatLongDateTime(details.lastConnect)); /* expanded Info */ nameLabel->setText(QString::fromUtf8(details.name.c_str())); diff --git a/retroshare-gui/src/gui/feeds/SecurityItem.cpp b/retroshare-gui/src/gui/feeds/SecurityItem.cpp index a3a2e3e7a..7bfe653bd 100644 --- a/retroshare-gui/src/gui/feeds/SecurityItem.cpp +++ b/retroshare-gui/src/gui/feeds/SecurityItem.cpp @@ -31,12 +31,12 @@ #include "gui/connect/ConfCertDialog.h" #include "gui/connect/ConnectFriendWizard.h" #include "gui/common/AvatarDefs.h" +#include "util/DateTime.h" #include "gui/notifyqt.h" #include #include -#include "rshare.h" /***** * #define DEBUG_ITEM 1 @@ -145,7 +145,7 @@ void SecurityItem::updateItemStatic() titleLabel->setText(title); QDateTime currentTime = QDateTime::currentDateTime(); - timeLabel->setText(Rshare::customLongDate(currentTime.toTime_t())); + timeLabel->setText(DateTime::formatLongDateTime(currentTime.toTime_t())); if (mIsHome) { diff --git a/retroshare-gui/src/gui/forums/ForumDetails.cpp b/retroshare-gui/src/gui/forums/ForumDetails.cpp index 442c7fdd0..9846b073d 100644 --- a/retroshare-gui/src/gui/forums/ForumDetails.cpp +++ b/retroshare-gui/src/gui/forums/ForumDetails.cpp @@ -19,7 +19,7 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ #include "ForumDetails.h" -#include "rshare.h" +#include "util/DateTime.h" #include #include @@ -90,7 +90,7 @@ void ForumDetails::loadDialog() // Set Last Post Date if (fi.lastPost) { - ui.postline->setText(Rshare::customLongDate(fi.lastPost)); + ui.postline->setText(DateTime::formatLongDateTime(fi.lastPost)); } // Set Forum ID diff --git a/retroshare-gui/src/gui/msgs/MessageComposer.cpp b/retroshare-gui/src/gui/msgs/MessageComposer.cpp index 7b393e81b..3c4408f5d 100644 --- a/retroshare-gui/src/gui/msgs/MessageComposer.cpp +++ b/retroshare-gui/src/gui/msgs/MessageComposer.cpp @@ -33,10 +33,10 @@ #include #include #include +#include #include -#include "rshare.h" #include "MessageComposer.h" #include @@ -55,6 +55,7 @@ #include "gui/common/Emoticons.h" #include "textformat.h" #include "util/misc.h" +#include "util/DateTime.h" #include "TagsMenu.h" #include "gui/common/TagDefs.h" #include "gui/connect/ConfCertDialog.h" @@ -947,9 +948,6 @@ QString MessageComposer::buildReplyHeader(const MessageInfo &msgInfo) } } - QDateTime qtime; - qtime.setTime_t(msgInfo.ts); - QString header = QString("-----%1-----").arg(tr("Original Message")); header += QString("
%1: %2
").arg(tr("From"), from); header += QString("%1: %2
").arg(tr("To"), to); @@ -958,11 +956,11 @@ QString MessageComposer::buildReplyHeader(const MessageInfo &msgInfo) header += QString("%1: %2
").arg(tr("Cc"), cc); } - header += QString("
%1: %2
").arg(tr("Sent"), Rshare::customLongDate(qtime)); + header += QString("
%1: %2
").arg(tr("Sent"), DateTime::formatLongDateTime(msgInfo.ts)); header += QString("%1: %2

").arg(tr("Subject"), QString::fromStdWString(msgInfo.title)); header += "
"; - header += tr("On %1, %2 wrote:").arg(qtime.toString(Qt::DefaultLocaleShortDate), from); + header += tr("On %1, %2 wrote:").arg(DateTime::formatDateTime(msgInfo.ts), from); return header; } diff --git a/retroshare-gui/src/gui/msgs/MessageWidget.cpp b/retroshare-gui/src/gui/msgs/MessageWidget.cpp index 3846670a1..6e646a382 100644 --- a/retroshare-gui/src/gui/msgs/MessageWidget.cpp +++ b/retroshare-gui/src/gui/msgs/MessageWidget.cpp @@ -41,6 +41,7 @@ #include "util/misc.h" #include "util/printpreview.h" #include "util/HandleRichText.h" +#include "util/DateTime.h" #include #include @@ -533,11 +534,7 @@ void MessageWidget::fill(const std::string &msgId) ui.bccText->clear(); } - { - QDateTime qtime; - qtime.setTime_t(msgInfo.ts); - ui.dateText->setText(qtime.toString(Qt::DefaultLocaleShortDate)); - } + ui.dateText->setText(DateTime::formatDateTime(msgInfo.ts)); std::string ownId = rsPeers->getOwnId(); diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 469d816ff..3c7e21283 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -271,6 +271,7 @@ HEADERS += rshare.h \ gui/im_history/IMHistoryItemPainter.h \ lang/languagesupport.h \ util/stringutil.h \ + util/DateTime.h \ util/win32.h \ util/RetroStyleLabel.h \ util/dllexport.h \ @@ -571,6 +572,7 @@ SOURCES += main.cpp \ gui/xprogressbar.cpp \ lang/languagesupport.cpp \ util/stringutil.cpp \ + util/DateTime.cpp \ util/win32.cpp \ util/RetroStyleLabel.cpp \ util/WidgetBackgroundImage.cpp \ diff --git a/retroshare-gui/src/rshare.cpp b/retroshare-gui/src/rshare.cpp index 372ca71ff..ec93333c6 100644 --- a/retroshare-gui/src/rshare.cpp +++ b/retroshare-gui/src/rshare.cpp @@ -57,8 +57,7 @@ QMap Rshare::_args; /**< List of command-line arguments. */ QString Rshare::_style; /**< The current GUI style. */ QString Rshare::_language; /**< The current language. */ QString Rshare::_stylesheet; /**< The current GUI stylesheet. */ -QString Rshare::_datetimeformat; /**< The format of dates in feed items etc. */ -QLocale Rshare::_locale; /**< The locale. */ +QString Rshare::_dateformat; /**< The format of dates in feed items etc. */ Log Rshare::_log; bool Rshare::useConfigDir; QString Rshare::configDir; @@ -356,31 +355,21 @@ Rshare::setLocale(QString languageCode) return retVal; } -/** return custom formatted date */ -QString Rshare::customLongDate(uint fromTime) -{ - return customLongDate(QDateTime::fromTime_t(fromTime)); -} - -QString Rshare::customLongDate(const QDateTime &fromTime) -{ - if (_datetimeformat.isEmpty()) { - return fromTime.toString(Qt::ISODate); - } else { - return _locale.toString(fromTime, _datetimeformat); - } -} - /** customize date format for feeds etc. */ void Rshare::customizeDateFormat() { - _locale = QLocale(); // set to default locale + QLocale locale = QLocale(); // set to default locale /* get long date format without weekday */ - QString dateformat = _locale.dateFormat(QLocale::LongFormat); - dateformat.replace(QRegExp("^dddd,*[^ ]* *('[^']+' )*"), ""); - dateformat.replace(QRegExp(",* *dddd"), ""); - dateformat = dateformat.trimmed(); - _datetimeformat = dateformat + " " + _locale.timeFormat(QLocale::ShortFormat); + _dateformat = locale.dateFormat(QLocale::LongFormat); + _dateformat.replace(QRegExp("^dddd,*[^ ]* *('[^']+' )*"), ""); + _dateformat.replace(QRegExp(",* *dddd"), ""); + _dateformat = _dateformat.trimmed(); +} + +/** Get custom date format (defaultlongformat) */ +QString Rshare::customDateFormat() +{ + return _dateformat; } /** Sets the GUI style RetroShare will use. If one was specified on the diff --git a/retroshare-gui/src/rshare.h b/retroshare-gui/src/rshare.h index 070211cf1..7570d3255 100644 --- a/retroshare-gui/src/rshare.h +++ b/retroshare-gui/src/rshare.h @@ -75,9 +75,9 @@ public: static bool setLanguage(QString languageCode = QString()); /** Sets the current locale. */ static bool setLocale(QString languageCode = QString()); - /** Returns custom formatted date */ - static QString customLongDate(uint fromTime); - static QString customLongDate(const QDateTime &fromTime); + /** Get custom date format (defaultlongformat) */ + static QString customDateFormat(); + /** Sets the current GUI style. */ static bool setStyle(QString styleKey = QString()); /** Sets the current GUI stylesheet. */ @@ -169,8 +169,7 @@ private: static QString _style; /**< The current GUI style. */ static QString _stylesheet; /**< The current GUI stylesheet. */ static QString _language; /**< The current language. */ - static QString _datetimeformat; /**< The format for dates in feed items etc. */ - static QLocale _locale; /**< The locale (object). */ + static QString _dateformat; /**< The format for dates in feed items etc. */ static Log _log; /**< Logs debugging messages to file or stdout. */ static bool useConfigDir; diff --git a/retroshare-gui/src/util/DateTime.cpp b/retroshare-gui/src/util/DateTime.cpp new file mode 100644 index 000000000..50b8a41ad --- /dev/null +++ b/retroshare-gui/src/util/DateTime.cpp @@ -0,0 +1,81 @@ +/**************************************************************** + * This file is distributed under the following license: + * + * Copyright (c) 2012, RetroShare Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#include + +#include "DateTime.h" +#include "rshare.h" + +QString DateTime::formatLongDate(time_t dateValue) +{ + return formatLongDate(QDateTime::fromTime_t(dateValue).date()); +} + +QString DateTime::formatLongDate(const QDate &dateValue) +{ + QString customDateFormat = Rshare::customDateFormat(); + + if (customDateFormat.isEmpty()) { + return dateValue.toString(Qt::ISODate); + } else { + return QLocale().toString(dateValue, customDateFormat); + } +} + +QString DateTime::formatLongDateTime(time_t datetimeValue) +{ + return formatLongDateTime(QDateTime::fromTime_t(datetimeValue)); +} + +QString DateTime::formatLongDateTime(const QDateTime &datetimeValue) +{ + return formatLongDate(datetimeValue.date()) + " " + formatTime(datetimeValue.time()); +} + +QString DateTime::formatDateTime(time_t datetimeValue) +{ + return formatDateTime(QDateTime::fromTime_t(datetimeValue)); +} + +QString DateTime::formatDateTime(const QDateTime &datetimeValue) +{ + return formatDate(datetimeValue.date()) + " " + formatTime(datetimeValue.time()); +} + +QString DateTime::formatDate(time_t dateValue) +{ + return formatDate(QDateTime::fromTime_t(dateValue).date()); +} + +QString DateTime::formatDate(const QDate &dateValue) +{ + return dateValue.toString(Qt::SystemLocaleShortDate); +} + +QString DateTime::formatTime(time_t timeValue) +{ + return formatTime(QDateTime::fromTime_t(timeValue).time()); +} + +QString DateTime::formatTime(const QTime &timeValue) +{ + return timeValue.toString(Qt::SystemLocaleShortDate); +} diff --git a/retroshare-gui/src/util/DateTime.h b/retroshare-gui/src/util/DateTime.h new file mode 100644 index 000000000..ec77e2e0b --- /dev/null +++ b/retroshare-gui/src/util/DateTime.h @@ -0,0 +1,55 @@ +/**************************************************************** + * This file is distributed under the following license: + * + * Copyright (c) 20120, RetroShare Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#ifndef _DATETIME_H +#define _DATETIME_H + +#include + +class QDateTime; +class QDate; +class QTime; + +class DateTime +{ +public: + /* format long date and time e.g. "September 30, 2012" */ + static QString formatLongDate(time_t dateValue); + static QString formatLongDate(const QDate &dateValue); + + /* format long date and time e.g. "September 30, 2012 01:05 PM" */ + static QString formatLongDateTime(time_t datetimeValue); + static QString formatLongDateTime(const QDateTime &datetimeValue); + + /* format date e.g. "9/30/12", "30.09.12" */ + static QString formatDate(time_t dateValue); + static QString formatDate(const QDate &dateValue); + + /* format time e.g. "13:05:12" */ + static QString formatTime(time_t timeValue); + static QString formatTime(const QTime &timeValue); + + /* format date and time (see formatDate & formatTime) */ + static QString formatDateTime(time_t datetimeValue); + static QString formatDateTime(const QDateTime &datetimeValue); +}; + +#endif