c-time function returns strange values












0















I'm working with a code in C, which gets the time from a server using the following:



(This is a very short version, other sections and functions of the code use time, ftime and localtime functions to get the time).



struct  tm *stiempo;
long ltiempo;
FILE * arch2;

int main()
{
print_error_time();
}


void print_error_time()
{
time (&ltiempo);
stiempo = localtime (&ltiempo);
fprintf (arch2, "%02.2ld/%02.2ld/%02.2ld, t %02.2ld:%02.2ld:%02.2ld", stiempo->tm_mday, (stiempo->tm_mon + 1), (stiempo->tm_year + 1900), stiempo->tm_hour, stiempo->tm_min, stiempo->tm_sec);
}


Sometimes it works just fine, but sometimes, when the time is printed into the file, i get something like this:



   21/08/2018,   15:48:7956003943165722682
21/08/2018, 15:50:7956003943165722667
21/08/2018, 15:51:7956003943165722649


Does anyone knows what could be causing this beheaviour or what could affect the time functions that makes them return those values?










share|improve this question























  • arch2 FILE pointer is not pointing to any FILE object.

    – H.S.
    Jan 2 at 19:40








  • 1





    You are using wrong format specifier in fprintf(), which is undefined behavior.

    – H.S.
    Jan 2 at 19:42











  • Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

    – Jonathan Leffler
    Jan 2 at 19:48













  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:28













  • @Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

    – Jonathan Leffler
    Jan 4 at 8:41
















0















I'm working with a code in C, which gets the time from a server using the following:



(This is a very short version, other sections and functions of the code use time, ftime and localtime functions to get the time).



struct  tm *stiempo;
long ltiempo;
FILE * arch2;

int main()
{
print_error_time();
}


void print_error_time()
{
time (&ltiempo);
stiempo = localtime (&ltiempo);
fprintf (arch2, "%02.2ld/%02.2ld/%02.2ld, t %02.2ld:%02.2ld:%02.2ld", stiempo->tm_mday, (stiempo->tm_mon + 1), (stiempo->tm_year + 1900), stiempo->tm_hour, stiempo->tm_min, stiempo->tm_sec);
}


Sometimes it works just fine, but sometimes, when the time is printed into the file, i get something like this:



   21/08/2018,   15:48:7956003943165722682
21/08/2018, 15:50:7956003943165722667
21/08/2018, 15:51:7956003943165722649


Does anyone knows what could be causing this beheaviour or what could affect the time functions that makes them return those values?










share|improve this question























  • arch2 FILE pointer is not pointing to any FILE object.

    – H.S.
    Jan 2 at 19:40








  • 1





    You are using wrong format specifier in fprintf(), which is undefined behavior.

    – H.S.
    Jan 2 at 19:42











  • Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

    – Jonathan Leffler
    Jan 2 at 19:48













  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:28













  • @Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

    – Jonathan Leffler
    Jan 4 at 8:41














0












0








0








I'm working with a code in C, which gets the time from a server using the following:



(This is a very short version, other sections and functions of the code use time, ftime and localtime functions to get the time).



struct  tm *stiempo;
long ltiempo;
FILE * arch2;

int main()
{
print_error_time();
}


void print_error_time()
{
time (&ltiempo);
stiempo = localtime (&ltiempo);
fprintf (arch2, "%02.2ld/%02.2ld/%02.2ld, t %02.2ld:%02.2ld:%02.2ld", stiempo->tm_mday, (stiempo->tm_mon + 1), (stiempo->tm_year + 1900), stiempo->tm_hour, stiempo->tm_min, stiempo->tm_sec);
}


Sometimes it works just fine, but sometimes, when the time is printed into the file, i get something like this:



   21/08/2018,   15:48:7956003943165722682
21/08/2018, 15:50:7956003943165722667
21/08/2018, 15:51:7956003943165722649


Does anyone knows what could be causing this beheaviour or what could affect the time functions that makes them return those values?










share|improve this question














I'm working with a code in C, which gets the time from a server using the following:



(This is a very short version, other sections and functions of the code use time, ftime and localtime functions to get the time).



struct  tm *stiempo;
long ltiempo;
FILE * arch2;

int main()
{
print_error_time();
}


void print_error_time()
{
time (&ltiempo);
stiempo = localtime (&ltiempo);
fprintf (arch2, "%02.2ld/%02.2ld/%02.2ld, t %02.2ld:%02.2ld:%02.2ld", stiempo->tm_mday, (stiempo->tm_mon + 1), (stiempo->tm_year + 1900), stiempo->tm_hour, stiempo->tm_min, stiempo->tm_sec);
}


Sometimes it works just fine, but sometimes, when the time is printed into the file, i get something like this:



   21/08/2018,   15:48:7956003943165722682
21/08/2018, 15:50:7956003943165722667
21/08/2018, 15:51:7956003943165722649


