/////////////////////////////////////////////////////////// // Bool.cs // Implementation of Class RelativeTimeFormatter // CSLA type: Read-only object // Created on: 10-Sept-2005 // Object design: John // Coded: 10-Sept-2005 /////////////////////////////////////////////////////////// using System; using System.Data; using CSLA.Data; using GZTW.Data; using CSLA; using System.Threading; using CSLA.Security; using System.Collections; namespace GZTW.AyaNova.BLL { /// /// Used to turn a date and time object /// into a string representation which is localized /// and relative to current date/time /// [Serializable] public class RelativeTimeFormatter : ReadOnlyBase { // Create a logger for use in this class //private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #region Attributes private string mYears=""; private string mMonths=""; private string mWeeks=""; private string mDays=""; private string mHours=""; private string mMinutes=""; private string mSeconds=""; private string mPast=""; private string mFuture=""; #endregion #region Constructor /// /// Private constructor to prevent direct instantiation /// private RelativeTimeFormatter() { } #endregion #region Business properties /// /// Convert relative date to string representation /// /// Date used as "now". /// this is a parameter rather than getting current date/time here /// because in a lengthy operation it could vary the display incorrectly /// Date to be reformatted into a relative string /// String representation of relative date i.e. "3 days 34 minutes ago" public string Format(DateTime currentdate, DateTime displaydate) { //take the date relative to now //and format into the correct string representation bool bPast=false; if(currentdate>displaydate) bPast=true; int Matches=0; string strReturn=""; // long years=DateDiff("yyyy",currentdate,displaydate); // // if(years!=0) // { // strReturn=AyaBizUtils.SS("",abs(years).ToString() + " " + this.mYears," "); // Matches++; // currentdate=currentdate.AddYears((int)years); // } // // long months=DateDiff("m",currentdate,displaydate); // if(months!=0) // { // strReturn += AyaBizUtils.SS("",abs(months).ToString() +" " + this.mMonths," "); // Matches++; // if(Matches>1) goto Done; // currentdate=currentdate.AddMonths((int)months); // } // long weeks=DateDiff("w",currentdate,displaydate); // if(weeks!=0) // { // strReturn += AyaBizUtils.SS("",weeks.ToString() + " " + this.mWeeks," "); // Matches++; // if(Matches>1) goto Done; // //currentdate=currentdate.AddWeeks(months); // } System.TimeSpan ts=currentdate.Subtract(displaydate); long days=ts.Days;//currentdate.Subtract(displaydate).Days; if(days!=0) { strReturn += AyaBizUtils.SS("",abs(days).ToString() + " " + this.mDays," "); Matches++; if(Matches>1) goto Done; currentdate=currentdate.AddDays(days); } long hours=ts.Hours;//currentdate.Subtract(displaydate).Hours; if(hours!=0) { strReturn += AyaBizUtils.SS("",abs(hours).ToString() + " " + this.mHours," "); Matches++; if(Matches>1) goto Done; currentdate=currentdate.AddHours(hours); } long minutes=ts.Minutes;//currentdate.Subtract(displaydate).Minutes; if(minutes!=0) { strReturn += AyaBizUtils.SS("",abs(minutes).ToString() + " " + this.mMinutes," "); Matches++; if(Matches>1) goto Done; currentdate=currentdate.AddMinutes(minutes); } long seconds=ts.Seconds;//currentdate.Subtract(displaydate).Seconds; if(seconds!=0) { strReturn += AyaBizUtils.SS("",abs(seconds).ToString() + " " + this.mSeconds," "); } Done: strReturn+=" " +(bPast ? this.mPast:this.mFuture); return strReturn; } /// /// simple abs function /// /// /// private long abs(long lvalue) { if(lvalue>0) return lvalue; return -lvalue; } /// /// Convert relative SmartDate to string representation /// /// Date used as "now". /// this is a parameter rather than getting current date/time here /// because in a lengthy operation it could vary the display incorrectly /// Date to be reformatted into a relative string /// String representation of relative date i.e. "3 days 34 minutes ago" /// Or an empty string if the date is empty public string Format(DateTime currentdate,SmartDate displaydate) { if(displaydate.IsEmpty) return ""; return Format(currentdate,displaydate.Date); } #endregion #region System.Object overrides // public override string ToString() // { // return mBoolValue.ToString(); // } // // /// // /// // public override bool Equals(Object obj) // { // if ( obj == null || GetType ( ) != obj.GetType ( ) ) return false; // RelativeTimeFormatter c=(RelativeTimeFormatter)obj; // return mBoolValue==c.mBoolValue; // } // // public override int GetHashCode() // { // return ("Bool" + mBoolValue.ToString()).GetHashCode(); // } #endregion #region Static methods /// /// Get all descriptive text for all time spans displayed in AyaNova in one trip to the database /// /// Locale desired /// public static RelativeTimeFormatter GetItem(string Language) { ////case 1039 //log.Debug("RelativeTimeFormatter.GetItem("+Language +")"); return (RelativeTimeFormatter)DataPortal.Fetch(new Criteria( Language)); } #endregion #region DAL DATA ACCESS /// /// protected override void DataPortal_Fetch(object Criteria) { Criteria crit = (Criteria)Criteria; SafeDataReader dr = null; try { DBCommandWrapper dbCommandWrapper = DBUtil.DB.GetSqlStringCommandWrapper( "SELECT aDisplayText, aDisplayTextCustom, aKey FROM aLocalizedText " + "WHERE (aKey LIKE @PARAM) AND (aLocale = @Locale) " + "ORDER BY aKey" ); dbCommandWrapper.AddInParameter("@PARAM",DbType.String,"UI.Label.TimeSpan.%"); dbCommandWrapper.AddInParameter("@Locale",DbType.String,crit.Language); dr = new SafeDataReader(DBUtil.DB.ExecuteReader(dbCommandWrapper)); string Text=""; string Key=""; while(dr.Read()) { //******************************************* //If there is no custom text then add the stock text if(dr.GetString("aDisplayTextCustom").Length<1) Text = dr.GetString("aDisplayText"); else//It's custom, add the custom text instead Text = dr.GetString("aDisplayTextCustom"); Key=dr.GetString("aKey"); switch(Key) { case "UI.Label.TimeSpan.Years": this.mYears=Text; break; case "UI.Label.TimeSpan.Months": this.mMonths=Text; break; case "UI.Label.TimeSpan.Weeks": this.mWeeks=Text; break; case "UI.Label.TimeSpan.Days": this.mDays=Text; break; case "UI.Label.TimeSpan.Hours": this.mHours=Text; break; case "UI.Label.TimeSpan.Minutes": this.mMinutes=Text; break; case "UI.Label.TimeSpan.Seconds": this.mSeconds=Text; break; case "UI.Label.TimeSpan.FutureRelative": this.mFuture=Text; break; case "UI.Label.TimeSpan.PastRelative": this.mPast=Text; break; } //******************************************* } } finally { if(dr!=null) dr.Close(); } } #endregion #region criteria /// /// Criteria for identifying existing object /// [Serializable] private class Criteria { public string Language; public Criteria(string _Language) { Language=_Language; } } #endregion }//end Class }//end Namespace