दुनिया और उसके पति को कोड नमूने पोस्ट करने लगते हैं, इन कुछ उत्तरों के आधार पर मैंने कुछ समय पहले लिखा था।
मुझे इस कोड के लिए स्थानीयकरण योग्य होने की एक विशिष्ट आवश्यकता थी। तो मेरे पास दो वर्ग हैं? व्याकरण
, जो स्थानीयकरण योग्य शर्तों को निर्दिष्ट करता है, और FuzzyDateExtensions
, जिसमें विस्तार विधियों का एक समूह होता है। मुझे भविष्य के समय से निपटने की कोई जरूरत नहीं थी, इसलिए इस कोड के साथ उन्हें संभालने के लिए कोई प्रयास नहीं किया गया।
मैंने स्रोत में कुछ XMLdoc छोड़ा है, लेकिन ब्रेवटी के लिए सबसे अधिक हटा दिया गया है (जहां वे स्पष्ट होंगे)। मैंने यहां हर वर्ग के सदस्य को भी शामिल नहीं किया है:
public class Grammar
{
/// Gets or sets the term for "just now".
public string JustNow { get; set; }
/// Gets or sets the term for "X minutes ago".
///
/// This is a pattern, where {0}
/// is the number of minutes.
///
public string MinutesAgo { get; set; }
public string OneHourAgo { get; set; }
public string HoursAgo { get; set; }
public string Yesterday { get; set; }
public string DaysAgo { get; set; }
public string LastMonth { get; set; }
public string MonthsAgo { get; set; }
public string LastYear { get; set; }
public string YearsAgo { get; set; }
/// Gets or sets the term for "ages ago".
public string AgesAgo { get; set; }
///
/// Gets or sets the threshold beyond which the fuzzy date should be
/// considered "ages ago".
///
public TimeSpan AgesAgoThreshold { get; set; }
///
/// Initialises a new instance with the
/// specified properties.
///
private void Initialise(string justNow, string minutesAgo,
string oneHourAgo, string hoursAgo, string yesterday, string daysAgo,
string lastMonth, string monthsAgo, string lastYear, string yearsAgo,
string agesAgo, TimeSpan agesAgoThreshold)
{ ... }
}
FuzzyDateString
कक्षा में निम्न शामिल हैं:
public static class FuzzyDateExtensions
{
public static string ToFuzzyDateString(this TimeSpan timespan)
{
return timespan.ToFuzzyDateString(new Grammar());
}
public static string ToFuzzyDateString(this TimeSpan timespan,
Grammar grammar)
{
return GetFuzzyDateString(timespan, grammar);
}
public static string ToFuzzyDateString(this DateTime datetime)
{
return (DateTime.Now - datetime).ToFuzzyDateString();
}
public static string ToFuzzyDateString(this DateTime datetime,
Grammar grammar)
{
return (DateTime.Now - datetime).ToFuzzyDateString(grammar);
}
private static string GetFuzzyDateString(TimeSpan timespan,
Grammar grammar)
{
timespan = timespan.Duration();
if (timespan >= grammar.AgesAgoThreshold)
{
return grammar.AgesAgo;
}
if (timespan < new TimeSpan(0, 2, 0)) // 2 minutes
{
return grammar.JustNow;
}
if (timespan < new TimeSpan(1, 0, 0)) // 1 hour
{
return String.Format(grammar.MinutesAgo, timespan.Minutes);
}
if (timespan < new TimeSpan(1, 55, 0)) // 1 hour 55 minutes
{
return grammar.OneHourAgo;
}
if (timespan < new TimeSpan(12, 0, 0) // 12 hours
&& (DateTime.Now - timespan).IsToday())
{
return String.Format(grammar.HoursAgo, timespan.RoundedHours());
}
if ((DateTime.Now.AddDays(1) - timespan).IsToday())
{
return grammar.Yesterday;
}
if (timespan < new TimeSpan(32, 0, 0, 0) // 32 days
&& (DateTime.Now - timespan).IsThisMonth())
{
return String.Format(grammar.DaysAgo, timespan.RoundedDays());
}
if ((DateTime.Now.AddMonths(1) - timespan).IsThisMonth())
{
return grammar.LastMonth;
}
if (timespan < new TimeSpan(365, 0, 0, 0, 0) // 365 days
&& (DateTime.Now - timespan).IsThisYear())
{
return String.Format(grammar.MonthsAgo, timespan.RoundedMonths());
}
if ((DateTime.Now - timespan).AddYears(1).IsThisYear())
{
return grammar.LastYear;
}
return String.Format(grammar.YearsAgo, timespan.RoundedYears());
}
}
उन महत्वपूर्ण चीजों में से एक जो मैं प्राप्त करना चाहता था, साथ ही स्थानीयकरण, यह था कि "आज" का अर्थ केवल "यह कैलेंडर दिन" होगा, इसलिए IsToday
, IsThisMonth
, < कोड> IsThisYear विधियां इस तरह दिखती हैं:
public static bool IsToday(this DateTime date)
{
return date.DayOfYear == DateTime.Now.DayOfYear && date.IsThisYear();
}
और गोल करने के तरीके इस तरह हैं (मैंने RoundedMonths
शामिल किया है, क्योंकि यह थोड़ा अलग है):
public static int RoundedDays(this TimeSpan timespan)
{
return (timespan.Hours > 12) ? timespan.Days + 1 : timespan.Days;
}
public static int RoundedMonths(this TimeSpan timespan)
{
DateTime then = DateTime.Now - timespan;
// Number of partial months elapsed since 1 Jan, AD 1 (DateTime.MinValue)
int nowMonthYears = DateTime.Now.Year * 12 + DateTime.Now.Month;
int thenMonthYears = then.Year * 12 + then.Month;
return nowMonthYears - thenMonthYears;
}
मुझे आशा है कि लोगों को यह उपयोगी और / या दिलचस्प लगेगा: ओ)