Skip to content

Commit 227eac3

Browse files
committed
change/optimize GetElapsedTime #54
1 parent 006f300 commit 227eac3

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

Diff for: UnityLauncherPro/Tools.cs

+28-30
Original file line numberDiff line numberDiff line change
@@ -1397,44 +1397,42 @@ public static Dictionary<string, string> ScanTemplates(string unityInstallPath)
13971397
return items;
13981398
}
13991399

1400-
// https://codereview.stackexchange.com/a/93247
1400+
// chatgpt
14011401
public static string GetElapsedTime(DateTime datetime)
14021402
{
1403-
TimeSpan ts = DateTime.Now.Subtract(datetime);
1403+
TimeSpan ts = DateTime.Now - datetime;
14041404

1405-
// The trick: make variable contain date and time representing the desired timespan,
1406-
// having +1 in each date component.
1407-
DateTime date = DateTime.MinValue + ts;
1408-
1409-
return ProcessPeriod(date.Year - 1, date.Month - 1, "year")
1410-
?? ProcessPeriod(date.Month - 1, date.Day - 1, "month")
1411-
?? ProcessPeriod(date.Day - 1, date.Hour, "day", "Yesterday")
1412-
?? ProcessPeriod(date.Hour, date.Minute, "hour")
1413-
?? ProcessPeriod(date.Minute, date.Second, "minute")
1414-
?? ProcessPeriod(date.Second, 0, "second")
1415-
?? "Right now";
1416-
}
1417-
1418-
private static string ProcessPeriod(int value, int subValue, string name, string singularName = null)
1419-
{
1420-
if (value == 0)
1405+
if (ts.TotalSeconds < 60)
14211406
{
1422-
return null;
1407+
return ts.TotalSeconds < 2 ? "Right now" : $"{(int)ts.TotalSeconds} seconds ago";
1408+
}
1409+
else if (ts.TotalMinutes < 60)
1410+
{
1411+
return ts.TotalMinutes < 2 ? "1 minute ago" : $"{(int)ts.TotalMinutes} minutes ago";
1412+
}
1413+
else if (ts.TotalHours < 24)
1414+
{
1415+
return ts.TotalHours < 2 ? "1 hour ago" : $"{(int)ts.TotalHours} hours ago";
14231416
}
1424-
if (value == 1)
1417+
else if (ts.TotalDays < 30)
14251418
{
1426-
if (!String.IsNullOrEmpty(singularName))
1419+
return ts.TotalDays < 2 ? "1 day ago" : $"{(int)ts.TotalDays} days ago";
1420+
}
1421+
else if (ts.TotalDays < 365)
1422+
{
1423+
if (ts.TotalDays < 60)
1424+
{
1425+
return "1 month ago";
1426+
}
1427+
else
14271428
{
1428-
return singularName;
1429+
return $"{(int)(ts.TotalDays / 30)} months ago";
14291430
}
1430-
string articleSuffix = name[0] == 'h' ? "n" : String.Empty;
1431-
return subValue == 0
1432-
? String.Format("A{0} {1} ago", articleSuffix, name)
1433-
: String.Format("a{0} {1} ago", articleSuffix, name);
1434-
}
1435-
return subValue == 0
1436-
? String.Format("{0} {1}s ago", value, name)
1437-
: String.Format("{0} {1}s ago", value, name);
1431+
}
1432+
else
1433+
{
1434+
return ts.TotalDays < 730 ? "1 year ago" : $"{(int)(ts.TotalDays / 365)} years ago";
1435+
}
14381436
}
14391437

14401438
public static bool ValidateDateFormat(string format)

0 commit comments

Comments
 (0)