c# - Have multiple check in/out/lunch time and get total hours -


i have 2 date time picker. assume in database saved data needed

//status      //date      //time      //name ---------------------------------------------- check in     1/8/2016    12:30:36pm     ali   lunch      1/8/2016     2:40:36pm     ali check in     1/8/2016     3:40:36pm     ali check out    1/8/2016     6:40:36pm     ali 

as don't want calculate lunch time. want calculate employee work day

6:40:36 pm - 12:30:36pm = 6 //total hours worked include lunch 

so have subtract lunch - checkk in take 1 hours

6 - (3:40:36 pm - 2:40:36 pm) = 5 hours //total hours worked 

what kind of logic should have this? know type of sql clause pick database. need way can calculate easier without implementing huge lines of codes.

this isn't particularly robust , refactoring, might give starting point extend own logic.

your states enum:

public enum status     {         checkin,         checkout,         lunch     } 

turn data list of these:

public class employeestatuschange     {         public int employeeid { get; set; }          public datetime datetime { get; set; }          public status status { get; set; }     } 

and use extension method:

public static double calculatehours(this list<employeestatuschange> changes)     {         changes = changes.orderby(x => x.datetime).tolist();          double hours = 0;         var start = datetime.minvalue;         var lunch = datetime.minvalue;         var checkedout = false;          foreach (var change in changes)         {             // exclude before first check in, or if have checked out             if ((start == datetime.minvalue && change.status != status.checkin) || checkedout)             {                 continue;             }              // set start time             if (start == datetime.minvalue && change.status == status.checkin)             {                 start = change.datetime;                 continue;             }              switch (change.status)             {                 case status.checkin:                     if (lunch == datetime.minvalue)                     {                         continue;                     }                      start = change.datetime;                     continue;                  case status.lunch:                     lunch = change.datetime;                     hours += (change.datetime - start).totalhours;                     break;                  case status.checkout:                     checkedout = true;                     hours += (change.datetime - start).totalhours;                     break;             }         }          return hours;     } 

this return 6.5:

var items = new list<employeestatuschange>(); items.add(new employeestatuschange { employeeid = 1, status = status.checkin, datetime = new datetime(2015, 1, 1, 9, 0, 0) }); items.add(new employeestatuschange { employeeid = 1, status = status.lunch, datetime = new datetime(2015, 1, 1, 10, 30, 0) }); items.add(new employeestatuschange { employeeid = 1, status = status.checkin, datetime = new datetime(2015, 1, 1, 11, 0, 0) }); items.add(new employeestatuschange { employeeid = 1, status = status.lunch, datetime = new datetime(2015, 1, 1, 12, 0, 0) }); items.add(new employeestatuschange { employeeid = 1, status = status.checkin, datetime = new datetime(2015, 1, 1, 13, 0, 0) }); items.add(new employeestatuschange { employeeid = 1, status = status.checkout, datetime = new datetime(2015, 1, 1, 17, 0, 0) }); items.calculatehours(); 

Comments