subtracting days from localtime problem
ALeine
aleine at austrosearch.net
Fri Apr 1 00:15:51 PST 2005
ganbold at micom.mng.net wrote:
> One more question, what will happen next time when
> time goes back in October?
> Does following line correct it as same as now?
>
> /* make mktime(3) figure out whether DST is in effect */
> t->tm_isdst = -1;
Yes, it will work correctly. You can see what happens by
setting now to 1131131131 (where you currently have
now = time(NULL)). It's not an April Fools' Day joke, that
just happens to be an appropriate time in November. :-)
I also noticed a number of bugs in your code that you may
want to fix:
- memory leak: you are allocating memory with my_alloc(),
but then you neither free(3) it nor keep track of it.
You should remove my_alloc() and just create a buffer
in main() and then pass it by reference to functions that
need to modify its contents.
- with my_alloc() you basically reinvented strdup(3), see
man 3 strdup for details. You do not need strdup(3) for
what you want to achieve, just pass the buffer from main()
by reference and then use snprintf(3) to modify its contents.
- snprintf(3) handles the size argument correctly, meaning
that you should specify the full size of the string since
at most size-1 characters are written with guaranteed NULL
termination, see man 3 snprintf for details.
ALeine
___________________________________________________________________
WebMail FREE http://mail.austrosearch.net
More information about the freebsd-hackers
mailing list