Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding Time
Message
From
25/02/2010 08:34:05
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
24/02/2010 18:31:45
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01450882
Message ID:
01450938
Views:
57
>I am trying to add time values [HHHH:MM] that are in entity rows and I was wondering if anyone has seen a method to do this. I am aware of the TimeSpan() functions, etc. but these all roll-over the hours when it hits the 23 + 1 to Days. I need to add values like 123:15 + 50:46, etc which would equal 174:01 where the minutes roll into the hours but hours do not roll into days. I have a method in Visual FoxPro that does this very well but I did not want to reinvent the wheel in .NET if I do not have to.

There are many ways to do that in .Net. If you would have your own custom representation like that then create your own struct and use it. ie:
struct myTime
{
  private int hours,minutes;
  public int Hours {get {return this.hours;}set{this.hours=value;}}
  public int Minutes { 
  get{ return minutes; }
  set{ minutes=value%60;  hours+=value/60;}}
  
  // constructing from a string
  public myTime(string s)
  {
    string[] parts = s.Split( new char[] {':'} );
    this.hours = int.Parse(parts[0]);
    this.minutes = int.Parse(parts[1]); 
  }
  // constructing from hours and minutes
  public myTime(int hours, int minutes)
  {
	this.hours = hours;
	this.minutes = minutes; 
  }

  // overriding string representation of this struct
  public override string ToString()
  {
     return String.Format( "{0}:{1}",
     this.Hours,
     this.Minutes.ToString().PadLeft(2,'0'));
  }

  // overloading the + operator to support adding two myTime structs
  // like adding date and integer (days) yielding a date 
  // or subtracting date from another date yielding an integer in VFP
  public static myTime operator +(myTime t1, myTime t2)
  { 
    int totalMins = t1.Minutes + t2.Minutes;
    int addHours = totalMins / 60; 
    return new myTime( t1.Hours + t2.Hours + addHours, totalMins%60 );
  }
}
Having a struct like that at hand you could simply do:

string s1 = "123:15";
string s2 = "50:46";

myTime t1 = new myTime(s1);
myTime t2 = new myTime(s2);

Console.WriteLine( "{0} + {1} = {2}", t1, t2, t1+t2);

or you could get the power of Linq to sum your 'time' values in your entity rows. Something like this:
myTime[] times = new myTime[] {
  new myTime("123:15"),
  new myTime("50:46"),
  new myTime("10:50"),
  new myTime("1:50")
  };
  
myTime totalTime = times.Aggregate( (current,next)  => current + next );
Console.WriteLine( "Total time taken to blah blah is {0}\nTotal hours was {1} and total minutes was", 
  totalTime, totalTime.Hours, totalTime.Minutes);
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform