This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
forked from JeremySkinner/git-dot-aspx
/
Helpers.cs
83 lines (65 loc) · 2.56 KB
/
Helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#region License
// Copyright 2010 Jeremy Skinner (http://www.jeremyskinner.co.uk)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// The latest version of this file can be found at http://github.com/JeremySkinner/git-dot-aspx
#endregion
namespace GitAspx {
using System;
using System.ComponentModel;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
public static class Helpers {
public static string ProjectUrl(this UrlHelper urlHelper, string project) {
return urlHelper.RouteUrl("project", new RouteValueDictionary(new {project}),
urlHelper.RequestContext.HttpContext.Request.Url.Scheme,
urlHelper.RequestContext.HttpContext.Request.Url.Host);
}
public static string ToPrettyDateString(this DateTime d) {
TimeSpan s = DateTime.Now.Subtract(d);
int dayDiff = (int)s.TotalDays;
int secDiff = (int)s.TotalSeconds;
if (dayDiff < 0 || dayDiff >= 31)
return string.Format("{0:MMMM d, yyyy}", d);
if (dayDiff == 0) {
if (secDiff < 60)
return "just now";
if (secDiff < 120)
return "1 minute ago";
if (secDiff < 3600)
return string.Format("{0} minutes ago", Math.Floor((double)secDiff / 60));
if (secDiff < 7200)
return "1 hour ago";
if (secDiff < 86400)
return string.Format("{0} hours ago", Math.Floor((double)secDiff / 3600));
}
if (dayDiff == 1)
return "yesterday";
if (dayDiff < 7)
return string.Format("{0} days ago", dayDiff);
if (dayDiff < 31)
return string.Format("{0} weeks ago", Math.Ceiling((double)dayDiff / 7));
return null;
}
public static string With(this string format, params string[] args) {
return string.Format(format, args);
}
public static void WriteNoCache(this HttpResponseBase response) {
response.AddHeader("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
response.AddHeader("Pragma", "no-cache");
response.AddHeader("Cache-Control", "no-cache, max-age=0, must-revalidate");
}
}
}