Just another "How do I handle dates?" WTF



  • Seriously what is it with dates that programmers just don't understand? Why does seemingly every second programmer think s/he needs to reinvent the wheel again when it comes to dates? (also, insert joke about "dates" as in "meeting up with someone you want to have sex with" and "programmers are nerds who just don't have dates and that's why they don't understand them" here).

    for (int i = 0; i <= this.DS.Cust.Count; i++)
    {
    	try
    	{
    		if (this.DS.Cust[i].Deadline.ToString("dd.MM.yyyy") == DateTime.Now.ToString("dd.MM.yyyy"))
    		{
    
    		}
    		else
    		{
    			datecheck = this.DS.Cust[i].Deadline.ToString("dd.MM.yyyy");
    			convertDate = Convert.ToInt16(datecheck.Substring(0, 2));
    			datecheck2 = DateTime.Now.ToString("dd.MM.yyyy");
    			convertDate2 = Convert.ToInt16(datecheck2.Substring(0, 2));
    
    			if ((convertDate == convertDate2) || (convertDate - 1 == convertDate2) || (convertDate - 2 == convertDate2) || (convertDate - 3 == convertDate2))
    			{
    				convertDate = Convert.ToInt16(datecheck.Substring(3, 2));
    				convertDate2 = Convert.ToInt16(datecheck2.Substring(3, 2));
    
    				if (convertDate == convertDate2)
    				{
    					convertDate = Convert.ToInt16(datecheck.Substring(6, 4));
    					convertDate2 = Convert.ToInt16(datecheck2.Substring(6, 4));
    
    					if (convertDate == convertDate2)
    					{
    						doStuff();
    					}
    				}
    			}
    		}
    	}
    	catch
    	{
    	}
    }
    

    It starts off with the good old "if(condition) -> Do Nothing" programming paradigma. This should be a best practice in every programming language.

    Next, we check if the day of the deadline is today (which isn't possible anyway, since we already checked for that in the if...), one day ago, two days ago or three days ago.
    If this is the case, we check if the month is the same. If this is the case, we check for the year too.

    I mean, we could've written something like

    DateTime deadline = getDeadline();
    if(DateTime.Now.Date <= deadline.AddDays(3))
        doStuff();
    

    to achieve the same, but that would be too complicated, I guess.

    What amazes me the most about this is that there already IS a sensible check for "is the deadline today?" in the if-clause. So why the programmer decided to introduce this WTF in the else-clause is beyond me.



  • @Hans_Mueller I like your writing style.

    I seem to recall something about there being a "main site"... I think you'd do well there.



  • @Zecc Yes, there already have been three entries published from code I sent in. I just wanted to avoid a new "Hanzo" series, if I sent all of this in at once. :P



  • @Zecc said in Just another "How do I handle dates?" WTF:

    I seem to recall something about there being a "main site"... I think you'd do well there.

    Please don't banish him to obscurity.



  • There is a function here to convert time in ticks to a struct where part of the algorithm involves looping all the years from epoch.



  • @Hans_Mueller said in Just another "How do I handle dates?" WTF:

    It starts off with the good old "if(condition) -> Do Nothing" programming paradigma. This should be a best practice in every programming language.

    I can't tell you how many times I did this until I finally realized it:

    if (something) continue;
    else {
        executeThis();
    }
    

    Was a somewhat common sight.



  • @Sumireko said in Just another "How do I handle dates?" WTF:

    @Hans_Mueller said in Just another "How do I handle dates?" WTF:

    It starts off with the good old "if(condition) -> Do Nothing" programming paradigma. This should be a best practice in every programming language.

    I can't tell you how many times I did this until I finally realized it:

    if (something) continue;
    else {
        executeThis();
    }
    

    Was a somewhat common sight.

    If you really mean continue as in go to the next iteration of the loop then I have no problem with that. I think it's good for clarity, just like an early return.


  • Impossible Mission - B

    @boomzilla That depends on whether or not the loop body contains anything after the if statement.





  • @boomzilla said in Just another "How do I handle dates?" WTF:

    @Sumireko said in Just another "How do I handle dates?" WTF:

    @Hans_Mueller said in Just another "How do I handle dates?" WTF:

    It starts off with the good old "if(condition) -> Do Nothing" programming paradigma. This should be a best practice in every programming language.

    I can't tell you how many times I did this until I finally realized it:

    if (something) continue;
    else {
        executeThis();
    }
    

    Was a somewhat common sight.

    If you really mean continue as in go to the next iteration of the loop then I have no problem with that. I think it's good for clarity, just like an early return.

    I've also done else continue within a for-loop which is slightly less forgiveable





  • @groo Wow. Just wow.

    Also :eek:


  • Discourse touched me in a no-no place

    @Sumireko

    (unless (something)
        (execute-this))
    

    :trollface:



  • @antiquarian said in Just another "How do I handle dates?" WTF:

    @Sumireko

    (unless (something)
        (execute-this))
    

    :trollface:

    #DEFINE until( while(!
    

    Now that I wrote that, I wonder how many people have attempted to implement (nonstandard/ghetto) "until" loops



  • @Sumireko bug report:

    do {
      // stuff
    } until(condition)
    

    works, but

    do {
      // stuff
    } until (condition)
    

    does not!


  • Winner of the 2016 Presidential Election

    @anotherusername said in Just another "How do I handle dates?" WTF:

    @Sumireko bug report:

    do {
      // stuff
    } until(condition)
    

    works, but

    do {
      // stuff
    } until (condition)
    

    does not!

    Regex to the rescue!

    Filed under: now you have 10e100 problems



  • @Sumireko said in Just another "How do I handle dates?" WTF:

    @antiquarian said in Just another "How do I handle dates?" WTF:

    @Sumireko

    (unless (something)
        (execute-this))
    

    :trollface:

    #DEFINE until( while(!
    

    Now that I wrote that, I wonder how many people have attempted to implement (nonstandard/ghetto) "until" loops

    If that's C/C++/C-like, it should be

    #define until(x) while(!(x))


  • @djls45 If that's C/C++, it should be

     #define until(x) while(!(x))
    

    Also - inb4 someone discovers the hard way why he needs double parens around his nontrivial but relatively simple condition.


  • Impossible Mission Players - A

    @Hans_Mueller said in Just another "How do I handle dates?" WTF:

    about "dates" as in "meeting up with someone you want to have sex with"

    Is that why I don't seem interested in going on dates?

    Damnit...



  • @Gąska anyone that writes non-trivial macro functions should be imprisoned for life

    People that write templates like the boost graph library should be executed in the most painful way as possible.



  • @wharrgarbl said in Just another "How do I handle dates?" WTF:

    @Gąska anyone that writes non-trivial macro functions should be imprisoned for life

    People that write templates like the boost graph library should be executed in the most painful way as possible.

    I wrote a macro and template combo for easy declaration of structures with generated non-trivial getters and setters, and I'm proud of it.



  • @Gąska said in Just another "How do I handle dates?" WTF:

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    @Gąska anyone that writes non-trivial macro functions should be imprisoned for life

    People that write templates like the boost graph library should be executed in the most painful way as possible.

    I wrote a macro and template combo for easy declaration of structures with generated non-trivial getters and setters, and I'm proud of it.

    Macros and templates are great when they save lots of programmer time. Go doesn't have either, so we just use really short variable names.



  • @wharrgarbl said in Just another "How do I handle dates?" WTF:

    anyone that writes non-trivial macro functions should be imprisoned for life

    :wave: I wrote (some of) these macros, what does that get me? https://github.com/Ares-Developers/YRpp/blob/master/ASMMacros.h


  • Winner of the 2016 Presidential Election

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    People that write templates like the boost graph library should be executed in the most painful way as possible.

    Why? I prefer someone else writing those templates to me having to write them myself.



  • @asdf I think @wharrgarbl is saying that good code uses no macros, templates, lowercase letters, functions, digits, symbols, or spaces. Here's an example of a good Hello World:

    LINENUMBERZEROCODEPRINTZEROGOTOONELINENUMBERONECODEPRINTONEGOTOONEZEROLINENUMBE
    RONEZEROCODEPRINTZEROGOTOONEONELINENUMBERONEONECODEPRINTZEROGOTOONEZEROZEROLINE
    NUMBERONEZEROZEROCODEPRINTONEGOTOONEZEROONELINENUMBERONEZEROONECODEPRINTZEROGOT
    OONEONEZEROLINENUMBERONEONEZEROCODEPRINTZEROGOTOONEONEONELINENUMBERONEONEONECOD
    EPRINTZEROGOTOONEZEROZEROZEROLINENUMBERONEZEROZEROZEROCODEPRINTZEROGOTOONEZEROZ
    EROONELINENUMBERONEZEROZEROONECODEPRINTONEGOTOONEZEROONEZEROLINENUMBERONEZEROON
    EZEROCODEPRINTONEGOTOONEZEROONEONELINENUMBERONEZEROONEONECODEPRINTZEROGOTOONEON
    EZEROZEROLINENUMBERONEONEZEROZEROCODEPRINTZEROGOTOONEONEZEROONELINENUMBERONEONE
    ZEROONECODEPRINTONEGOTOONEONEONEZEROLINENUMBERONEONEONEZEROCODEPRINTZEROGOTOONE
    ONEONEONELINENUMBERONEONEONEONECODEPRINTONEGOTOONEZEROZEROZEROZEROLINENUMBERONE
    ZEROZEROZEROZEROCODEPRINTZEROGOTOONEZEROZEROZEROONELINENUMBERONEZEROZEROZEROONE
    CODEPRINTONEGOTOONEZEROZEROONEZEROLINENUMBERONEZEROZEROONEZEROCODEPRINTONEGOTOO
    NEZEROZEROONEONELINENUMBERONEZEROZEROONEONECODEPRINTZEROGOTOONEZEROONEZEROZEROL
    INENUMBERONEZEROONEZEROZEROCODEPRINTONEGOTOONEZEROONEZEROONELINENUMBERONEZEROON
    EZEROONECODEPRINTONEGOTOONEZEROONEONEZEROLINENUMBERONEZEROONEONEZEROCODEPRINTZE
    ROGOTOONEZEROONEONEONELINENUMBERONEZEROONEONEONECODEPRINTZEROGOTOONEONEZEROZERO
    ZEROLINENUMBERONEONEZEROZEROZEROCODEPRINTZEROGOTOONEONEZEROZEROONELINENUMBERONE
    ONEZEROZEROONECODEPRINTONEGOTOONEONEZEROONEZEROLINENUMBERONEONEZEROONEZEROCODEP
    RINTONEGOTOONEONEZEROONEONELINENUMBERONEONEZEROONEONECODEPRINTZEROGOTOONEONEONE
    ZEROZEROLINENUMBERONEONEONEZEROZEROCODEPRINTONEGOTOONEONEONEZEROONELINENUMBERON
    EONEONEZEROONECODEPRINTONEGOTOONEONEONEONEZEROLINENUMBERONEONEONEONEZEROCODEPRI
    NTZEROGOTOONEONEONEONEONELINENUMBERONEONEONEONEONECODEPRINTZEROGOTOONEZEROZEROZ
    EROZEROZEROLINENUMBERONEZEROZEROZEROZEROZEROCODEPRINTZEROGOTOONEZEROZEROZEROZER
    OONELINENUMBERONEZEROZEROZEROZEROONECODEPRINTONEGOTOONEZEROZEROZEROONEZEROLINEN
    UMBERONEZEROZEROZEROONEZEROCODEPRINTONEGOTOONEZEROZEROZEROONEONELINENUMBERONEZE
    ROZEROZEROONEONECODEPRINTZEROGOTOONEZEROZEROONEZEROZEROLINENUMBERONEZEROZEROONE
    ZEROZEROCODEPRINTONEGOTOONEZEROZEROONEZEROONELINENUMBERONEZEROZEROONEZEROONECOD
    EPRINTONEGOTOONEZEROZEROONEONEZEROLINENUMBERONEZEROZEROONEONEZEROCODEPRINTONEGO
    TOONEZEROZEROONEONEONELINENUMBERONEZEROZEROONEONEONECODEPRINTONEGOTOONEZEROONEZ
    EROZEROZEROLINENUMBERONEZEROONEZEROZEROZEROCODEPRINTZEROGOTOONEZEROONEZEROZEROO
    NELINENUMBERONEZEROONEZEROZEROONECODEPRINTZEROGOTOONEZEROONEZEROONEZEROLINENUMB
    ERONEZEROONEZEROONEZEROCODEPRINTONEGOTOONEZEROONEZEROONEONELINENUMBERONEZEROONE
    ZEROONEONECODEPRINTZEROGOTOONEZEROONEONEZEROZEROLINENUMBERONEZEROONEONEZEROZERO
    CODEPRINTZEROGOTOONEZEROONEONEZEROONELINENUMBERONEZEROONEONEZEROONECODEPRINTZER
    OGOTOONEZEROONEONEONEZEROLINENUMBERONEZEROONEONEONEZEROCODEPRINTZEROGOTOONEZERO
    ONEONEONEONELINENUMBERONEZEROONEONEONEONECODEPRINTZEROGOTOONEONEZEROZEROZEROZER
    OLINENUMBERONEONEZEROZEROZEROZEROCODEPRINTZEROGOTOONEONEZEROZEROZEROONELINENUMB
    ERONEONEZEROZEROZEROONECODEPRINTONEGOTOONEONEZEROZEROONEZEROLINENUMBERONEONEZER
    OZEROONEZEROCODEPRINTONEGOTOONEONEZEROZEROONEONELINENUMBERONEONEZEROZEROONEONEC
    ODEPRINTONEGOTOONEONEZEROONEZEROZEROLINENUMBERONEONEZEROONEZEROZEROCODEPRINTZER
    OGOTOONEONEZEROONEZEROONELINENUMBERONEONEZEROONEZEROONECODEPRINTONEGOTOONEONEZE
    ROONEONEZEROLINENUMBERONEONEZEROONEONEZEROCODEPRINTONEGOTOONEONEZEROONEONEONELI
    NENUMBERONEONEZEROONEONEONECODEPRINTONEGOTOONEONEONEZEROZEROZEROLINENUMBERONEON
    EONEZEROZEROZEROCODEPRINTZEROGOTOONEONEONEZEROZEROONELINENUMBERONEONEONEZEROZER
    OONECODEPRINTONEGOTOONEONEONEZEROONEZEROLINENUMBERONEONEONEZEROONEZEROCODEPRINT
    ONEGOTOONEONEONEZEROONEONELINENUMBERONEONEONEZEROONEONECODEPRINTZEROGOTOONEONEO
    NEONEZEROZEROLINENUMBERONEONEONEONEZEROZEROCODEPRINTONEGOTOONEONEONEONEZEROONEL
    INENUMBERONEONEONEONEZEROONECODEPRINTONEGOTOONEONEONEONEONEZEROLINENUMBERONEONE
    ONEONEONEZEROCODEPRINTONEGOTOONEONEONEONEONEONELINENUMBERONEONEONEONEONEONECODE
    PRINTONEGOTOONEZEROZEROZEROZEROZEROZEROLINENUMBERONEZEROZEROZEROZEROZEROZEROCOD
    EPRINTZEROGOTOONEZEROZEROZEROZEROZEROONELINENUMBERONEZEROZEROZEROZEROZEROONECOD
    EPRINTONEGOTOONEZEROZEROZEROZEROONEZEROLINENUMBERONEZEROZEROZEROZEROONEZEROCODE
    PRINTONEGOTOONEZEROZEROZEROZEROONEONELINENUMBERONEZEROZEROZEROZEROONEONECODEPRI
    NTONEGOTOONEZEROZEROZEROONEZEROZEROLINENUMBERONEZEROZEROZEROONEZEROZEROCODEPRIN
    TZEROGOTOONEZEROZEROZEROONEZEROONELINENUMBERONEZEROZEROZEROONEZEROONECODEPRINTZ
    EROGOTOONEZEROZEROZEROONEONEZEROLINENUMBERONEZEROZEROZEROONEONEZEROCODEPRINTONE
    GOTOONEZEROZEROZEROONEONEONELINENUMBERONEZEROZEROZEROONEONEONECODEPRINTZEROGOTO
    ONEZEROZEROONEZEROZEROZEROLINENUMBERONEZEROZEROONEZEROZEROZEROCODEPRINTZEROGOTO
    ONEZEROZEROONEZEROZEROONELINENUMBERONEZEROZEROONEZEROZEROONECODEPRINTONEGOTOONE
    ZEROZEROONEZEROONEZEROLINENUMBERONEZEROZEROONEZEROONEZEROCODEPRINTONEGOTOONEZER
    OZEROONEZEROONEONELINENUMBERONEZEROZEROONEZEROONEONECODEPRINTZEROGOTOONEZEROZER
    OONEONEZEROZEROLINENUMBERONEZEROZEROONEONEZEROZEROCODEPRINTONEGOTOONEZEROZEROON
    EONEZEROONELINENUMBERONEZEROZEROONEONEZEROONECODEPRINTONEGOTOONEZEROZEROONEONEO
    NEZEROLINENUMBERONEZEROZEROONEONEONEZEROCODEPRINTZEROGOTOONEZEROZEROONEONEONEON
    ELINENUMBERONEZEROZEROONEONEONEONECODEPRINTZEROGOTOONEZEROONEZEROZEROZEROZEROLI
    NENUMBERONEZEROONEZEROZEROZEROZEROCODEPRINTZEROGOTOONEZEROONEZEROZEROZEROONELIN
    ENUMBERONEZEROONEZEROZEROZEROONECODEPRINTONEGOTOONEZEROONEZEROZEROONEZEROLINENU
    MBERONEZEROONEZEROZEROONEZEROCODEPRINTONEGOTOONEZEROONEZEROZEROONEONELINENUMBER
    ONEZEROONEZEROZEROONEONECODEPRINTZEROGOTOONEZEROONEZEROONEZEROZEROLINENUMBERONE
    ZEROONEZEROONEZEROZEROCODEPRINTZEROGOTOONEZEROONEZEROONEZEROONELINENUMBERONEZER
    OONEZEROONEZEROONECODEPRINTONEGOTOONEZEROONEZEROONEONEZEROLINENUMBERONEZEROONEZ
    EROONEONEZEROCODEPRINTZEROGOTOONEZEROONEZEROONEONEONELINENUMBERONEZEROONEZEROON
    EONEONECODEPRINTZEROGOTOONEZEROONEONEZEROZEROZEROLINENUMBERONEZEROONEONEZEROZER
    OZEROCODEPRINTZEROGOTOONEZEROONEONEZEROZEROONELINENUMBERONEZEROONEONEZEROZEROON
    ECODEPRINTZEROGOTOONEZEROONEONEZEROONEZEROLINENUMBERONEZEROONEONEZEROONEZEROCOD
    EPRINTONEGOTOONEZEROONEONEZEROONEONELINENUMBERONEZEROONEONEZEROONEONECODEPRINTZ
    EROGOTOONEZEROONEONEONEZEROZEROLINENUMBERONEZEROONEONEONEZEROZEROCODEPRINTZEROG
    OTOONEZEROONEONEONEZEROONELINENUMBERONEZEROONEONEONEZEROONECODEPRINTZEROGOTOONE
    ZEROONEONEONEONEZEROLINENUMBERONEZEROONEONEONEONEZEROCODEPRINTZEROGOTOONEZEROON
    EONEONEONEONELINENUMBERONEZEROONEONEONEONEONECODEPRINTONE
    

    Newlines added for readability but whitespace is completely insignificant in BIT, including in the middle of a word.


  • Discourse touched me in a no-no place

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    the boost graph library

    :wtf:

    When compiling programs that use the BGL, be sure to compile with optimization.

    Why would it be important to enable optimization when compiling programs that use the library? How the heck are you supposed to debug code that uses it? I can understand turning on opt for speed, but the way it's written makes me think they need it for correctness, which is totally crazy. (I know it's virtually done with templates and so effectively needs to be built in specialised form on each use. I don't like that sort of thing about C++, but it's how that language rolls.)

    You might have other valid concerns. I'm just happy to not need to use it. ;)



  • @asdf said in Just another "How do I handle dates?" WTF:

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    People that write templates like the boost graph library should be executed in the most painful way as possible.

    Why? I prefer someone else writing those templates to me having to write them myself.

    What about nobody writing shit like the boost graph language? That's worse than anything I ever saw on this site front page.



  • @DCoder said in Just another "How do I handle dates?" WTF:

    I wrote (some of) these macros, what does that get me?

    0_1492173164651_images (17).jpg


  • Discourse touched me in a no-no place

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    anyone that writes non-trivial macro functions should be imprisoned for life

    User name checks out. :trollface:



  • @Hans_Mueller said in Just another "How do I handle dates?" WTF:

    I mean, we could've written something like
    DateTime deadline = getDeadline();
    if(DateTime.Now.Date <= deadline.AddDays(3))
    doStuff();

    to achieve the same, but that would be too complicated, I guess.

    That wouldn't actually achieve the same, because the existing code enforces that the current date has to be in the same month as the deadline in order to do stuff. Whether you actually want that or not is another question altogether (probably not, but I can imagine in some cases you might).

    I remember seeing a similar piece of code in SSDS which was basically commented along the lines of "too hard to get this right for the first day of a month, so just leave it like this". In that case you know that fixing the code to be sensible also fixes it to be correct.



  • @dkf said in Just another "How do I handle dates?" WTF:

    @wharrgarbl said in Just another "How do I handle dates?" WTF:

    the boost graph library

    :wtf:

    When compiling programs that use the BGL, be sure to compile with optimization.

    Why would it be important to enable optimization when compiling programs that use the library? How the heck are you supposed to debug code that uses it? I can understand turning on opt for speed, but the way it's written makes me think they need it for correctness, which is totally crazy.

    I guess it's not for correctness, as it is for practical usability. When every character of source code translates into 200 lines of code with 20 nested function calls, you either enable inlining and monomorphization, or end up with hello world that takes 20 seconds to execute and leaves behind 200MB object files


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.