a question about time(NULL) function

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?

Thnx.
It is 12:00am UTC 01/01/1970 (ie, the first second of the year)

Are you sure your code is correct?
Time function is from midnight Jan 1 1970 (end of Jan Ist 1970);
Oh dear - :-)
Hmm, I looked at the UNIX man page for time(). Did you look somewhere else? Might it be different on different platforms?

EDIT: I thought it would be standard across platforms since this is the Win forum :)
But then we're talking msoft. They follow their own standards.
Last edited on
I always thought it was from the first second of the year 1970, but I thought I'd check.
It was from here http://rabbit.eng.miami.edu/info/functions/time.html

When I tried it, I was a day out as well - who know, maybe a windows thing or my calcs.
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)

Cheers, Danial.
....?!
Last edited on
this is my program :
It returns Day, one less than real:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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(long int &ss);
int getMonth( int month, int &remainingDays,int  year  );
int getHour(int &seconds);
int getMinute(int &seconds);

int main()

{
    long int 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(long int &ss)
{
    int year = 1970;
    while(1)
    {
        if( isThisALeapYear(year) && ss>= (60*60*24*366) )  //366 days
        {
            ss-=(60*60*24*366);
            year++;
        }
        else if( (!isThisALeapYear(year)) && ss>= (60*60*24*365))  //365 days
        {
            ss-=(60*60*24*365);
            year++;
        }
        else
            break;
    }
    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);
    }
    else
        return 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;
}


is anyone knowing reason ?
Last edited on
This looks like homework.
> This looks like homework.

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.

Hope this helps.
Last edited on
thanks Duoas!
.... that's three days since day one: the day number is 4

you are right. I used some loops for calculating day. but I didn't consider that point.
Thanks for hint. ( so usuful !)

Topic archived. No new replies allowed.