Wednesday, January 18, 2006

Two days before the day after tomorrow

Clearly a South Park fan worked here once:
cat oracle_GetThisworkingDay

DATE=`date +%Y%m%d%H%M`
CUTOFF=$2

#!/bin/ksh
# oracle_GetThisworkingDay
# Script to retrieve the current working day (YYYYMMDD) from
# the working_calendar table in the Oracle database.
oracle_GetPreviousWorkingDay `oracle_GetNextWorkingDay $DATE $CUTOFF`

I'll spare you the contents of these scripts. Suffice to say they call the following procedures:
   FUNCTION previous_day (
      p_date                              DATE DEFAULT SYSDATE
   )
      RETURN VARCHAR2
   IS
      v_result                      VARCHAR2 (10);
   BEGIN
      SELECT dt
        INTO v_result
        FROM working_calendar
       WHERE dt = (SELECT MAX (dt)
                     FROM working_calendar
                    WHERE dt < p_date );
    RETURN TO_CHAR(v_result,'YYYYMMDD');
  END;
( nice use of SQL there ) and of course...
   FUNCTION next_day (
      p_now                               DATE DEFAULT CURRENT_DATE
   )
      RETURN DATE
   IS
      RESULT                        DATE;
   BEGIN
      SELECT MIN (dt)
        INTO RESULT
        FROM working_calendar
       WHERE dt >= p_now + 1;

      RETURN RESULT;
   END;

4 comments:

Scott Swank said...

I'm rather fond of the fact that next_day simlply returns a date. By comparison, previous_day fetches the date into a varchar2 and then implicitly converts the varchar2 back to a date so that it can apply to_char() to obtain a varchar2.

Sergei said...

If you don't understand the logic, it does not mean where is no logic.

This script means, that if today is not working day, then the date of the last working date should be used.

I agree, that more understandable would be to check, if today is a working day, and then return a proper date, but this solution is more elegant in my mind.

Thai Rices said...

Sergei,

The fact that people like you condone this kind of nonsense is the reason it keeps happening.

Keep it up, I need the laughs.

Sergei said...

I am not sure, the person, who wrote this, cares about my condoness.

I see here, that the person(at least the one, who wrote the script) is smart enouth to have some reason for this kind of code.

I also almost 100% sure, that SQL part and script were written by different people in different points of time.

Whatever, it is much better than the code I have seen:

var f = 0;

100 lines of code

if f != 0 then
100 lines of code
end if