How to find out the number of seconds from 1700 until the specified date and time

Hello!

How to find out the number of seconds from 1700 until the specified date and time?

Now I'm trying to find quantity seconds between 00h 00m 00s 01d 01m 1970y and 16h 00m 26s 11d 02m 13th. But it turns zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    // time: 16 00 26
    // date: 11 02 13
    struct tm time2013;
    time2013.tm_hour = 16;
    time2013.tm_min = 00;
    time2013.tm_sec = 26;
    time2013.tm_mday = 11;
    time2013.tm_mon = 2-1;
    time2013.tm_year = 113;

    // time: 00 00 00
    // date: 01 01 1970
    struct tm time1970;
    time1970.tm_hour = 00;
    time1970.tm_min = 00;
    time1970.tm_sec = 00;
    time1970.tm_mday = 01;
    time1970.tm_mon = 1-1;
    time1970.tm_year = 70;
    
    double seconds = difftime(mktime(&time2013), mktime(&time1970));

well lets do the math.
1 minute = 60 seconds.
60 minutes = 1 hr = 60 * 60 = 3600 Seconds in 1 hour.
24 hr = 1 day = 3600 * 24 = 86400 seconds in 1 day. There are
365.25 Days in a year = 86400 * 365.25 = 31557600 Seconds in a year.
so from 2013 - 1970 that is 43 yrs which is 31557600 * 43 = 1356976800 Seconds. A unsigned long long has a max value of: 18446744073709551615
so the maximum difference would be 18446744073709551615 / 31557600
= 584542046090.62639791999391588714 years. So unless you are trying to find the number of seconds that passed form now until the creation of everything you may need to use long doubles or even possibly large =p

To get your result you will have to work backwards. Or figure the amount of seconds from each one then subtract them from each other.
Last edited on
> But it turns zero.

No, it doesn't. http://ideone.com/cEiItv
OH those are functions it threw me off with the struct..that might be his problem JL you used a std::tm and he used a struct.
> you used a std::tm and he used a struct.

It is the same thing.
He would have either plugged in using namespace std ; somewhere or used #include <time.h>
Oh I see http://www.cplusplus.com/reference/ctime/tm/ sorry I haven't used that before. Have only used one of the time function so far (for getting a random seed.) I'm still fairly new (4-5months.)
I don't understand the problem.

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
/* 
 * File:   main.cpp
 * Author: Ivan
 *
 * Created on May 23, 2013, 3:24 PM
 */

#include <iostream>
/* mktime example: weekday calculator */
#include <ctime>       /* time_t, struct tm, time, mktime */

int main(int argc, char** argv) {
    // time: 16 00 26
    // date: 11 02 13
    std::tm time2013;
    time2013.tm_hour = 16;
    time2013.tm_min = 00;
    time2013.tm_sec = 26;
    time2013.tm_mday = 11;
    time2013.tm_mon = 2-1;
    time2013.tm_year = 113;
 
    // time: 00 00 00
    // date: 01 01 1970
    std::tm time1970;
    time1970.tm_hour = 00;
    time1970.tm_min = 00;
    time1970.tm_sec = 00;
    time1970.tm_mday = 01;
    time1970.tm_mon = 1-1;
    time1970.tm_year = 70;
 
    std::cout << std::fixed 
        << std::difftime( std::mktime(&time2013), std::mktime(&time1970) ) << '\n' ;

    return 0;
}



0.000000
Press [Enter] to close the terminal ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
/* mktime example: weekday calculator */
#include <ctime>       /* time_t, struct tm, time, mktime */

int main() 
{
    // time: 16 00 26
    // date: 11 02 13
    std::tm time2013;
    time2013.tm_hour = 16;
    time2013.tm_min = 00;
    time2013.tm_sec = 26 ;
    time2013.tm_mday = 11;
    time2013.tm_mon = 2-1;
    time2013.tm_year = 113;
 
    // The Microsoft library internally uses Universal Coordinated Time (UTC)
    // Your time zone setting may be such that midnight 1 Jan 1970 local time 
    // is a timepoint before midnight 1 Jan 1970 UTC. 
    // std::time_t of zero specifies start of epoh (midnight 1 Jan 1970 UTC) 
    std::cout << std::fixed << std::difftime( std::mktime(&time2013), 0 )  << '\n' ;
}

Thanks! It is work.

Now the main question: "How to find out the number of seconds from 1700 until the specified date and time?"

I do not know how to do it.
> How to find out the number of seconds from 1700 until the specified date and time?

Either specify the date and time in UTC (instead of local time).

Or specify it in localtime, and then adjust it for the timezone offset.
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
#include <iostream>
/* mktime example: weekday calculator */
#include <ctime>       /* time_t, struct tm, time, mktime */

int main() 
{
    // time: 16 00 26
    // date: 11 02 13
    // local time
    std::tm time2013;
    time2013.tm_hour = 16;
    time2013.tm_min = 00;
    time2013.tm_sec = 26 ;
    time2013.tm_mday = 11;
    time2013.tm_mon = 2-1;
    time2013.tm_year = 113;


    // Microsoft specific:
    // Add difference in seconds between local time and UTC
    time2013.tm_sec += _timezone ; 

    std::cout << "seconds since midnight 01 Jan 1970 UTC: " 
              << std::mktime(&time2013) << '\n' ;

    std::cout << "seconds since midnight 01 Jan 1970 local time: " 
              << std::mktime(&time2013) - _timezone << '\n' ;
}
Thanks!

But I still wonder, suddenly I need time between the date to 1907 (or 1812) and the current date.

Why the function returns -1?

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
/* 
 * File:   main.cpp
 * Author: Ivan
 *
 * Created on May 23, 2013, 3:24 PM
 */

#include <iostream>
#include <ctime>

int main(int argc, char** argv) {
    // time: 00 00 00
    // date: 01 01 1970
    std::tm time1970;
    time1970.tm_hour = 00;
    time1970.tm_min = 00;
    time1970.tm_sec = 00;
    time1970.tm_mday = 01;
    time1970.tm_mon = 1 - 1;
    time1970.tm_year = 0;

    std::cout << std::mktime(&time1970) << std::endl;

    return 0;
}


Output

-1
Press [Enter] to close the terminal ...
Last edited on
> Why the function returns -1?

The library can't handle time points before the start of the epoch.

Run this program on your machine to see what is the start of epoch.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <ctime>
 
int main()
{
    std::time_t the_beginning_of_time {} ;
    std::cout << "earliest timepoint that can be handled by the library is:\n"
               << std::ctime( &the_beginning_of_time ) ;
}

http://ideone.com/7qFrnd
Thanks!


earliest timepoint that can be handled by the library is:
Thu Jan 01 04:00:00 1970
Press [Enter] to close the terminal ...
With GNU date:
# date -d 1700-01-01 +%s
-8520341992
# date -d now +%s
1369399045
# echo $[1369399045-(-8520341992)]
9889741037
keskiverto, thank you!


With GNU date:
# date -d 1700-01-01 +%s
-8520341992
# date -d now +%s
1369399045
# echo $[1369399045-(-8520341992)]
9889741037


How to do this in C++?
Topic archived. No new replies allowed.