Permalink
Browse files

date formatting

  • Loading branch information...
1 parent 865857a commit 64d8edf8f70eabc3d7d8b7c1a940b016f52f2f63 @humbhenri committed May 16, 2012
Showing with 49 additions and 4 deletions.
  1. +4 −2 TimeTracker.pro
  2. +23 −0 prettydate.cpp
  3. +19 −0 prettydate.h
  4. +3 −2 projectwidget.cpp
View
@@ -26,7 +26,8 @@ SOURCES += main.cpp\
reportwidget.cpp \
clock.cpp \
projectitemdelegate.cpp \
- projectitem.cpp
+ projectitem.cpp \
+ prettydate.cpp
HEADERS += mainwindow.h \
preferenceswidget.h \
@@ -43,7 +44,8 @@ HEADERS += mainwindow.h \
reportwidget.h \
clock.h \
projectitemdelegate.h \
- projectitem.h
+ projectitem.h \
+ prettydate.h
FORMS += mainwindow.ui \
preferenceswidget.ui \
View
@@ -0,0 +1,23 @@
+#include "prettydate.h"
+
+const QString PrettyDate::JUST_NOW = "Just now ";
+const QString PrettyDate::MINUTE_AGO = "Minute ago ";
+const QString PrettyDate::MINUTES_AGO = " minutes ago ";
+const QString PrettyDate::HOUR_AGO = "Hour ago ";
+const QString PrettyDate::HOURS_AGO = " hours ago ";
+
+QString PrettyDate::prettify(QDateTime aDateTime)
+{
+ int secsDiff = aDateTime.secsTo(QDateTime::currentDateTime());
+ if(secsDiff < 60)
+ return JUST_NOW;
+ else if (secsDiff < 120)
+ return MINUTE_AGO;
+ else if (secsDiff < 3600)
+ return (secsDiff/60) + MINUTES_AGO;
+ else if (secsDiff < 7200)
+ return HOUR_AGO;
+ else if (secsDiff < 86400)
+ return (secsDiff/3600) + HOURS_AGO;
+ return aDateTime.toString("dd/MM/yyyy hh:mm:ss");
+}
View
@@ -0,0 +1,19 @@
+#ifndef PRETTYDATE_H
+#define PRETTYDATE_H
+
+#include <QDateTime>
+
+class PrettyDate
+{
+public:
+ static QString prettify(QDateTime);
+ static const QString JUST_NOW;
+ static const QString MINUTE_AGO;
+ static const QString MINUTES_AGO;
+ static const QString HOUR_AGO;
+ static const QString HOURS_AGO;
+private:
+ PrettyDate(){}
+};
+
+#endif // PRETTYDATE_H
View
@@ -29,6 +29,7 @@ or implied, of Humberto Pinheiro.*/
#include "project.h"
#include "timespan.h"
#include "project.h"
+#include "prettydate.h"
#include <QList>
#include <QStandardItemModel>
#include <QStandardItem>
@@ -81,9 +82,9 @@ void ProjectWidget::loadProjectDetails(Project *project)
QList<QStandardItem*> items;
while (it.hasPrevious()) {
TimeSpan *ts = it.previous();
- QStandardItem *beginning = new QStandardItem(ts->getStart().toString("MMM dd yyyy hh:mm:ss"));
+ QStandardItem *beginning = new QStandardItem(PrettyDate::prettify(ts->getStart()));
beginning->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
- QStandardItem *end = new QStandardItem(ts->getEnd().toString("MMM dd yyyy hh:mm:ss"));
+ QStandardItem *end = new QStandardItem(PrettyDate::prettify(ts->getEnd()));
end->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
QStandardItem *duration = new QStandardItem(ts->duration());
duration->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

0 comments on commit 64d8edf

Please sign in to comment.