Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:10:01+00:00 2026-06-17T10:10:01+00:00

I have written this code down, my object of the program is to calculate

  • 0

I have written this code down, my object of the program is to calculate the minutes between two given dates and time.
lets say the difference in minute between:

14/1/2016 23:18
and
14/1/2004 23:18
is:
6,311,520.00 minutes 

this is the code i have wrote:

i have some bug in the calculation, from what i found my problem is at most 1440 minute difference from the correct answer – checked by excel.
i think my problem is in the calculation of the LEAP DAYS between the two dates:

    #include <stdio.h>

    typedef struct {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;     
    }time;
    time time1,time2;

long calcTime(time,time);
int calcDaysFromStart(int,int);
int leapcheck(int);

int main()
{

    printf("Hello\n");
    printf("For calculating the difference between two times:\n");
    printf("Enter the date for first time:\n");
    printf("Enter day:\n");
    scanf("%d",&time1.day);
    printf("Enter month:\n");
    scanf("%d",&time1.month);
    printf("Enter year:\n");
    scanf("%d",&time1.year);
    printf("Enter the exact hour for first time:\n");
    printf("Enter the hour:\n");
    scanf("%d",&time1.hour);
    printf("Enter the minutes:\n");
    scanf("%d",&time1.minute);
    printf("Enter the seconds:\n");
    scanf("%d",&time1.second);
    printf("-----------------------------------\n");
    printf("Enter the date for second time:\n");
    printf("Enter day:\n");
    scanf("%d",&time2.day);
    printf("Enter month:\n");
    scanf("%d",&time2.month);
    printf("Enter year:\n");
    scanf("%d",&time2.year);
    printf("Enter the exact hour for first time:\n");
    printf("Enter the hour:\n");
    scanf("%d",&time2.hour);
    printf("Enter the minutes:\n");
    scanf("%d",&time2.minute);
    printf("Enter the seconds:\n");
    scanf("%d",&time2.second);
    printf("-----------------------------------\n");
    printf("-----------------------------------\n");
    printf("The first time is: %d:%d:%d %d/%d/%d\n", time1.hour ,time1.minute ,time1.second ,time1.day, time1.month ,time1.year);
    printf("The second time is: %d:%d:%d %d/%d/%d\n", time2.hour ,time2.minute ,time2.second ,time2.day, time2.month ,time2.year);
    printf("The Difference between the two times in minutes is:%ld\n", calcTime(time1,time2));
    return 1;   
}


long calcTime(time time1,time time2)
{
    long t1,t2,totalDiff;
    long yearDiffeInMinutes = 0;
    int leapt1, leapt2,leapAdd;
    leapt1 = leapcheck(time1.year);
    leapt2 = leapcheck(time2.year);
    int daysFromStartt1, daysFromStartt2;
    daysFromStartt1 = calcDaysFromStart(time1.month,leapt1);
    daysFromStartt2 = calcDaysFromStart(time2.month,leapt2);
    t1 = time1.minute+time1.hour*60+time1.day*1440+daysFromStartt1*1440;
    t2 = time2.minute+time2.hour*60+time2.day*1440+daysFromStartt2*1440;

    if (time1.year>time2.year)
    {
        leapAdd = (time1.year-time2.year)/4;
        if ((leapt1==1) && (time1.month<3))
            leapAdd--;
        if((leapt2==1) && (time2.month>2))
            leapAdd--;
        printf("THE PARAM leapApp IS:%d\n",leapAdd);
        yearDiffeInMinutes = ((time1.year-time2.year)*525600+leapAdd*1440);
        totalDiff = yearDiffeInMinutes+(t1-t2);
        printf("The first time is bigger\n");
        return totalDiff;
    }
    else if(time2.year>time1.year)
    {
        leapAdd = (time2.year-time1.year)/4;
        if ((leapt2==1) && (time2.month<3))
            leapAdd--;
        if((leapt1==1) && (time1.month>2))
            leapAdd--;
        yearDiffeInMinutes = ((time2.year-time1.year)*525600+leapAdd*1440);
        totalDiff = yearDiffeInMinutes+(t2-t1);
        printf("The second time is bigger\n");
        return totalDiff; 
    }
    else if(t1>t2)/**both times are in the same year**/
    {
        printf("The first time is bigger\n");
        if ((leapt1==1) && (time1.month>2))
            if(time2.month<2)
                return (t1-t2+1440);
        return(t1-t2);
    }
    else if(t2>t1)
    {
        printf("The second time is bigger\n");
        if ((leapt2==1) && (time2.month>2))
            if(time1.month<2)
                return (t2-t1+1440);
        return (t2-t1);
    }
    else
    {
        printf("Both times are equals\n");
        return 0;
    }
}


/**check if the year is leap, return 0 if not a leap and 1 if a leap**/
int leapcheck(int year)
{
    if(year%400==0 || (year%100!=0 && year%4==0))
    {
        printf("THE YEAR %d IS LEAP\n",year);
        return 1;
    }
    printf("THE YEAR %d is NOT LEAP\n",year);
    return 0;
}



/**clalculate how many days past from start ofthe year**/
int calcDaysFromStart(int month, int leap)
{
    if (month==1)
        return 0;
    else if (month==2)
        return 31;
    else if (month==3)
        return (59+leap);
    else if (month==4)
        return (90+leap);
    else if (month==5)
        return (120+leap);
    else if (month==6)
        return (151+leap);
    else if (month==7)
        return (181+leap);
    else if (month==8)
        return (212+leap);
    else if (month==9)
        return (243+leap);
    else if (month==10)
        return (273+leap);
    else if (month==11)
        return (304+leap);
    else if (month==12)
        return (334+leap);
    else return -1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:10:02+00:00Added an answer on June 17, 2026 at 10:10 am

    The quick fix for your example would be to add the following line in both if and else:

    if(leapt1==1 && leapt2==1)
        leapAdd++;
    

    But this code will fail for bigger intervals (which include 100 and 400 years leap exceptions). So, I suggest you to rewrite this part of code; for example you can iterate through each of the year in the interval and check if it leap or common, with your special casing for start and end of interval).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written this code what it does is if user types postcode or
I have written this code outside all functions: int l, k; for (l =
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this code in javascript however I have to make it work
In my code I have written this but it fails to compile: In Class1.h:
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.