hello everybody,
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?
Thanx for all replys. ...
as you said it starts counting seconds from 1st second of year 1970.
I checked my program several times. But i didn't find the problem. I think I must send it here! (Soon)
// CodeBlock
#include <iostream.h>
#include <time.h> //for time(NULL)
#include <conio.h> //for getch()
#include <windows.h>
//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);
int main()
{
longint seconds;
int lastTime;
do {
if ( time(NULL)-lastTime < 1 )
continue;
//clrscr();
system("cls");
lastTime = time(NULL);
int GMT =0// ( 3*60 + 30 ) * 60;
seconds = time(NULL)+GMT;// +24*60*60/*plus a day*/;
int year=getYears(seconds);
//getting years:
cout<<"Now : "<<year<<", ";
//getting Days:
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";
break;
case 2: cout<<"February";
break;
case 3: cout<<"March";
break;
case 4: cout<<"April";
break;
case 5: cout<<"May";
break;
case 6: cout<<"Jun";
break;
case 7: cout<<"July";
break;
case 8: cout<<"August";
break;
case 9: cout<<"September";
break;
case 10: cout<<"October";
break;
case 11: cout<<"November";
break;
case 12: cout<<"December";
break;
}
cout<<", "<<remainingDays<<endl;
//clock:
int hours, minutes;
hours = getHour(remaininfSeconds);
minutes = getMinute(remaininfSeconds);
cout<<"Time: "<<hours<<" : "<<minutes<<" : " << remaininfSeconds<<endl;
} while ( 1 );
getch();
return 0;
}
/* 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
return 1;
if( year % 100 == 0 ) //not a leap year
return 0;
if( year % 4 == 0 ) //a leap year
return 1;
return 0;
}
/*
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;
while(1)
{
if( isThisALeapYear(year) && ss>= (60*60*24*366) ) //366 days
{
ss-=(60*60*24*366);
year++;
}
elseif( (!isThisALeapYear(year)) && ss>= (60*60*24*365)) //365 days
{
ss-=(60*60*24*365);
year++;
}
elsebreak;
}
return year;
}
/*
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 )
{
int monthDays;
switch ( month )
{
case 1: monthDays = 31;
break;
case 2: (isThisALeapYear(year))? monthDays = 29:monthDays = 28 ; //february is 29 days in a leap year
break;
case 3: monthDays = 31;
break;
case 4: monthDays = 30;
break;
case 5: monthDays = 31;
break;
case 6: monthDays = 30;
break;
case 7: monthDays = 31;
break;
case 8: monthDays = 31;
break;
case 9: monthDays = 30;
break;
case 10: monthDays = 31;
break;
case 11: monthDays = 30;
break;
case 12: monthDays = 31;
break;
default: monthDays = 0;
}
if( remainingDays - monthDays> 0 )
{
remainingDays -= monthDays;
return getMonth( month+1,remainingDays, year);
}
elsereturn month;
}
/*
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 ) ;
seconds-=hours*3600;
return hours;
}
/*
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 ) ;
seconds-=minutes*60;
return minutes;
}
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.