Solved

c++ calculating seconds in a year

Posted on 2011-03-08
9
330 Views
Last Modified: 2012-05-11
how can i write a c++ program to determine  the number of seconds in any given year?

using namespace std;

int main(int argc, char *argv[])
{
    int year;
        cin >> year;
         cout << "leap year " << endl;
        }
        else
        {
        cout << " not leap year "  << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
0
Comment
Question by:Gator77
9 Comments
 
LVL 1

Expert Comment

by:gdemiro
ID: 35074968
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35075158
I would do it by using two DateTime objects, one set to the time at the beginning of the year in question and the other set to the beginning of the next year.  Convert both DateTimes to seconds and subtract the result to get the length of the year in seconds.

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 35075204
Use 'mktime()' (http://www.cplusplus.com/reference/clibrary/ctime/mktime/) for that, it returns a 'time_t' value for the date you pass in. 'time_t' now is nothing else but the amount of seconds elapsed since midnight (00:00:00), January 1, 1970 for any given date. Calculate that for Jan 1, Midnight and Dec 31 29:59 of that give year and subtract the results, then you have the amount of seconds in that year. Sorry for not being more specific, but EE has quite a strict homework policy.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 35080226
If your intention is to calculate leap year, I would recommend following logic rather than something based on number of seconds in a year

bool isLeapYear(int year)
{
	return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}

Open in new window



-Kusala
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35081589
obviously the number of seconds per year calculates by 366 * 24 * 3600 for leap years and 365 * 24 * 3600 else.

leap years we have for years dividable by 4 without remainder but not century years like 1900 beside the year is dividable by 400 without remainder like in 2000.

you can see the post of kusala for implementation of that rule.

Sara
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35081848
Don't forget the occasional Leap-Second:

http://en.wikipedia.org/wiki/Leap_second
0
 
LVL 10

Expert Comment

by:pjasnos
ID: 35090449
Leap seconds is definitely an overkill here - they can't be predicted algorithmically, so all you can do is include a big fat table of past values.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36518453
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best sources to up-to-date in C++? 8 90
Exception thrown at 0x00007FFD5BC81F28 7 49
Dialogbox API leak? 18 98
How do I get Window Title of all opened process? 4 11
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question