Page 1 of 1

Neat App

Posted: Sun Mar 09, 2008 12:01 am
by jzgriffin
I was thinking about time, earlier, and wondered how many seconds had passed since 0001-01-01 00:00:00...and, well, you can imagine what I came up with.

Code: Select all

/* by [email protected] */
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#define Sleep usleep
#endif

#define CENTURY 20
#define YEAR 8
#define MSG "Seconds since 0001-01-01 00:00:00"

int months[] = {
        31,
        31 + 28,
        31 + 28 + 31,
        31 + 28 + 31 + 30,
        31 + 28 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30,
        31 + 28 + 31 + 30 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
        31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
}, months2[12];

unsigned long long int Calculate(int print)
{
        time_t rawtime;
        struct tm *timeinfo;

        time(&rawtime);
        timeinfo = localtime(&rawtime);

        int month = timeinfo->tm_mon;
        int day = day = timeinfo->tm_mday, hour = timeinfo->tm_hour;
        int minute = timeinfo->tm_min, second = timeinfo->tm_sec;
        int yyyy = (CENTURY * 100) + YEAR;
        int leap = ((yyyy % 4 == 0) && (yyyy % 100 != 0) || (yyyy % 400 == 0));
        if(leap) {
                int idx;
                for(idx = 0; idx < sizeof(months) / sizeof(months[0]); idx++) {
                        months2[idx] = months[idx] + 1;
                }
        }
        unsigned long long int result = ((yyyy * (leap ? 366 : 365)) +
                months2[month]) + day;
        result = (result * 24) + hour;
        result = (result * 60) + minute;
        result = (result * 60) + second;

        if(print) {
                printf("Century: %d\nYear: %d\nMonth: %d\nDay: %d\n", CENTURY, YEAR,
                        month, day);
                printf("Hour: %d\nMinute: %d\nSecond: %d\n", hour, minute, second);
                printf("Year (centurized): %d\n", yyyy);
                printf("Is leap year? %d\n", leap);
        }

        return result;
}

int main()
{
        int print = 1;
        for(; ; ) {
                unsigned long long int value = Calculate(print);
                print = 0;
                printf("\r");
#ifdef MINGW
                printf("%s: %I64u", MSG, value);
#else
                printf("%s: %lld", MSG, value);
#endif
                fflush(stdin);
                Sleep(1);
        }

        return 0;
}
It seems to calculate it fairly accurately...so, woot. :-)

Took me a few minutes to figure out why MinGW was failing miserably with %lld.

This is somewhat related: http://theoryuniverse.com/theory/?p=4

Posted: Sun Mar 09, 2008 5:26 am
by eddyb
63506467560 :P

Posted: Sun Mar 09, 2008 10:14 am
by Zacariaz
what about leap year? (maybe allso summer/winter time)

Posted: Sun Mar 09, 2008 10:19 am
by jzgriffin

Code: Select all

        int leap = ((yyyy % 4 == 0) && (yyyy % 100 != 0) || (yyyy % 400 == 0));
        if(leap) {
                int idx;
                for(idx = 0; idx < sizeof(months) / sizeof(months[0]); idx++) {
                        months2[idx] = months[idx] + 1;
                }
        }
        unsigned long long int result = ((yyyy * (leap ? 366 : 365)) +
                months2[month]) + day; 
That code takes account of the current leap year. I hadn't remembered doing it for past leap years as well. Thanks. :-P

Edit: New version:

Code: Select all

/**
 * By Jeremiah Griffin <[email protected]>
 */

#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#define Sleep usleep
#endif

#define CENTURY 20
#define YEAR 8
#define MSG "Seconds since 0001-01-01 00:00:00"
#define LEAP(y) (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))

int months[] = {
	31,
	31 + 28,
	31 + 28 + 31,
	31 + 28 + 31 + 30,
	31 + 28 + 31 + 30 + 31,
	31 + 28 + 31 + 30 + 31 + 30,
	31 + 28 + 31 + 30 + 31 + 30 + 31,
	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
	31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
};

unsigned long long int Calculate(int print)
{
	time_t rawtime;
	struct tm *timeinfo;

	time(&rawtime);
	timeinfo = localtime(&rawtime);

	int month = timeinfo->tm_mon;
	int day = day = timeinfo->tm_mday, hour = timeinfo->tm_hour;
	int minute = timeinfo->tm_min, second = timeinfo->tm_sec;
	int yyyy = (CENTURY * 100) + YEAR;
	int leap = LEAP(yyyy);
	int idx, pastLeaps = 0;
	for(idx = 1; idx <= yyyy; idx++) if(LEAP(idx)) pastLeaps++;
	unsigned long long int result;
	result = (365 * yyyy) + day + pastLeaps + months[month];
	result = (result * 24) + hour;
	result = (result * 60) + minute;
	result = (result * 60) + second;

	if(print) {
		printf("Century: %d\nYear: %d\nMonth: %d\nDay: %d\n", CENTURY,
			YEAR, month, day);
		printf("Hour: %d\nMinute: %d\nSecond: %d\n", hour, minute,
			second);
		printf("Year (centurized): %d\n", yyyy);
		printf("Is leap year? %d\n", leap);
		printf("Past leap years: %d\n", pastLeaps);
	}

	return result;
}

int main()
{
	int print = 1;
	for(; ; ) {
		unsigned long long int value = Calculate(print);
		print = 0;
		printf("\r");
#ifdef MINGW
		printf("%s: %I64u", MSG, value);
#else
		printf("%s: %lld", MSG, value);
#endif
		fflush(stdin);
		Sleep(995);
	}

	return 0;
}

Posted: Sun Mar 09, 2008 1:04 pm
by Zacariaz
and you remembered the yyyy % 100 too, nice work.

the only other thing i can really come up with is the summer/winter time thing, but that is very tricky.

Posted: Sun Mar 09, 2008 4:53 pm
by jzgriffin
Tricky, hmm. I'll have to try it, then. :-)

*goes off and reads about daylight savings time*

Posted: Sun Mar 09, 2008 5:44 pm
by Zacariaz
One of the main problems, as far as i know, is that the concept isn't used all over the globe, thus if it should be taken into account relyes on where you are on the globe and furthermore if you are to take it into account again it relyes on the time of years and so on and so forth...

Don't really think it's an issue, just thought i would mention it.

Posted: Sun Mar 09, 2008 5:47 pm
by jzgriffin
Yeah, after reading about it, I decided against it...it's still a pretty neat program, though, in my opinion. :-D