On 2010-10-08 at 08:07 -0700, Andrew Hume wrote: > now i would have just said > hour = t/(3600 * 1000); // remember t is in milliseconds > // if i wanted hr in 00..23 range: hour = hour%24; > > bill said > xx = t/1000 > strncpy(hr_str, &(ctime(&xx)[11]), 2); > hr_str[2] = 0; > hour = atoi(hr_str); > > ignoring logic errors (we really wanted hour from teh epoch, and not > hour of teh day) and time errors (ctime does localtime, not UTC); > my question is, where does someone learn this technique? > i am gobsmacked.
In his defence: if you had wanted hour of the day, it's far better to use system libraries, which are well debugged, than to roll your own date calculations and discover the need to deal with things like leap-seconds, etc. And for hour-of-the-day, in those cases where that's actually needed, localtime is normally what's wanted by non-sysadmins. Now, still better to just use localtime() and access the tm_hour field from the resulting struct tm, sure. But I've seen self-calculations snowball out of control as people just add a little bit here or there, until you end up with something almost as bad as the extraneous string intermediate steps that "Bill" came up with. So the logic errors are really communication failures at the requirements stage. Now, the string munging, that's another story entirely and someone who'd do that, I can believe that they just weren't paying attention to the requirements as they were specified, as they were off mentally "solving" the problem already. -Phil _______________________________________________ Tech mailing list Tech@lopsa.org http://lopsa.org/cgi-bin/mailman/listinfo/tech This list provided by the League of Professional System Administrators http://lopsa.org/