I created some code and classes to get a random Hour and Minute between current time (NOW) and the End of the Day. My code works, but some times the times are wrong.
Lets say it's 11:30 PM and I run the code, I get WRONG output, where the write output is shown below... The values Must be in order from NOW (when executed) to End of the Current Day.
Does anyone know what I'm doing wrong?
WRONG (hour/minute):
- 0:23 - 12:23 AM (next day)
- 23:15 - 11:15 PM
RIGHT (hour/minute):
- 23:15 - 11:15 PM
- 0:23 - 12:23 AM (next day)
public class MyHourMinuteTestClass
{
ArrayList projects = new ArrayList();
TimeSpan start = DateTime.Now.TimeOfDay;
TimeSpan s = TimeSpan.FromHours(DateTime.Now.Hour);
TimeSpan e = TimeSpan.FromHours(23.99);
int maxMin = (int)((e - s).TotalMinutes);
public MyHourMinuteTestClass()
{
}
public void TestValues()
{
// create 2 sets of values and add a new SocialTimes instance containing the hour/minute to the array list
for(int i = 0; i < 2; i++)
{
int minutes = randomGen.Next(maxMin);
TimeSpan t = start.Add(TimeSpan.FromMinutes(minutes));
int PostTimeHour = t.Hours;
int PostTimeMinutes = t.Minutes;
projects.Add(new SocialTimes(NowDateStr, PostTimeHour, PostTimeMinutes));
}
// then I sort the projects array list
projects.Sort(new hoursMinutesComparer());
}
}
public class SocialTimes
{
string DateToPostStr = "";
public int HourToPost = 0;
public int MinuteToPost = 0;
public SocialTimes(string DateToPostStr, int HourToPost, int MinuteToPost)
{
this.DateToPostStr = DateToPostStr;
this.HourToPost = HourToPost;
this.MinuteToPost = MinuteToPost;
}
public string getDateToPostStr()
{
return this.DateToPostStr;
}
public int getHourToPost()
{
return this.HourToPost;
}
public int getMinuteToPost()
{
return this.MinuteToPost;
}
public int getHourMinuteToPost()
{
int sum;
sum = HourToPost * 60 + MinuteToPost;
return sum;
}
}
public class hoursMinutesComparer : IComparer
{
int IComparer.Compare(Object xx, Object yy)
{
SocialTimes x = (SocialTimes)xx;
SocialTimes y = (SocialTimes)yy;
return x.getHourMinuteToPost().CompareTo(y.getHourMinuteToPost());
}
}