Fixed issue #4126: Capitalize the first letter in the Push dialog
[TortoiseGit.git] / src / TortoiseProc / LoglistUtils.cpp
blob73eae8b6ecf12b11b6fac043dd11098f1cc54db4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012, 2014, 2016-2017, 2019 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "../Resources/LoglistCommonResource.h"
22 #include "LoglistUtils.h"
23 #include "registry.h"
25 CLoglistUtils::CLoglistUtils()
29 CLoglistUtils::~CLoglistUtils()
33 /**
34 * FUNCTION : FormatDateAndTime
35 * DESCRIPTION : Generates a displayable string from a CTime object in
36 * system short or long format or as a relative value
37 * cTime - the time
38 * option - DATE_SHORTDATE or DATE_LONGDATE
39 * bIncluedeTime - whether to show time as well as date
40 * bRelative - if true then relative time is shown if reasonable
41 * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format
42 * rather than locale
43 * RETURN : CString containing date/time
45 CString CLoglistUtils::FormatDateAndTime(const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/, bool bRelative /*=false*/)
47 if (bRelative)
49 return ToRelativeTimeString(cTime);
51 else
53 static bool useSystemLocales = CRegDWORD(L"Software\\TortoiseGit\\UseSystemLocaleForDates", TRUE) != FALSE;
54 // should we use the locale settings for formatting the date/time?
55 if (useSystemLocales)
57 // yes
58 SYSTEMTIME sysTime;
59 cTime.GetAsSystemTime(sysTime);
61 wchar_t buf[100] = { 0 };
63 GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, nullptr, buf, _countof(buf) - 1);
64 CString datetime = buf;
65 if (bIncludeTime)
67 datetime += L' ';
68 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, nullptr, buf, _countof(buf) - 1);
69 datetime += buf;
71 return datetime;
73 else
75 // no, so fixed format
76 if (bIncludeTime)
77 return cTime.Format(L"%Y-%m-%d %H:%M:%S");
78 else
79 return cTime.Format(L"%Y-%m-%d");
84 /**
85 * Converts a given time to a relative display string (relative to current time)
86 * Given time must be in local timezone
88 CString CLoglistUtils::ToRelativeTimeString(CTime time)
90 // convert to COleDateTime
91 SYSTEMTIME sysTime;
92 time.GetAsSystemTime(sysTime);
93 COleDateTime oleTime(sysTime);
94 return ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());
97 /**
98 * Generates a display string showing the relative time between the two given times as COleDateTimes
100 CString CLoglistUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)
102 COleDateTimeSpan ts = RelativeTo - time;
104 //years
105 if(fabs(ts.GetTotalDays()) >= 3 * 365)
106 return ExpandRelativeTime(static_cast<int>(ts.GetTotalDays()) / 365, IDS_YEAR_AGO, IDS_YEARS_AGO);
108 //Months
109 if(fabs(ts.GetTotalDays()) >= 60)
110 return ExpandRelativeTime(static_cast<int>(ts.GetTotalDays()) / 30, IDS_MONTH_AGO, IDS_MONTHS_AGO);
112 //Weeks
113 if(fabs(ts.GetTotalDays()) >= 14)
114 return ExpandRelativeTime(static_cast<int>(ts.GetTotalDays()) / 7, IDS_WEEK_AGO, IDS_WEEKS_AGO);
116 //Days
117 if(fabs(ts.GetTotalDays()) >= 2)
118 return ExpandRelativeTime(static_cast<int>(ts.GetTotalDays()), IDS_DAY_AGO, IDS_DAYS_AGO);
120 //hours
121 if(fabs(ts.GetTotalHours()) >= 2)
122 return ExpandRelativeTime(static_cast<int>(ts.GetTotalHours()), IDS_HOUR_AGO, IDS_HOURS_AGO);
124 //minutes
125 if(fabs(ts.GetTotalMinutes()) >= 2)
126 return ExpandRelativeTime(static_cast<int>(ts.GetTotalMinutes()), IDS_MINUTE_AGO, IDS_MINUTES_AGO);
128 //seconds
129 return ExpandRelativeTime(static_cast<int>(ts.GetTotalSeconds()), IDS_SECOND_AGO, IDS_SECONDS_AGO);
133 * Passed a value and two resource string ids
134 * if count is 1 then FormatString is called with format_1 and the value
135 * otherwise format_2 is used
136 * the formatted string is returned
138 CString CLoglistUtils::ExpandRelativeTime(int count, UINT format_1, UINT format_n)
140 CString answer;
141 if (count == 1)
142 answer.FormatMessage(format_1, count);
143 else
144 answer.FormatMessage(format_n, count);
146 return answer;