How to calculate workdays in the startup celebrity leave system

Source: Internet
Author: User

The new version of stardom adds a system that automatically calculates the length of time for asking for leave. This article discusses the implementation of the leave system algorithm. (If you have a good algorithm, please contact me .)

Before asking for leave, we assume that the company's working hours are from PM to PM. The middle hour is lunch time.

(1) first consider the day's leaveFor example, today is.

Assume that the user's leave is from 9:00:00 to 16:00:00

Then, if manually calculated, it should be divided into two ends:

(A) Calculate the period from to (3 hours)

(B) Calculate the period from to (3 hours)

That is, the user's leave is actually 3 + 3 for 6 hours.

However, the user's leave time is optional. He may choose from to, so the hour from to is not counted as leave.

If we use AB to represent the user's leave time, CD is used to represent the work and rest morning time, and EF is used to represent the work and rest afternoon time

Therefore, one day's leave is the intersection of two time periods, that is, the leave duration = AB cd + AB ∩ ef

Next we will consider the AB sequence CD, that is, the intersection of two time periods. We need to consider six situations: (for details, the common method for high school mathematics to calculate the set intersection .)

Therefore, we can write a method: get2timespan to get the length of two time periods (in minutes .)

        private double Get2TimeSpan(DateTime dt1, DateTime dt2, DateTime dt3, DateTime dt4)        {            //1.            if (dt4 <= dt1)                return 0;            //2.            if (dt3 >= dt2)                return 0;            //3             if (dt1 >= dt3 && dt1 <= dt4 && dt4 <= dt2)                return (dt4 - dt1).TotalMinutes;            //4            if (dt1 <= dt3 && dt3 <= dt2 && dt2 <= dt4)                return (dt2 - dt3).TotalMinutes;            //5            if (dt1 <= dt3 && dt4 <= dt2)                return (dt4 - dt3).TotalMinutes;            return (dt2 - dt1).TotalMinutes;        }


The above method is the intersection of two times. One day is actually divided into two in the morning and afternoon, so we can write a function to get the length of the intersection in one day.

        public double GetSameDayTimeSpan(DateTime dt1, DateTime dt2, DateTime base1, DateTime base2, DateTime base3, DateTime base4, DataTable table)        {            if (isWorkDay(dt1, table))            {                return Get2TimeSpan(dt1, dt2, base1, base2) + Get2TimeSpan(dt1, dt2, base3, base4);            }            else            {                return 0;            }        }


In this way, we can obtain and put the user's leave duration within one day.

 

 

(2) Cross-day leave

For cross-day leave, we need to know whether each day is a workday. Here, whether the work day is not entirely decided on Saturday or Sunday, because for example, the National Day 10.1, whether it is a work day or not, is a holiday.

Therefore, we need to determine whether a business day is a business day:

   public bool isWorkDay(DateTime dt, DataTable  table)        {            bool isworkday = false;            DataView dv = table.DefaultView;            dv.RowFilter = " sdate=#" + dt.ToString("yyyy-MM-dd") + "#";            if (dv.Count > 0)            {                isworkday = dv[0]["isworkday"].ToString() == "1" ? true : false;                return isworkday;            }            if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)            {                return false;            }            return true;        }

Here, the custom date has the highest priority. If no custom Date exists, the default date is used automatically.

In the start star leave system, the system determines whether to ask for leave every day. If it is a work day, the system directly returns the length of the day; otherwise, the system returns 0.

Int day = (odt2-odt1 ). days; for (INT I = 0; I <= day; I ++) {if (I = 0) {_ base = odt1.adddays (I ). tostring ("yyyy-mm-dd"); base1 = datetime. parse (_ base + "" + dtfrom1); base2 = datetime. parse (_ base + "" + dtto1); base3 = datetime. parse (_ base + "" + dtfrom2); base4 = datetime. parse (_ base + "" + dtto2); TDT = base4; totalminiter + = getsamedaytimespan (odt1, TdT, base1, base2, base3, base4, table2 );} else {if (I = day) {// last day _ base = odt1.adddays (I ). tostring ("yyyy-mm-dd"); base1 = datetime. parse (_ base + "" + dtfrom1); base2 = datetime. parse (_ base + "" + dtto1); base3 = datetime. parse (_ base + "" + dtfrom2); base4 = datetime. parse (_ base + "" + dtto2); TDT = base1; totalminiter + = getsamedaytimespan (base1, odt2, base1, base2, base3, base4, table2 );} else {TDT = odt1.adddays (I); If (Isworkday(TdT, table2) {totalminiter + = workdaytotalminiter;} else {totalminiter + = 0 ;}}}return totalminiter;


At this point, we have shared with you the workday algorithm in the Qixing leave system. If you have a good algorithm, contact us.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.