Time Calculation as shift in iOS

Discussion in 'iOS Programming' started by arungre, Nov 25, 2013.

  1. arungre, Nov 25, 2013
    Last edited by a moderator: Nov 25, 2013

    arungre macrumors newbie

    Joined:
    Oct 6, 2011
    #1
    Need idea to convert the below php code to iOS

    Code:
    <?php
    function intersection($s1, $e1, $s2, $e2)
    {
            if ($e1 < $s2)
                    return 0;
            if ($s1 > $e2)
                    return 0;
            if ($s1 < $s2)
                    $s1 = $s2;
            if ($e1 > $e2)
                    $e1 = $e2;
            return $e1 - $s1;
    }
    
            $start = strtotime("18:30");
           // $end = strtotime("24:00");
             $end = strtotime("05:30") + 3600*24; // the work ended at 05:30 morning of the next day
    
            $morning_start = strtotime("07:00");
            $morning_end = strtotime("15:00");
    
            $afternoon_start = strtotime("15:00");
            $afternoon_end = strtotime("20:00");
    
            $night_start = strtotime("20:00");
            $night_end = strtotime("07:00") + 3600*24; // 07:00 of next day, add 3600*24 seconds
    
            echo "morning: " . intersection( $start, $end, $morning_start, $morning_end ) / 3600 . " hours\n";
           echo "afternoon: " . intersection( $start, $end, $afternoon_start, $afternoon_end ) / 3600 . " hours\n";
            echo "night: " . intersection( $start, $end, $night_start, $night_end ) / 3600 . " hours\n";
    ?>

    Run this php code u will understand it will explain you more
    Just a shift calculation.
    Any idea pls share with me...:(
     
  2. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
  3. Duncan C, Nov 25, 2013
    Last edited by a moderator: Nov 25, 2013

    Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    Cocoa touch has a great support for time and date calculations.

    Do a search in Xcode on "Calendrical Calculations". You want to create an NSCalendar object, and use it, along with NSDate and NSDateComponent objects to do whatever calculations you need.

    You might also need NSDateFormatter objects if you need to date strings to dates or visa versa.

    I don't know PHP very well and I'm tired so I did not get the sense of the code you posted. Plus I'm not going to do you work for you, but rather point you in the direction you need so you can solve your problem for yourself.)


     
  4. arungre, Nov 27, 2013
    Last edited: Nov 27, 2013

    arungre thread starter macrumors newbie

    Joined:
    Oct 6, 2011
    #4
    Thanks for the post Duncan! Just a small calculation module.

    Thanks for the post Duncan! Just a small calculation module.
    I just need an idea regular shift and extra time shift
    If person works for extra time shift then he will get bonus additionally.this is the concept
    So if the person fixing the extra time work from 10pm to 2am. then if he works from 8pm to 1am how to calculate regular time shift(2hrs) and extra time shift(3hrs). This is the calculation idea i need...



     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Your post is still really vague and incomplete.

    In your example, you listed one 4 hour shift and one 5 hour shift, and then say that should be counted as 2 regular hours and 3 overtime hours, which does not make sense.

    You need to define the problem in words first before you can implement it in code.

    From your second post I gather that you are talking about work shifts, and are trying to calculate how many hours are to be paid at normal time and how many hours should be paid at overtime.

    What are the rules that decide when hours count as overtime?

    This varies from country to country and even from employer to employer.

    Are overtime hours determined by the total number of hours worked in a shift? Total number of hours worked in a a week? Are all hours worked during certain times defined as overtime hours? (for example you might count all hours worked between 12:00

    State the rules for overtime clearly. Once you've done that we can help your figure out how to come up with code that implements those overtime rules.

    Also, how is your data provided? Do you get starting and ending times for each shift? What about breaks during the shift? Are they counted as work hours, or should they be subtracted from the total length of the shift? Will the data be in a file, or will the user use pickers to select that start and end times for each shift? Are you tracking multiple employees with a single program, or just one employee?
     
  6. arungre thread starter macrumors newbie

    Joined:
    Oct 6, 2011
    #6
    Reply

    Sorry for vague and incomplete of my above post
    Hope you understand
    This is for one employee
    Multiple data will be provided while calculation

    suppose i fix the extra time are as follows
    Regular 8am -6pm and obviously my night shift will be - 6pm to nextday8am

    if person works from 4pm and works till 2am next day.
    I just want to calculate the regular shift working hours and night shift working hours in xcode ObjectiveC

    can you help me on this....

    Regards
    Arun


     
  7. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    You can't describe the problem clearly after asking you repeatedly, so no, I can't. Trying to help somebody like you is maddening. I give up.


     

Share This Page