Does anyone knows what could be causing this beheaviour or what could affect the time functions that makes them return those values?







c time localtime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 19:31









FaustoFausto

31




31













  • arch2 FILE pointer is not pointing to any FILE object.

    – H.S.
    Jan 2 at 19:40








  • 1





    You are using wrong format specifier in fprintf(), which is undefined behavior.

    – H.S.
    Jan 2 at 19:42











  • Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

    – Jonathan Leffler
    Jan 2 at 19:48













  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:28













  • @Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

    – Jonathan Leffler
    Jan 4 at 8:41



















  • arch2 FILE pointer is not pointing to any FILE object.

    – H.S.
    Jan 2 at 19:40








  • 1





    You are using wrong format specifier in fprintf(), which is undefined behavior.

    – H.S.
    Jan 2 at 19:42











  • Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

    – Jonathan Leffler
    Jan 2 at 19:48













  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:28













  • @Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

    – Jonathan Leffler
    Jan 4 at 8:41

















arch2 FILE pointer is not pointing to any FILE object.

– H.S.
Jan 2 at 19:40







arch2 FILE pointer is not pointing to any FILE object.

– H.S.
Jan 2 at 19:40






1




1





You are using wrong format specifier in fprintf(), which is undefined behavior.

– H.S.
Jan 2 at 19:42





You are using wrong format specifier in fprintf(), which is undefined behavior.

– H.S.
Jan 2 at 19:42













Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

– Jonathan Leffler
Jan 2 at 19:48







Most of the fields in the struct tm are not long but int and printing them with %ld on a 64-bit machine leads to unpredictable behaviour. You could use %.2d or %02d to get your desired result (probably), though maybe you should use 4 instead of 2 for the year (though it does no damage with 2). On a 32-bit machine, you might get away with as sizeof(int) == sizeof(long). Also, Win64 might be OK for the sam reason.

– Jonathan Leffler
Jan 2 at 19:48















Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

– Fausto
Jan 2 at 22:28







Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

– Fausto
Jan 2 at 22:28















@Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

– Jonathan Leffler
Jan 4 at 8:41





@Fausto: the trouble with undefined behaviour is that the code is allowed to work as expected and to work other than as expected, and both are correct because the official requirement is undefined. One way that you can see it working on Machine A and failing on Machine B is if Machine A is 32-bit and Machine B is 64-bit. You could also run into issues if the working machine is Windows 64-bit and the non-working machine is not. And you could run into issues if the CPU on one machine is little-endian and on the other big-endian. You've not identified how similar your two machines are.

– Jonathan Leffler
Jan 4 at 8:41












1 Answer
1






active

oldest

votes


















3














From localtime:



Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};


All the tm structure member are of type int and you are using format specifier %ld for them in fprintf(). Instead, you should use %d format specifier.



From C Standard#7.21.6.1p9




9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.







share|improve this answer
























  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:50











  • @Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

    – H.S.
    Jan 3 at 1:24












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012133%2fc-time-function-returns-strange-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














From localtime:



Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};


All the tm structure member are of type int and you are using format specifier %ld for them in fprintf(). Instead, you should use %d format specifier.



From C Standard#7.21.6.1p9




9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.







share|improve this answer
























  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:50











  • @Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

    – H.S.
    Jan 3 at 1:24
















3














From localtime:



Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};


All the tm structure member are of type int and you are using format specifier %ld for them in fprintf(). Instead, you should use %d format specifier.



From C Standard#7.21.6.1p9




9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.







share|improve this answer
























  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:50











  • @Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

    – H.S.
    Jan 3 at 1:24














3












3








3







From localtime:



Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};


All the tm structure member are of type int and you are using format specifier %ld for them in fprintf(). Instead, you should use %d format specifier.



From C Standard#7.21.6.1p9




9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.







share|improve this answer













From localtime:



Broken-down time is stored in the structure tm which is defined in <time.h> as follows:

struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};


All the tm structure member are of type int and you are using format specifier %ld for them in fprintf(). Instead, you should use %d format specifier.



From C Standard#7.21.6.1p9




9 If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.








share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 19:49









H.S.H.S.

5,6291420




5,6291420













  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:50











  • @Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

    – H.S.
    Jan 3 at 1:24



















  • Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

    – Fausto
    Jan 2 at 22:50











  • @Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

    – H.S.
    Jan 3 at 1:24

















Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

– Fausto
Jan 2 at 22:50





Could that be the only reason? I'm trying to replicate that error in another machine but i can't. I'm running the same code and it prints the time the way it should be, even printing with %ld

– Fausto
Jan 2 at 22:50













@Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

– H.S.
Jan 3 at 1:24





@Fausto An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. On my machine, with %ld, I was getting - 03/01/2019, 01:4294967309:37 result.

– H.S.
Jan 3 at 1:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012133%2fc-time-function-returns-strange-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]