andrew4582

BirthdayInfo

Apr 10th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. struct BirthdayInfo {
  2.  
  3.             DateTime _today;
  4.             DateTime _birthday;
  5.             TimeSpan _fromToday;
  6.             string _name;
  7.  
  8.             public BirthdayInfo(DateTime birthday,string name) {
  9.                 _today = DateTime.Now;
  10.                 _birthday = birthday;
  11.                 _fromToday = TimeSpan.Zero;
  12.                 _name = name;
  13.             }
  14.             public BirthdayInfo(DateTime birthday)
  15.                 : this(birthday,null) {
  16.                 _name = null;
  17.             }
  18.  
  19.             public string Name {
  20.                 get { return _name; }
  21.                 set { _name = value; }
  22.             }
  23.  
  24.             public DateTime Today {
  25.                 get { return _today; }
  26.                 set {
  27.                     _today = value;
  28.                     _fromToday = TimeSpan.Zero;
  29.                 }
  30.             }
  31.             public DateTime Birthday {
  32.                 get { return _birthday; }
  33.                 set {
  34.                     _birthday = value;
  35.                     _fromToday = TimeSpan.Zero;
  36.                 }
  37.             }
  38.  
  39.             public int YearsFromToday {
  40.                 get {
  41.  
  42.                     DateTime thisYear = new DateTime(Today.Year,Birthday.Month,Birthday.Day,Birthday.Hour,Birthday.Minute,0);
  43.  
  44.                     if(Today > thisYear)
  45.                         return thisYear.Year - Birthday.Year;
  46.                     else
  47.                         return (thisYear.Year - Birthday.Year) - 1;
  48.                 }
  49.             }
  50.             public int DaysFromToday {
  51.                 get {
  52.                     return this.FromToday.Days;
  53.                 }
  54.             }
  55.  
  56.             public int MinutesFromToday {
  57.                 get {
  58.                     return (int)this.FromToday.TotalMinutes;
  59.                 }
  60.             }
  61.  
  62.             public long SecondsFromToday {
  63.                 get {
  64.                     return (long)this.FromToday.TotalSeconds;
  65.                 }
  66.             }
  67.  
  68.             public TimeSpan FromToday {
  69.                 get {
  70.                     if(_fromToday == TimeSpan.Zero)
  71.                         _fromToday = _today.Subtract(_birthday);
  72.                     return _fromToday;
  73.                 }
  74.             }
  75.  
  76.             public override string ToString() {
  77.  
  78.                 var info = this;
  79.                 StringBuilder builder = new StringBuilder();
  80.                 Action<string,object> output = (a,b) => builder.AppendLine(string.Format("{0,-20}  =  {1,20}",a,b));
  81.  
  82.                 output("{0}'s BirthDay".FormatWith(info.Name),info.Birthday);
  83.                 output("Years since birth",info.YearsFromToday.ToString("#,#") + " years");
  84.                 output("Days since birth",info.DaysFromToday.ToString("#,#") + " days");
  85.                 output("Minutes since birth",info.DaysFromToday.ToString("#,#") + " minutes");
  86.                 output("Seconds since birth",info.SecondsFromToday.ToString("#,#") + " seconds");
  87.                 return builder.ToString();
  88.             }
  89.         }
Advertisement
Add Comment
Please, Sign In to add comment