1 // TortoiseGit - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2008 - TortoiseGit
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
21 #include "resource.h"
\r
22 #include "TortoiseGitBlameAppUtils.h"
\r
23 #include "Registry.h"
\r
25 CAppUtils::CAppUtils(void)
\r
29 CAppUtils::~CAppUtils(void)
\r
34 * FUNCTION : FormatDateAndTime
\r
35 * DESCRIPTION : Generates a displayable string from a CTime object in
\r
36 * system short or long format or as a relative value
\r
38 * option - DATE_SHORTDATE or DATE_LONGDATE
\r
39 * bIncluedeTime - whether to show time as well as date
\r
40 * bRelative - if true then relative time is shown if reasonable
\r
41 * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format
\r
42 * rather than locale
\r
43 * RETURN : CString containing date/time
\r
45 CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/,
\r
46 bool bRelative /*=false*/)
\r
51 datetime = ToRelativeTimeString( cTime );
\r
55 // should we use the locale settings for formatting the date/time?
\r
56 if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE))
\r
60 cTime.GetAsSystemTime( sysTime );
\r
64 GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf,
\r
65 sizeof(buf)/sizeof(TCHAR)-1);
\r
69 datetime += _T(" ");
\r
70 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1);
\r
76 // no, so fixed format
\r
79 datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S"));
\r
83 datetime = cTime.Format(_T("%Y-%m-%d"));
\r
91 * Converts a given time to a relative display string (relative to current time)
\r
92 * Given time must be in local timezone
\r
94 CString CAppUtils::ToRelativeTimeString(CTime time)
\r
97 // convert to COleDateTime
\r
99 time.GetAsSystemTime( sysTime );
\r
100 COleDateTime oleTime( sysTime );
\r
101 answer = ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());
\r
106 * Generates a display string showing the relative time between the two given times as COleDateTimes
\r
108 CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)
\r
111 COleDateTimeSpan ts = RelativeTo - time;
\r
113 if(fabs(ts.GetTotalDays()) >= 3*365)
\r
115 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/365, IDS_YEAR_AGO, IDS_YEARS_AGO );
\r
118 if(fabs(ts.GetTotalDays()) >= 60)
\r
120 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/30, IDS_MONTH_AGO, IDS_MONTHS_AGO );
\r
124 if(fabs(ts.GetTotalDays()) >= 14)
\r
126 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/7, IDS_WEEK_AGO, IDS_WEEKS_AGO );
\r
130 if(fabs(ts.GetTotalDays()) >= 2)
\r
132 answer = ExpandRelativeTime( (int)ts.GetTotalDays(), IDS_DAY_AGO, IDS_DAYS_AGO );
\r
136 if(fabs(ts.GetTotalHours()) >= 2)
\r
138 answer = ExpandRelativeTime( (int)ts.GetTotalHours(), IDS_HOUR_AGO, IDS_HOURS_AGO );
\r
142 if(fabs(ts.GetTotalMinutes()) >= 2)
\r
144 answer = ExpandRelativeTime( (int)ts.GetTotalMinutes(), IDS_MINUTE_AGO, IDS_MINUTES_AGO );
\r
148 answer = ExpandRelativeTime( (int)ts.GetTotalSeconds(), IDS_SECOND_AGO, IDS_SECONDS_AGO );
\r
153 * Passed a value and two resource string ids
\r
154 * if count is 1 then FormatString is called with format_1 and the value
\r
155 * otherwise format_2 is used
\r
156 * the formatted string is returned
\r
158 CString CAppUtils::ExpandRelativeTime( int count, UINT format_1, UINT format_n )
\r
163 answer.FormatMessage( format_1, count );
\r
167 answer.FormatMessage( format_n, count );
\r