I wrote a program for calculating current date and time using function time(NULL).
I read this function contains total seconds from 1970.01.01 till now.
but in the end I found 1 day less than today's date.
So, does it calculate second from time 00:00:00 in the beginning of year 1970 or from end of January 1st?
#include <time.h> //for time(NULL)
#include <conio.h> //for getch()
//using namespace std;
//just some prototypes:
bool isThisALeapYear(int year);
int getYears(longint &ss);
int getMonth( int month, int &remainingDays,int year );
int getHour(int &seconds);
int getMinute(int &seconds);
if ( time(NULL)-lastTime < 1 )
lastTime = time(NULL);
int GMT =0// ( 3*60 + 30 ) * 60;
seconds = time(NULL)+GMT;// +24*60*60/*plus a day*/;
cout<<"Now : "<<year<<", ";
int remainingDays = int( seconds / (60*60*24) );
int remaininfSeconds = seconds - 60*60*24*int( seconds / (60*60*24) ) ;// = int( seconds / (60*60*24) ) *60*60*24;
switch ( getMonth(1, remainingDays, year ) )
case 1: cout<<"January";
case 2: cout<<"February";
case 3: cout<<"March";
case 4: cout<<"April";
case 5: cout<<"May";
case 6: cout<<"Jun";
case 7: cout<<"July";
case 8: cout<<"August";
case 9: cout<<"September";
case 10: cout<<"October";
case 11: cout<<"November";
case 12: cout<<"December";
int hours, minutes;
hours = getHour(remaininfSeconds);
minutes = getMinute(remaininfSeconds);
cout<<"Time: "<<hours<<" : "<<minutes<<" : " << remaininfSeconds<<endl;
} while ( 1 );
/* This is a function that returns whether a desired year is leap or not.
if year mod 4 =0 then this is a leap year
except year mod 100 = 0
but year mod 400 = 0 is also a leap year
\param year: is the esired year
\return : whether year is a leap year or not
bool isThisALeapYear(int year)
if( year % 400 == 0 ) //a leap year
if( year % 100 == 0 ) //not a leap year
if( year % 4 == 0 ) //a leap year
This is a function that returns current year
\param ss: total second from start till now
\returns the curent year
int getYears(longint &ss)
int year = 1970;
if( isThisALeapYear(year) && ss>= (60*60*24*366) ) //366 days
elseif( (!isThisALeapYear(year)) && ss>= (60*60*24*365)) //365 days
This is a year that calculates current month based on a recursive form
\param month: is next month to calculate by remaining days
\param remainingDays: is the day that remains beside calculated years.(Its called by refrence fordecreasing for every month.
\pram year: is current year. It is used for calculating Februari's days it is 28 days or 29 days(for leap year)
\eturns current month
int getMonth( int month, int &remainingDays,int year )
switch ( month )
case 1: monthDays = 31;
case 2: (isThisALeapYear(year))? monthDays = 29:monthDays = 28 ; //february is 29 days in a leap year
case 3: monthDays = 31;
case 4: monthDays = 30;
case 5: monthDays = 31;
case 6: monthDays = 30;
case 7: monthDays = 31;
case 8: monthDays = 31;
case 9: monthDays = 30;
case 10: monthDays = 31;
case 11: monthDays = 30;
case 12: monthDays = 31;
default: monthDays = 0;
if( remainingDays - monthDays> 0 )
remainingDays -= monthDays;
return getMonth( month+1,remainingDays, year);
a function for calculating current HOURS
\param seconds: is seconds remaining in today.
\return: hours in today.
int getHour(int &seconds)
int hours = int( seconds / 3600 ) ;
a function for calculating current MINUTES
\param seconds: is seconds remaining after last hour .
\return: minutes after last hour in today.
int getMinute(int &seconds)
int minutes = int( seconds / 60 ) ;
So? He only asked to help figure out what he did wrong.
The trick is to remember that it counts seconds from 1970 Jan 01 00:00:00Z. What is easy to forget in this question is the difference between 0 and 1.
Let's say a day is 100 units. Given 348 units, that's three days since day one: the day number is 4. The same error occurs for all the other times, but you obviate it by counting (with loops) over a zero-based index. But you never adjusted the day from zero-based to one-based.
Here are some hints for improvement:
1. The time() function always reports 86400 seconds per day. Using this information, you can write a simple equation to return the number of complete days since 1970 Jan 01.
(The underlying UNIX time implementation is kludged to report leap seconds as the previous second... so you don't need to worry about days that actually have 86401 seconds.)
2. There are roughly 365.24 days per year. Use this to calculate the current year.
3. The exact number of days between the current year and 1970 can be calculated with another simple equation. Remember the following:
- 1972 was a leap year.
- every fourth year is a leap year.
- a normal year has 365 days.
- a leap year has 365 + 1 days.
To help get your head around this one, first calculate the number of days from 1970 to the current year using 365 days per year. Then ask yourself, how many more days are added by the leap years?
4. Use an array of const char * to lookup the month name instead of the switch() statement.