Created
February 13, 2013 02:11
-
-
Save anonymous/4875303 to your computer and use it in GitHub Desktop.
Why does adding and subtracting integers and longs get weird?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// ch10 | |
// | |
// Created by john salyer on 2/12/13. | |
// Copyright (c) 2013 john salyer. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <time.h> | |
int main(int argc, const char * argv[]) | |
{ | |
//what is now? | |
long secondsSince1970 = time(NULL); | |
struct tm now = *localtime(&secondsSince1970); //the number of seconds since jan 1 1970 | |
//localtime_r(&secondsSince1970, &now); | |
printf("The date now is %d-%d-%d\n", now.tm_mon+1, now.tm_mday, now.tm_year+1900); | |
printf("days since Jan 1: %d \n",now.tm_yday+1); | |
//what is 4M seconds from now? | |
long fourmillion = 4000000; | |
long plusfourmillion = secondsSince1970 + fourmillion; | |
long then = time(&plusfourmillion); | |
struct tm future;// = *localtime(&secondsSince1970); | |
localtime_r(&then, &future); | |
printf("The date in 4 million seconds will be %d-%d-%d\n", future.tm_mon+1, future.tm_mday, future.tm_year+1900); | |
//it should be something like 46.296296296 days!!! | |
printf("days since Jan 1: %d \n",future.tm_yday+1); | |
//why is it 47? | |
printf("What's %d - %d = %d\n",future.tm_yday+1, now.tm_yday+1, future.tm_yday - now.tm_yday ); | |
//shouldn't be the number of seconds that we're calculating? | |
printf("Tell us the difference between NOW %li and THEN %li equals %li", secondsSince1970, then, secondsSince1970 - then); | |
// -9 !?!?! WTF!? | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment