I'd like to determine a part of C# that took a long time…
For example,
var start=TimeSpan.Parse("19:00"); // This is fluctuating
var end = TimeSpan.Parse("3:00"); // The next day, at 3 o'clock, fluctuates here too
I'd like to know how many hours it is if there is a time between start and end.
Thank you for your cooperation.
c#
Probably, I would like to calculate the working hours at midnight.
Please refer to this article on VB.
I'm having trouble determining the time zone in VB2013.
Based on the above reference, the following is what I tried to make with C#.
Going to and from work requires not only the time, but also the date.
using System;
using System.Collections.General;
namespace ConsoleApp1
{
class program
{
static void Main (string[]args)
{
DateTimeworkStart = DateTime.Parse("2019-06-21 19:00");
DateTimeworkEnd=DateTime.Parse("2019-06-2203:00");
TimeSpanworkMidNight=CalcMidNightWork(workStart,workEnd);
Console.WriteLine("Workday and Time="+workStart.ToString());
Console.WriteLine("Date and Time to Leave ="+workEnd.ToString());
Console.WriteLine("Late Night="+workMidNight.ToString());
return;
}
// PAIR INFORMATION CLASS OF START/END TIME OF CALCULATION TARGET TIME
public class TimeSpanPair
{
public TimeSpan TSSstart {get;set;}
public TimeSpan TSEnd {get;set;}
}
// Calculated Time Zone List—Starting at 00:00 on the calculated date
staticList<TimeSpanPair>tsMidNight=newList<TimeSpanPair>(){
newTimeSpanPair() {TSStart=newTimeSpan(0,0,0), TSEnd=newTimeSpan(5,0,0)}, // 00:00 - 05:00
newTimeSpanPair() {TSStart=newTimeSpan(22,0,0), TSEnd=newTimeSpan(24,0,0)} // 22:00 - 24:00
};
/// <summary>
/// Acquisition of late-night work hours (multiple consecutive days can be calculated)
/// </summary>
/// <param name="startTime">Work Date & Time</param>
/// <param name="endTime">Date </param>
/// <returns>Late Night Hours</returns>
staticTimeSpan CalcMidNightWork (DateTime startTime, DateTime endTime)
{
TimeSpan result = new TimeSpan(0,0,0,0); // Late Night Working Hours Report Initial Value 00:00
DateTime workStart = startTime; // Work start time per applicable day
DateTimeworkDate=workStart.Date;// 00:00 on that day
DateTime tspStart = workDate; // For calculated (midnight) time zone start time tasks
DateTime tspEnd=workDate; // For calculated (midnight) time zone end-of-day operations
do
{
workDate=workStart.Date; // Set 00:00 for that day as the calculation base value
// Loops by the number of elements in the calculated (midnight) time zone list
foreach (TimeSpanPair tsp intsMidNight)
{
tspStart = workDate.Add(tssp.TSStart); // Set the start time of the calculated time zone
tspEnd=workDate.Add(tssp.TSEnd); // Set the end time of the calculated time zone
// Determine if the working hours are within the calculated time zone
if(!(workStart>=tsspEnd)||(endTime<=tsspStart)))
{
tspStart=(tspStart<workStart)?workStart:tspStart;//Adjust to actual start time
tspEnd=(tsspEnd>endTime)?endTime: tspEnd;//Adjust to actual end time
result+=(tsspEnd-tsspStart);// Calculate the hours worked during the calculated time zone and add it to the reported value
}
}
workStart=workDate.AddDays(1.0); // Start the next day at 00:00
} while(workStart<endTime);// Loops while working hours remain
return result;
}
}
}
var start=TimeSpan.Parse("19:00"); // This is fluctuating
var end = TimeSpan.Parse("3:00"); // The next day, at 3 o'clock, fluctuates here too
var day = new TimeSpan(1,0,0,0); // 1 day for the next day
TimeSpanans=end+day-start;
Can't I?
If the end time is simply less than the start time, add one day to calculate.
TimeSpan is defined by overloading operators such as +-<>, so
You can add and subtract as time.
var start=TimeSpan.Parse("19:00"); // This is fluctuating
var end = TimeSpan.Parse("3:00"); // The next day, at 3 o'clock, fluctuates here too
if(end<start){
end = end.Add (TimeSpan.FromDays(1));
// end+=TimeSpan.FromDays(1); may be
}
TimeSpandiff = end-start;
Console.WriteLine(diff.ToString(@".hh\:mm"));
I'd like to know how many hours it is if there is a time between start and end.
I overlooked it.
To determine if there is an intersection of sections
It's easy to find the starting maximum and ending minimum values.
TimeSpan CalcCross (TimeSpan begin, TimeSpan end)
{
if(end<begin){
end+=TimeSpan.FromDays(1);
}
TimeSpan m1 = TimeSpan.Parse ("22:00");
TimeSpan m2 = TimeSpan.Parse("1.05:00");
TimeSpan x1 = Max(begin,m1);
TimeSpan x2 = Min(end,m2);
TimeSpan cross=x2-x1;
if(cross<TimeSpan.Zero)returnTimeSpan.Zero;
else return cross;
}
TimeSpan Min (TimeSpant1, TimeSpant2)
{
if(t1>t2)return t2;
else return t1;
}
TimeSpan Max (TimeSpant1, TimeSpant2)
{
if(t1>t2)return t1;
else return t2;
}
[Test]
public void Test1()
{
// You can try five patterns.
Console.WriteLine (CalcCross(TimeSpan.Parse("9:00"), TimeSpan.Parse("19:00")); // Not overlapping 0:00:00
Console.WriteLine (CalcCross(TimeSpan.Parse("19:00"), TimeSpan.Parse("6:00")); // 7:00:00 including start and end
Console.WriteLine (CalcCross(TimeSpan.Parse("23:00"), TimeSpan.Parse("1:00")); // 2:00:00 including range
Console.WriteLine(CalcCross(TimeSpan.Parse("20:00"), TimeSpan.Parse("2:00")); // 4:00:00 with overlapping first half
Console.WriteLine(CalcCross(TimeSpan.Parse("23:00"), TimeSpan.Parse("6:00")); // Late overlapping 6:00:00
}
© 2024 OneMinuteCode. All rights reserved.