Neat App

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Neat App

Post 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
eddyb

Post by eddyb »

63506467560 :P
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

what about leap year? (maybe allso summer/winter time)
This was supposed to be a cool signature...
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post 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;
}
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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.
This was supposed to be a cool signature...
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post by jzgriffin »

Tricky, hmm. I'll have to try it, then. :-)

*goes off and reads about daylight savings time*
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post 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.
This was supposed to be a cool signature...
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post by jzgriffin »

Yeah, after reading about it, I decided against it...it's still a pretty neat program, though, in my opinion. :-D
Post Reply