16 Jan, 2010, Bojack wrote in the 1st comment:
Votes: 0
I recently got back into coding again and immediately got a project by the "boss". Anyways he wanted something to start every thursday and last 24hrs so I came up with this:
char buffer[80];
char buf[MSL];
struct tm * timeinfo;
char * weekday[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};

time (&current_time);
timeinfo = localtime (&current_time);
strftime(buffer, 80, "%A", timeinfo);

mktime (timeinfo);
if (!str_cmp (buffer, weekday[timeinfo->tm_wday]) )
{
do code here


My question is could string compare actually work like this seeing how I used strftime to only take the day of the week "%A" which I know is the word of the day?
16 Jan, 2010, elanthis wrote in the 2nd comment:
Votes: 0
It's called "cron" and there's no reason in hell to write your own app for this.

If you are sold on the idea of poorly reimplementing 30+ year old standard utilities that make up one of the core foundational tools of every system administrator out there, I definitely see no reason to do a string comparison on locale-sensitive week names instead of just comparing the day of week as an integer.
16 Jan, 2010, Bojack wrote in the 3rd comment:
Votes: 0
This is within a mud, its to turn on a specific quest per day set.
16 Jan, 2010, Scandum wrote in the 4th comment:
Votes: 0
It might work, but like elanthis said it's easier to do an integer comparison:
if (timeinfo->tm_wday == 4)
{
do_stuff();
}
16 Jan, 2010, elanthis wrote in the 5th comment:
Votes: 0
Aaah, my bad. The way you mentioned boss I thought you meant this was a general programming job. Sorry. :)

In that case, do what Scandum said, it is much easier and what you have is technically broken (change locale settings, strftime will give you a different string, and your string comparison fails).
16 Jan, 2010, Bojack wrote in the 6th comment:
Votes: 0
Yea soon as you said integer, it clicked in my head but now im just curious if string compare will work just for future reference using %A instead.
16 Jan, 2010, Bojack wrote in the 7th comment:
Votes: 0
UPDATE: Yeah both methods work, using integer or using str_cmp works as long as you have the right % symbol for strftime.
16 Jan, 2010, elanthis wrote in the 8th comment:
Votes: 0
No, Bojack, it does not, and I already explained that.

Maybe a deeper explanation will help sort you out. strftime will return a result based on current locale, which is a user-configurable setting regarding which language an application should use for system library results. If you ever run the code with a non-English locale, strftime might return totally different text than you expect, since in other languages %A will not be "Thursday" on Thursdays, but will be whatever the translation of Thurday is in that language. It worked when you tried it only because the environment happened to be aligned properly with your expectations, but it is totally wrong to rely on that kind of thing. You only care that the day is Thursday, not that the system happens to call Thursday "Thursday" and not "Donnerstag" or "Jueves" or "Gioved" or so on. Just because you expect the code to always run with an English locale set doesn't mean it does, and writing code that relies on that just because it happens to work now is incredibly stupid.
16 Jan, 2010, Bojack wrote in the 9th comment:
Votes: 0
I wrote this code knowing full well of what type of server I am on, what I know about it and what to look for. End of story.
16 Jan, 2010, elanthis wrote in the 10th comment:
Votes: 0
Which is incompetent, because you DON'T know what type of server you WILL be on, nor what some other future developer or sysadmin who doesn't have the same assumptions you do might do with the environment settings. Shit changes in ways you can't always plan for, and code that relies on something variable never varying just because it works now is _bad code_. End of story.
16 Jan, 2010, Twisol wrote in the 11th comment:
Votes: 0
Hear, hear.
16 Jan, 2010, Kline wrote in the 12th comment:
Votes: 0
Bojack said:
I wrote this code knowing full well of what type of server I am on, what I know about it and what to look for. End of story.

Testy reply to be the one asking for help? It's generally against the grain to blatantly spit in the face of good advice.
16 Jan, 2010, David Haley wrote in the 13th comment:
Votes: 0
Especially since the question was with respect to "future reference", it makes sense to take the technically correct solution that will always work, not the variable solution that happens to work now but is not at all guaranteed to work in general.

Writing code that is not technically correct and being content with it because it happens to work "now" is a great way to make trouble later on. In another thread Elanthis talked about spending lots of time fixing problems that other people put in; this is exactly the kind of problem that would need to be fixed later when it would be so easy to get it right the first time.
16 Jan, 2010, Koron wrote in the 14th comment:
Votes: 0
elanthis said:
Which is incompetent, because you DON'T know what type of server you WILL be on

Or maybe he does. Okay, so his implementation might not have been the best, but back off, eh?
16 Jan, 2010, Mudder wrote in the 15th comment:
Votes: 0
Koron said:
elanthis said:
Which is incompetent, because you DON'T know what type of server you WILL be on

Or maybe he does. Okay, so his implementation might not have been the best, but back off, eh?


Elanthis may have been a bit harsh but the guy was asking for advice with this exact thing. Yes it may technically work but it's also technically bad code. I think elanthis was just trying to get that point across, regardless of how he made the other guy feel which obviously offended him and resulted in snippy retorts.

If I've learned anything about being here it is when asking for advice, take it or leave it. Don't take offense to the way information is given because in the end people are only responding to help you. People here aren't customer service nerds, they're computer nerds. :wink:
16 Jan, 2010, David Haley wrote in the 16th comment:
Votes: 0
Well, it's also that you shouldn't ask questions if you won't like the answer or don't like being told that your initial approach was flawed. It's not that question-askers should bow before question-answerers, but generally if somebody is asking a question in the first place it's (usually) an indication that they have stuff to learn. Ah well.

Mudder said:
People here aren't customer service nerds, they're computer nerds. :wink:

Not sure what you're implying here.
16 Jan, 2010, Lyanic wrote in the 17th comment:
Votes: 0
David Haley said:
Mudder said:
People here aren't customer service nerds, they're computer nerds. :wink:

Not sure what you're implying here.

He's implying that your customer service skills suck, computer nerd.

Also, where do you keep disappearing to, Mudder?
16 Jan, 2010, David Haley wrote in the 18th comment:
Votes: 0
Lyanic said:
He's implying that your customer service skills suck, computer nerd.

Not sure why that is remotely relevant, but eh. Ok. (And people should speak for themselves, not others, when they talk about being socially inept…)
17 Jan, 2010, Skol wrote in the 19th comment:
Votes: 0
Bojack, one thing you might do (I would say 'should' do) for readability in the code:
//Somewhere with your defines
#define THURSDAY 4

if (timeinfo->tm_wday == THURSDAY)
{
do_stuff((.),(.));
}
17 Jan, 2010, elanthis wrote in the 20th comment:
Votes: 0
Yeah, yeah. I'm an asshole. I drive the point home because I want to make damn sure some other new coder doesn't hit Google, find the thread, and then make a crappy decision because someone made ridiculous claims and I was too lazy to just spell it out and make it right. I don't mind people not knowing how to do something; everything I know I had to learn and at some point did not know. However, when you consciously put effort into publicly stating that doing things the wrong way is acceptable you are actively harming other people by feeding them bad information that will just bite them in the ass. Any public discussion should be held with the knowledge that someone might try to learn from it, and that no public discussion is (or should be, at least) about just the people participating in it.

P.S. I am sorry I used the word "incompetent." I overuse it, especially on people who don't really deserve it.
0.0/22