Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if time is within A set of range?

I am creating an Attendance Application and I am looking for a way to check if their attendance logs is within an hour of their Schedule

Lets say Employee1 has a schedule of January 31, 2019 @ 00:30 but he arrives and checks in ahead of time so his logs state that he checks in at January 30, 2019 @ 23:45. So I am trying to find that if an employee logs in within an hour ahead or an hour late that will be his log in time. Like he can log in between Jan 30, 2019 23:30 till Jan 31, 2019 01:30.

What I did at the moment is that I have 4 more DateTime variables that handle these but it doesn't seem to work.

So this is my Employee Class

public class Emp1
    {
        public int ID { get; set; }
        public DateTime In { get; set; }
        public DateTime Out { get; set; }
        public DateTime dateTime1 { get; set; }
        public DateTime dateTime2 { get; set; }
        public DateTime dateTime3 { get; set; }
        public DateTime dateTime4 { get; set; }
    }

my Actual Attendance Logs class

public class Actual
    {
        public int ID { get; set; }
        public DateTime ActualLog { get; set; }
        public int LogStatus { get; set; }
        public DateTime date { get; set; }
    }

So this is my code on how I am populating my Employee Schedule

List<Emp1> em = new List<Emp1>();

                foreach (DataRow row in empContainer.Rows)
                {
                    em.Add(new Emp1()
                    {
                        ID = Convert.ToInt32(row[0].ToString()),
                        In = Convert.ToDateTime(row[1].ToString()),
                        Out = Convert.ToDateTime(row[2].ToString()),
                        dateTime1 = Convert.ToDateTime(row[1].ToString()).AddHours(1),
                        dateTime2 = Convert.ToDateTime(row[1].ToString()).AddHours(-1),
                        dateTime3 = Convert.ToDateTime(row[2].ToString()).AddHours(1),
                        dateTime4 = Convert.ToDateTime(row[2].ToString()).AddHours(-1)
                    });
                }

And this is my current LINQ statement to populate my DataGrid

public void Main()
        {
            List<Emp1> emps;
            List<Actual> actuals;

            actuals = emp.GetActual(@"C:\Users\IT\Desktop\Sample\SampleActual.dat");
            emps = GetEmpSched();



            var final = (from a1 in actuals
                         join a2 in actuals on a1.ID equals a2.ID
                         join t1 in emps on a1.ID equals t1.ID
                         join t2 in emps on a2.ID equals t2.ID
                         where ((a1.LogStatus == 0) && (a2.LogStatus == 1)) && ((t1.In == t2.In) && (t1.Out==t2.Out)) && ((t1.dateTime1 > a1.ActualLog) && (a1.ActualLog > t1.dateTime2)) && ((t2.dateTime3 > a2.ActualLog) && (a2.ActualLog > t2.dateTime4))
                         select new
                         {
                             User_ID = t1.ID,
                             Scheduled_In = t1.In,
                             Actual_Login = a1.ActualLog,
                             Scheduled_Out = t2.Out,
                             Actual_Out = a2.ActualLog
                         }).Distinct(). ToList();

            tbContainer = StaticClasses.ToDataTable(final);

            dgvAttendance.ItemsSource = emp.CalculateEmployeeAttendance(tbContainer);
        }

This is my employee schedule source

Employee Schedule

And this is my Employee Attendance Logs

Attendance Logs

As you see I have 4 extra DateTime variables that I just added to handle the extra or less hours, but it still doesn't work which I have no Idea why.

And this is the current output Actual Output

So I am missing data because of my conditions, which should work fine but producing erroneous output.

Any ideas why is this happening?

Any answers that would help me out would be great. Thanks

like image 479
Nate Avatar asked Nov 07 '22 21:11

Nate


1 Answers

I think you are trying the hard way, this should be more simple.

To compare the actual log In time with the scheduled one and check if it's within an hour you can just substract both and check the difference.

It's easy with c#:

    var scheduledLogIn = new TimeSpan(8, 0, 0);
    var actualLogIn = new DateTime(2019, 1, 31, 8, 15, 27);

    var delay = (actualLogIn.TimeOfDay - scheduledLogIn).Duration();

    if (delay.TotalHours < 1)
    {
        // It's ok
    } else
    {
        // more than 1h earlier or late!
    }

If you are interested on checking if it's actually earlier or late, remove Duration and check if TotalHours is negative/positive.

like image 69
agascon Avatar answered Nov 12 '22 14:11

agascon