CA Service Management

 View Only
  • 1.  CA Service Desk date function in spell code

    Posted Dec 16, 2016 03:12 AM

    Dear all,

     

    I need some help for a couple of date calculations in spl codes.

    I want to find some unix time values for specific dates, for example tomorrow morning 08 am or first day 02 pm of upcoming month. Are there any date functions except now() for spl codes? Is it possible to make calculations by adding days / months / years to a date?

     

    Regards,

    Utku



  • 2.  Re: CA Service Desk date function in spell code
    Best Answer

    Posted Dec 16, 2016 03:48 AM

    Hi Utku,

    here is an example that shows all possible conversions:

    void showdate() {
         int msg_i;
         date znow, manual_date;
         znow = now();
         printf(format("int date: %d\n", (int)znow));     // unix timestamp
         printf(format("str date: %s\n", (date)znow));     // string
        
         manual_date = (date)"12/16/2016 13:40:26";     // defining date manually as string
         manual_date = (int)manual_date + (60 * 60 * 24);     // adding 24 hours without workshift
         printf(format("manual date: %s\n", (string)manual_date));
        
         send_wait(0, top_object(), "call_attr", "wrkshft", "val_by_key", "sym", "(08:30-17:30)(9x5)", 1, "sched");     // fetching workshift by name
         printf(format("shifted date: %s\n", (date)workshift_work2abs((string)msg[1], (date)now(), (duration)(60 * 60 * 9))));     // adding 9 hours using (08:30 am - 7:30 pm) workshift
    }

    output:

    D:\Scripts\bop>bop_cmd -u sd -f exec.frg showdate()
    int date: 1481877626
    str date: 12/16/2016 13:40:26
    manual date: 12/17/2016 13:40:26
    shifted date: 12/19/2016 13:40:26

     

    BONUS:

    date tomorrow_morning;
    tomorrow_morning = (date)format("%s 08:00:00", substr((string)((date)((int)now() + (int)(24 * 60 * 60))), 0, 10));
    printf(format("Tomorrow morning: %s\n", tomorrow_morning));
    // Tomorrow morning: 12/17/2016 08:00:00

     

    Regards,

    cdtj



  • 3.  Re: CA Service Desk date function in spell code

    Posted Dec 19, 2016 08:35 AM

    YES!! VERY usefull....

    Thank you so much cdtj.



  • 4.  Re: CA Service Desk date function in spell code

    Posted Dec 20, 2016 12:44 PM

    great... thanks a lot cdtj



  • 5.  RE: CA Service Desk date function in spell code

    Posted Jun 14, 2023 06:01 AM
    // returns weekday of timestamp, 0 (sunday) - 6 (saturday)
    int getDay(int timestamp) {
        return (int)(timestamp / 86400 + 4) % 7;
    }
    
    // returns hour of timestamp, 0 - 23
    int getHours(int timestamp) {
        int day;
        day = ((int)(timestamp / 86400.0) * 86400);
        return (timestamp - day) / 3600;
    }
    
    // returns minute of timestamp, 0 - 59
    int getMinutes(int timestamp) {
        int day, hours;
        day = (int)(timestamp / 86400.0) * 86400;
        hours = (int)((timestamp - day) / 3600);
        return (int)((timestamp - day - hours * 3600) / 60);
    }
    
    // returns second of timestamp, 0 - 59
    int getSeconds(int timestamp) {
        int day, hours, minutes;
        day = (int)(timestamp / 86400.0) * 86400;
        hours = (int)((timestamp - day) / 3600);
        minutes = (int)((timestamp - day - hours * 3600) / 60);
        return timestamp - day - hours * 3600 - minutes * 60;
    }
    
    // returns full 4-digit year of timestamp, e. g. 1973
    int getFullYear(int timestamp) {
        int year, days;
        
        year = 1970;
        
        while(timestamp > 0) {
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                year++;
            }
        }
        return year;
    }
    
    // returns month of timestamp, 0 - 11
    int getMonth(int timestamp) {
        int year, month, month_days[12], days, tmp;
        
        year = 1970;
        month = 0;
        month_days[0]  = 31; month_days[1]  = 28; month_days[2]  = 31; month_days[3]  = 30; month_days[4]  = 31; month_days[5]  = 30; 
        month_days[6]  = 31; month_days[7]  = 31; month_days[8]  = 30; month_days[9]  = 31; month_days[10] = 30; month_days[11] = 31;
        tmp = 0;
        
        while(timestamp > 0) {
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                tmp = timestamp;
                year++;
            }
        }
        timestamp = tmp;
        
        while(timestamp > 0) {
            if(month == 1 && days == 366) {
                timestamp -= 29 * 86400;
            }
            else {
                timestamp -= month_days[month] * 86400;
            }
            if(timestamp >= 0) {
                month++;
            }
        }
        return month;
    }
    
    // returns day of the month of timestamp, 1 - 31
    int getDate(int timestamp) {
        int year, month, month_days[12], days, day, tmp;
        
        year = 1970;
        month = 0;
        month_days[0]  = 31; month_days[1]  = 28; month_days[2]  = 31; month_days[3]  = 30; month_days[4]  = 31; month_days[5]  = 30; 
        month_days[6]  = 31; month_days[7]  = 31; month_days[8]  = 30; month_days[9]  = 31; month_days[10] = 30; month_days[11] = 31;
        day = 0;
        tmp = 0;
        
        while(timestamp > 0) {
            tmp = timestamp;
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                year++;
            }
        }
        timestamp = tmp;
        
        while(timestamp > 0) {
            if(month == 1 && days == 366) {
                timestamp -= 29 * 86400;
            }
            else {
                timestamp -= month_days[month] * 86400;
            }
            if(timestamp >= 0) {
                tmp = timestamp;
                month++;
            }
        }
        timestamp = tmp;
        
        while(timestamp >= 0) {
            timestamp -= 86400;
            day++;
        }
        return day;
    }
    
    // returns 0 or 1, wether timestamp is in summertime (germany only) or not
    // (summertime is between last sunday in march 2:00 am and last sunday in october 2.00 am)
    //
    // requires getMonth(), getDate() and getDay()
    int isSummertime(int timestamp) {
        int day, m_sunday, o_sunday;
        
        // Math.abs(day);
        day = (int)timestamp / 86400.0;
        day = day * 86400;
        
        // this is real bullshit, but i dunno how to make it better without date to timestamp functions
        // gets first day of the year
        while(!(getMonth(day) == 0 && getDate(day) == 1)) {
            day -= 86400;
        }
        
        while(getMonth(day) < 3) {
            if(getDay(day) == 0) {
                m_sunday = day;
            }
            day += 86400;
        }
        m_sunday += 7200;
        
        while(getMonth(day) < 10) {
            if(getDay(day) == 0) {
                o_sunday = day;
            }
            day += 86400;
        }
        o_sunday += 7200;
    
        if(timestamp >= m_sunday && timestamp <= o_sunday) {
            return 1;
        }
        else {
            return 0;
        }
    }
    

    !!! NEVER really tested :)




  • 6.  RE: CA Service Desk date function in spell code

    Posted Jun 14, 2023 06:02 AM
    // returns weekday of timestamp, 0 (sunday) - 6 (saturday)
    int getDay(int timestamp) {
        return (int)(timestamp / 86400 + 4) % 7;
    }
    
    // returns hour of timestamp, 0 - 23
    int getHours(int timestamp) {
        int day;
        day = ((int)(timestamp / 86400.0) * 86400);
        return (timestamp - day) / 3600;
    }
    
    // returns minute of timestamp, 0 - 59
    int getMinutes(int timestamp) {
        int day, hours;
        day = (int)(timestamp / 86400.0) * 86400;
        hours = (int)((timestamp - day) / 3600);
        return (int)((timestamp - day - hours * 3600) / 60);
    }
    
    // returns second of timestamp, 0 - 59
    int getSeconds(int timestamp) {
        int day, hours, minutes;
        day = (int)(timestamp / 86400.0) * 86400;
        hours = (int)((timestamp - day) / 3600);
        minutes = (int)((timestamp - day - hours * 3600) / 60);
        return timestamp - day - hours * 3600 - minutes * 60;
    }
    
    // returns full 4-digit year of timestamp, e. g. 1973
    int getFullYear(int timestamp) {
        int year, days;
        
        year = 1970;
        
        while(timestamp > 0) {
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                year++;
            }
        }
        return year;
    }
    
    // returns month of timestamp, 0 - 11
    int getMonth(int timestamp) {
        int year, month, month_days[12], days, tmp;
        
        year = 1970;
        month = 0;
        month_days[0]  = 31; month_days[1]  = 28; month_days[2]  = 31; month_days[3]  = 30; month_days[4]  = 31; month_days[5]  = 30; 
        month_days[6]  = 31; month_days[7]  = 31; month_days[8]  = 30; month_days[9]  = 31; month_days[10] = 30; month_days[11] = 31;
        tmp = 0;
        
        while(timestamp > 0) {
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                tmp = timestamp;
                year++;
            }
        }
        timestamp = tmp;
        
        while(timestamp > 0) {
            if(month == 1 && days == 366) {
                timestamp -= 29 * 86400;
            }
            else {
                timestamp -= month_days[month] * 86400;
            }
            if(timestamp >= 0) {
                month++;
            }
        }
        return month;
    }
    
    // returns day of the month of timestamp, 1 - 31
    int getDate(int timestamp) {
        int year, month, month_days[12], days, day, tmp;
        
        year = 1970;
        month = 0;
        month_days[0]  = 31; month_days[1]  = 28; month_days[2]  = 31; month_days[3]  = 30; month_days[4]  = 31; month_days[5]  = 30; 
        month_days[6]  = 31; month_days[7]  = 31; month_days[8]  = 30; month_days[9]  = 31; month_days[10] = 30; month_days[11] = 31;
        day = 0;
        tmp = 0;
        
        while(timestamp > 0) {
            tmp = timestamp;
            days = 365;
            if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                days = 366;
            }
            timestamp -= days * 86400;
            if(timestamp >= 0) {
                year++;
            }
        }
        timestamp = tmp;
        
        while(timestamp > 0) {
            if(month == 1 && days == 366) {
                timestamp -= 29 * 86400;
            }
            else {
                timestamp -= month_days[month] * 86400;
            }
            if(timestamp >= 0) {
                tmp = timestamp;
                month++;
            }
        }
        timestamp = tmp;
        
        while(timestamp >= 0) {
            timestamp -= 86400;
            day++;
        }
        return day;
    }
    
    // returns 0 or 1, wether timestamp is in summertime (germany only) or not
    // (summertime is between last sunday in march 2:00 am and last sunday in october 2.00 am)
    //
    // requires getMonth(), getDate() and getDay()
    int isSummertime(int timestamp) {
        int day, m_sunday, o_sunday;
        
        // Math.abs(day);
        day = (int)timestamp / 86400.0;
        day = day * 86400;
        
        // this is real bullshit, but i dunno how to make it better without date to timestamp functions
        // gets first day of the year
        while(!(getMonth(day) == 0 && getDate(day) == 1)) {
            day -= 86400;
        }
        
        while(getMonth(day) < 3) {
            if(getDay(day) == 0) {
                m_sunday = day;
            }
            day += 86400;
        }
        m_sunday += 7200;
        
        while(getMonth(day) < 10) {
            if(getDay(day) == 0) {
                o_sunday = day;
            }
            day += 86400;
        }
        o_sunday += 7200;
    
        if(timestamp >= m_sunday && timestamp <= o_sunday) {
            return 1;
        }
        else {
            return 0;
        }
    }
    

    !!! NEVER really tested :)