X-Git-Url: http://git.sourceforge.jp/view?p=tortoisegit%2FTortoiseGitJp.git;a=blobdiff_plain;f=src%2FTortoiseGitBlame%2FTortoiseGitBlameAppUtils.cpp;h=f1067bfb2a9096cee44ecfc5b0b28adafbb0cb2f;hp=01025266a25f39cc9a4fcc7090fcb7abc75f768e;hb=3c3d2cc150d5944a0905bea1e6bd86f5a4a96790;hpb=1b5f5c490b3c8de8d20f5b63704a060b2282b123 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; +} +