From 3c3d2cc150d5944a0905bea1e6bd86f5a4a96790 Mon Sep 17 00:00:00 2001 From: Colin Law Date: Sat, 31 Jan 2009 10:52:19 +0000 Subject: [PATCH] Make relative time format available for GitBlame --- src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp | 124 +++++++++++++++++----- src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h | 21 +++- src/TortoiseProc/AppUtils.cpp | 5 +- src/TortoiseProc/AppUtils.h | 12 ++- 4 files changed, 126 insertions(+), 36 deletions(-) diff --git a/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp b/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp index 0102526..f1067bf 100644 --- a/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp +++ b/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp @@ -17,6 +17,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // #include "StdAfx.h" +#include "math.h" #include "resource.h" #include "TortoiseGitBlameAppUtils.h" #include "Registry.h" @@ -32,45 +33,120 @@ CAppUtils::~CAppUtils(void) /** * FUNCTION : FormatDateAndTime * DESCRIPTION : Generates a displayable string from a CTime object in - * system short or long format dependant on setting of option - * as DATE_SHORTDATE or DATE_LONGDATE + * system short or long format or as a relative value + * cTime - the time + * option - DATE_SHORTDATE or DATE_LONGDATE + * bIncluedeTime - whether to show time as well as date + * bRelative - if true then relative time is shown if reasonable * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format * rather than locale * RETURN : CString containing date/time */ -CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/ ) +CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/, + bool bRelative /*=false*/) { CString datetime; - // should we use the locale settings for formatting the date/time? - if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE)) + if ( bRelative ) { - // yes - SYSTEMTIME sysTime; - cTime.GetAsSystemTime( sysTime ); - - TCHAR buf[100]; - - GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf, - sizeof(buf)/sizeof(TCHAR)-1); - datetime = buf; - if ( bIncludeTime ) - { - datetime += _T(" "); - GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1); - datetime += buf; - } + datetime = ToRelativeTimeString( cTime ); } else { - // no, so fixed format - if ( bIncludeTime ) + // should we use the locale settings for formatting the date/time? + if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE)) { - datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S")); + // yes + SYSTEMTIME sysTime; + cTime.GetAsSystemTime( sysTime ); + + TCHAR buf[100]; + + GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf, + sizeof(buf)/sizeof(TCHAR)-1); + datetime = buf; + if ( bIncludeTime ) + { + datetime += _T(" "); + GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1); + datetime += buf; + } } else { - datetime = cTime.Format(_T("%Y-%m-%d")); + // no, so fixed format + if ( bIncludeTime ) + { + datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S")); + } + else + { + datetime = cTime.Format(_T("%Y-%m-%d")); + } } } return datetime; } + +/** + * Converts a given time to a relative display string (relative to current time) + * Given time must be in local timezone + */ +CString CAppUtils::ToRelativeTimeString(CTime time) +{ + CString answer; + // convert to COleDateTime + SYSTEMTIME sysTime; + time.GetAsSystemTime( sysTime ); + COleDateTime oleTime( sysTime ); + answer = ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime()); + // change this to return answer when happy + return answer; +} + +/** + * Generates a display string showing the relative time between the two given times as COleDateTimes + */ +CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo) +{ + CString answer; + COleDateTimeSpan ts = RelativeTo - time; + //years + if(fabs(ts.GetTotalDays()) >= 3*365) + { + answer .FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365)); + } + //Months + if(fabs(ts.GetTotalDays()) >= 60) + { + answer.FormatMessage( _T("%1!d! Months ago"), (int)(ts.GetTotalDays()/30) ); + return answer; + } + //Weeks + if(fabs(ts.GetTotalDays()) >= 14) + { + answer.FormatMessage(_T("%1!d! Weeks ago"), (int)(ts.GetTotalDays()/7) ); + return answer; + } + //Days + if(fabs(ts.GetTotalDays()) >= 2) + { + answer.FormatMessage(_T("%1!d! Days ago"), (int)(ts.GetTotalDays()) ); + return answer; + } + //hours + if(fabs(ts.GetTotalHours()) >= 2) + { + answer.FormatMessage(_T("%1!d! Hours ago"), (int)(ts.GetTotalHours()) ); + return answer; + } + //minutes + if(fabs(ts.GetTotalMinutes()) >= 2) + { + answer.FormatMessage(_T("%1!d! Minutes ago"), (int)(ts.GetTotalMinutes()) ); + return answer; + } + //seconds + answer.FormatMessage(_T("%1!d! Seconds ago"), (int)(ts.GetTotalSeconds()) ); + return answer; +} + diff --git a/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h b/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h index ca38bac..1a49752 100644 --- a/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h +++ b/src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h @@ -30,12 +30,27 @@ public: /** * FUNCTION : FormatDateAndTime * DESCRIPTION : Generates a displayable string from a CTime object in - * system short or long format dependant on setting of option - * as DATE_SHORTDATE or DATE_LONGDATE. bIncludeTime (optional) includes time. + * system short or long format or as a relative value + * cTime - the time + * option - DATE_SHORTDATE or DATE_LONGDATE + * bIncluedeTime - whether to show time as well as date + * bRelative - if true then relative time is shown if reasonable + * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format + * rather than locale * RETURN : CString containing date/time */ - static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true ); + static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true, + bool bRelative=false ); + /** + * Converts a given time to a relative display string (relative to current time) + * Given time must be in local timezone + */ + static CString ToRelativeTimeString(CTime time); private: + /** + * Generates a display string showing the relative time between the two given times as COleDateTimes + */ + static CString ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo); }; diff --git a/src/TortoiseProc/AppUtils.cpp b/src/TortoiseProc/AppUtils.cpp index 5c6c335..a45a15c 100644 --- a/src/TortoiseProc/AppUtils.cpp +++ b/src/TortoiseProc/AppUtils.cpp @@ -1613,7 +1613,6 @@ CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIn /** * Converts a given time to a relative display string (relative to current time) * Given time must be in local timezone - * If more than a year ago or in the future then normal date/time is shown */ CString CAppUtils::ToRelativeTimeString(CTime time) { @@ -1629,8 +1628,6 @@ CString CAppUtils::ToRelativeTimeString(CTime time) /** * Generates a display string showing the relative time between the two given times as COleDateTimes - * time must be earlier than RelativeTo - * If more than a year ago or time > RelativeTo then an empty string is returned */ CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo) { @@ -1639,7 +1636,7 @@ CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeT //years if(fabs(ts.GetTotalDays()) >= 3*365) { - answer .FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365)); + answer.FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365)); } //Months if(fabs(ts.GetTotalDays()) >= 60) diff --git a/src/TortoiseProc/AppUtils.h b/src/TortoiseProc/AppUtils.h index 4d9afda..17e34e9 100644 --- a/src/TortoiseProc/AppUtils.h +++ b/src/TortoiseProc/AppUtils.h @@ -164,8 +164,13 @@ public: /** * FUNCTION : FormatDateAndTime * DESCRIPTION : Generates a displayable string from a CTime object in - * system short or long format dependant on setting of option - * as DATE_SHORTDATE or DATE_LONGDATE. bIncludeTime (optional) includes time. + * system short or long format or as a relative value + * cTime - the time + * option - DATE_SHORTDATE or DATE_LONGDATE + * bIncluedeTime - whether to show time as well as date + * bRelative - if true then relative time is shown if reasonable + * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format + * rather than locale * RETURN : CString containing date/time */ static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true, @@ -173,7 +178,6 @@ public: /** * Converts a given time to a relative display string (relative to current time) * Given time must be in local timezone - * If more than a year ago or in the future then normal date/time is shown */ static CString ToRelativeTimeString(CTime time); @@ -183,8 +187,6 @@ private: static bool GetMimeType(const CTGitPath& file, CString& mimetype); /** * Generates a display string showing the relative time between the two given times as COleDateTimes - * time must be earlier than RelativeTo - * If more than a year ago or time > RelativeTo then an empty string is returned */ static CString ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo); }; -- 2.11.0