Quote:
Originally Posted by ArthurHuang
Uh...very cool..but how to write it in C codes?
|
Yes, it is cool. However, you have not even put in the effort to tell us in what language you want to implement that problem. So the time spent by ta0kira solving your problem was completely lost because you didn't want to spend the time to elaborate on your question. That doesn't sound fair, does it?
For that matter, while you could have forgotten to specify your programming language while concentrating on your question, you also withold is information about wat distro you use and what hardware.
Well, let me
assume that you use GNU Linux on a PC and not Berkely Unix on a Sparc, then the way to implement this is to use the
time() function, and pass that result (the number in seconds since 1970-01-01) to
localtime() function. It returns a structure of type
struct tm which contains fields indicating the number of the weekday.
Once you know the day number, you can calculate how many days you have to go back to the last Monday, multiply by 86400, substract this from your earlier obtained
time() value, feed this into
localtime() again. The time in seconds to the next Friday is calculated likewise.
A general advice, as soon as you start calculation on calendar data, go back to unix timestamp, do your calculations and convert back to human readable date. Only the
date function is an exception so you can escape to that sometimes, as shown. But even then it is often better to go back to unix timestamps.
Info on these functions:
http://www.cs.utah.edu/dept/old/texi...19.html#SEC317
jlinkels