CA Service Management

  • 1.  Date functions on Spel

    Posted Nov 16, 2018 08:49 AM

    Hi all,

     

    is there any way to get weekname, week number, month and other date details on spel code? We need to validate if a ticket is opened the X business day of a month, the N week of month and other details but i cant find any documentation to get this info.

     

    Thanks in advance.

    Regards,



  • 2.  Re: Date functions on Spel

    Posted Dec 10, 2018 10:33 AM

    Hi.

    There are no core builtin date functions available to get this kind of Information.

    Nevertheless, there are three functions available beside the string conversion of a date, which might help you in achieving your goal.

    The functions are:

    NameArgumentsreturn valueDescription
    workshift_work2absstring schedule,
    date start_date,
    duration  dur
    dateReturns a target date reflecting the start_date , the Duration and the given schedule.
    workshift_abs2workstring schedule,
    date start_date,
    date end_date
    durationreturns a duration matching into the given schedule reflecting the start and end date
    get_pos_date

    int pos,
    int day,
    int month,

    int year

    date

    pos=0 : first,
    pos=2 : second,
    pos=3 : third,
    pos=4 : forth,
    pos=1 : last
    day=0 - 6 / starting with sunday
    month=0 - 11
    year=~1936 - ~2035

    returns a date stamp for the given day

     

    I can imagine some code which will get you the timestamps you are looking for using these functions.

    Hope this helps. Regards

    ..............Michael



  • 3.  Re: Date functions on Spel

    Posted Dec 10, 2018 04:52 PM

    Michael,

    Do you know use "get_pos_date"?

    Can you explain please?



  • 4.  Re: Date functions on Spel

    Posted Dec 10, 2018 05:13 PM

    Hey Daniel.
    The function is used, when calculating timestamps reflecting timezones especially handling  daylight saving time.
    Regards
    ....Michael



  • 5.  Re: Date functions on Spel

    Posted Dec 10, 2018 05:17 PM

    So you are easaly able to get something like "the last sunday in march". If you pass in year=0 the current year will be used.



  • 6.  Re: Date functions on Spel

    Posted Dec 10, 2018 06:06 PM

    Great. Thanks.



  • 7.  Re: Date functions on Spel
    Best Answer

    Posted Dec 10, 2018 01:15 PM

    Some answers...

    Enjoy!

     

    int z_getYear(date zd_date)
    {
         return (int) gsub(substr((string) zd_date, '/[0-9]+ '), '[^0-9]', ''); // Retorna o ANO de uma data
    }


    int z_getMonth(date zd_date)
    {
         return (int) substr((string) zd_date, '[0-9]+'); // Retorna o MES de uma data
    }


    int z_getDay(date zd_date)
    {
         return (int) gsub(substr((string) zd_date, '/[0-9]+/'), '/', ''); // Retorna o DIA de uma data
    }


    int z_getHour(date zd_date)
    {
         return (int) substr((string) zd_date, 11, 2); // Retorna a HORA de uma data
    }


    int z_getMinute(date zd_date)
    {
         return (int) substr((string) zd_date, 14, 2); // Retorna o MINUTO de uma data
    }


    int z_getSecond(date zd_date)
    {
         return (int) substr((string) zd_date, 17, 2); // Retorna o SEGUNDO de uma data
    }


    int z_getWeekday(date zd_date, ...)
    {
         ////////////////////////////////////////////////////////////////////////
         // Metodo:           z_getWeekday
         // Autor:               Daniel Becker Bighelini
         // Criado em:          28/09/2017
         // Modificado por:     Daniel Becker Bighelini
         // Modificado em:     28/09/2017
         //
         // Parametros de entrada:
         // ARG #0 : (date) zd_date          Data de inicio
         // ARG #1 :     (int)  zi_depurar     [OPCIONAL] Define o nivel de depuracao da funcao (0=Desligado, 1=Log resumido, 2=Log detalhado, 3=Super detalhado)
         // ARG #2 :     (string) zs_metodo     [OPCIONAL] Define o nome do metodo que esta chamando este metodo
         //
         // Resultado:
         //          Obtem o dia da semana de uma data.
         //           Algoritmo extraido do video https://www.youtube.com/watch?v=jcfOXF1XiG4
         //
         // Exemplo:
         //          z_getWeekday('12/25/2017')
         //           z_getWeekday('12/25/2017', 1, 'teste')
         ////////////////////////////////////////////////////////////////////////
         // Atribuindo variaveis
         string zs_metodo;
         zs_metodo = "z_getWeekday";
         int zi_depurar;
         
         if (argc>1) zi_depurar = (int) argv[1];
         if (argc>2) zs_metodo = format("%s %s", argv[2], zs_metodo);
         
        ///////////////////////////////////////////////////////////////////////////////////////
        // Inicio do codigo
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Consultando dia, mes e ano...", zs_metodo);
         int month, year, day;
         day = z_getDay(zd_date);
         month = z_getMonth(zd_date);
         year = z_getYear(zd_date);
         
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Calculando dia da semana...", zs_metodo);
         float a, b, c, d, e, f, g, h, i, r;
         a = (12 - month) / 10;
         b = year - a;
         c = month + (12 * a);
         d = (int) b / 100;
         e = d / 4;
         f = e + 2 - d;
         g = (int) (((float) 365 + (float) 1/4) * b);
         h = (int) (((float) 30 + (float) 6001/10000) * (c+1));
         i = f + g + h + day + 5;
         r = (int) i % 7 - 1;
         
         if (zi_depurar > 0) {
              string zs_weekday;
              switch (r)
              {
                   case 0: zs_weekday = "Domingo"; break;
                   case 1: zs_weekday = "Segunda"; break;
                   case 2: zs_weekday = "Terça"; break;
                   case 3: zs_weekday = "Quarta"; break;
                   case 4: zs_weekday = "Quinta"; break;
                   case 5: zs_weekday = "Sexta"; break;
                   case 6: zs_weekday = "Sábado"; break;
              }
              logf(SIGNIFICANT, "%s Retorno: '%s' = '%s'", zs_metodo, zd_date, zs_weekday);
         }

         return r;
         // Fim do codigo
        ///////////////////////////////////     
    }


    string z_getDateToString(date zd_date, ...)
    {
         ////////////////////////////////////////////////////////////////////////
         // Metodo:           z_getDateToString
         // Autor:               Daniel Becker Bighelini
         // Criado em:          28/09/2017
         // Modificado por:     Daniel Becker Bighelini
         // Modificado em:     28/09/2017
         //
         // Parametros de entrada:
         // ARG #0 : (date) zd_date          Define a data que sera transformada em string.
         // ARG #1 :     (int)  zi_depurar     [OPCIONAL] Define o nivel de depuracao da funcao (0=Desligado, 1=Log resumido, 2=Log detalhado, 3=Super detalhado)
         // ARG #2 :     (string) zs_metodo     [OPCIONAL] Define o nome do metodo que esta chamando este metodo
         //
         // Resultado:
         //          Obtem uma string contendo apenas a parte da data de uma variavel 'date'.
         //
         // Exemplo:
         //          z_getDateToString('12/25/2017 00:00:00')
         //           z_getDateToString('12/25/2017 00:00:00', 1, 'teste')
         ////////////////////////////////////////////////////////////////////////
         // Atribuindo variaveis
         string zs_metodo;
         zs_metodo = "z_getDateToString";
         int zi_depurar;
         
         if (argc>1) zi_depurar = (int) argv[1];
         if (argc>2) zs_metodo = format("%s %s", argv[2], zs_metodo);
         
        ///////////////////////////////////////////////////////////////////////////////////////
        // Inicio do codigo
         string zs_regex_date;
         zs_regex_date = '[0-9]+/[0-9]+/[0-9]+';
         
         if (zi_depurar > 0) {
              logf(SIGNIFICANT, "%s REGEX date   : '%s'", zs_metodo, zs_regex_date);
              logf(SIGNIFICANT, "%s Date OLD     : '%s'", zs_metodo, zd_date);
         }
         
         string zs_date_new;
         zs_date_new = substr((string) zd_date, zs_regex_date);
         if (zi_depurar > 0) {
              logf(SIGNIFICANT, "%s Date NEW     : '%s'", zs_metodo, zs_date_new);
         }

         return zs_date_new;
         // Fim do codigo
        ///////////////////////////////////     
    }


    date z_getEasterDay(int zi_year, ...)
    {
         ////////////////////////////////////////////////////////////////////////
         // Metodo:           z_getEasterDay
         // Autor:               Daniel Becker Bighelini
         // Criado em:          28/09/2017
         // Modificado por:     Daniel Becker Bighelini
         // Modificado em:     28/09/2017
         //
         // Parametros de entrada:
         // ARG #0 : (int) zi_year          Ano
         // ARG #1 :     (int) zi_depurar     [OPCIONAL] Define o nivel de depuracao da funcao (0=Desligado, 1=Log resumido, 2=Log detalhado, 3=Super detalhado)
         // ARG #2 :     (string) zs_metodo     [OPCIONAL] Define o nome do metodo que esta chamando este metodo
         //
         // Resultado:
         //          Obtem o dia da pascoa.
         //           Algoritmo extraido do video https://www.youtube.com/watch?v=jcfOXF1XiG4
         //
         // Exemplo:
         //          z_getEasterDay(2017)
         //           z_getEasterDay(2017, 1, 'teste')
         ////////////////////////////////////////////////////////////////////////
         // Atribuindo variaveis
         string zs_metodo;
         zs_metodo = "z_getEasterDay";
         int zi_depurar;
         
         if (argc>1) zi_depurar = (int) argv[1];
         if (argc>2) zs_metodo = format("%s %s", argv[2], zs_metodo);

        ///////////////////////////////////////////////////////////////////////////////////////
        // Inicio do codigo
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Calculando dia da pascoa...", zs_metodo);
         float a, b, c, d, e, f, g, h, i, k, l, m, p, q;
         a = zi_year % 19;
         b = zi_year / 100;
         c = zi_year % 100;
         d = (int) (b / 4);
         e = ((int) b) % 4;
         f = (int) ((b + 8) / 25);
         g = (int) ((1 + b - f) / 3);
         h = (int) (((19 * a) + b + 15 - (d + g))) % 30;
         i = (int) (c / 4);
         k = (int) c % 4;
         l = (int) ((32 + (2 * e) + (2 * i) - (h + k))) % 7;
         m = (int) ((a + (11 * h) + (22 * l)) / 451);
         p = (int) ((h + l + 114 - (7 * m)) / 31); // mes
         q = (int) ((h + l + 114 - (7 * m))) % 31; // dia + 1
         
         date zd_easter;
         zd_easter = (date) format("%d/%d/%d 00:00:00", p, q+1, zi_year);
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Retorno: %d = '%s'", zs_metodo, zi_year, zd_easter);

         return zd_easter;
         // Fim do codigo
        ///////////////////////////////////     
    }


    string z_getHolidaysBrazil(int zi_year, ...)
    {
         ////////////////////////////////////////////////////////////////////////
         // Metodo:           z_getHolidaysBrazil
         // Autor:               Daniel Becker Bighelini
         // Criado em:          28/09/2017
         // Modificado por:     Daniel Becker Bighelini
         // Modificado em:     28/09/2017
         //
         // Parametros de entrada:
         // ARG #0 : (int) zi_year          Ano
         // ARG #1 :     (int) zi_depurar     [OPCIONAL] Define o nivel de depuracao da funcao (0=Desligado, 1=Log resumido, 2=Log detalhado, 3=Super detalhado)
         // ARG #2 :     (string) zs_metodo     [OPCIONAL] Define o nome do metodo que esta chamando este metodo
         //
         // Resultado:
         //          Obtem uma string contendo todos os feriados brasileiros de um ano especifico.
         //           Algoritmo extraido do video https://www.youtube.com/watch?v=jcfOXF1XiG4
         //
         // Exemplo:
         //          z_getHolidaysBrazil(2017)
         //           z_getHolidaysBrazil(2017, 1, 'teste')
         ////////////////////////////////////////////////////////////////////////
         // Atribuindo variaveis
         string zs_metodo;
         zs_metodo = "z_getHolidaysBrazil";
         int zi_depurar;
         
         if (argc>1) zi_depurar = (int) argv[1];
         if (argc>2) zs_metodo = format("%s %s", argv[2], zs_metodo);

        ///////////////////////////////////////////////////////////////////////////////////////
        // Inicio do codigo
         enum {LINES=20, SEGS_DIA=86400};
         date zd_holiday_fixed[LINES], zd_easter;
         zd_easter = z_getEasterDay(zi_year, zi_depurar, zs_metodo);
         int zi_l;
         zi_l = -1;
         
         // Feriados fixos e moveis
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Preenchendo matriz de feriados fixos e moveis...", zs_metodo);
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("01/01/%d 00:00:00", zi_year);     // Dia mundial da paz
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("02/02/%d 00:00:00", zi_year);     // Navegantes
         zi_l++; zd_holiday_fixed[zi_l] = (date) ((int) zd_easter - (47 * SEGS_DIA));     // Carnaval
         zi_l++; zd_holiday_fixed[zi_l] = (date) ((int) zd_easter - (2 * SEGS_DIA));          // Paixao de cristo
         zi_l++; zd_holiday_fixed[zi_l] = zd_easter;                                                  // Pascoa
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("04/21/%d 00:00:00", zi_year);     // Tiradentes
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("05/01/%d 00:00:00", zi_year);     // Dia do trabalho
         zi_l++; zd_holiday_fixed[zi_l] = (date) ((int) zd_easter + (60 * SEGS_DIA));     // Corphus Christi
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("09/07/%d 00:00:00", zi_year);     // Dia da independencia
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("09/20/%d 00:00:00", zi_year);     // Revolucao farroupilha
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("10/12/%d 00:00:00", zi_year);     // Nossa senhora de Aparecida
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("11/02/%d 00:00:00", zi_year);     // Dia de finados
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("11/15/%d 00:00:00", zi_year);     // Proclamacao da republica
         zi_l++; zd_holiday_fixed[zi_l] = (date) format("12/25/%d 00:00:00", zi_year);     // Natal
         zi_l++;
         
         string zs_holidays;
         zs_holidays = '';

         int zi_i;
         for (zi_i=0; zi_i<zi_l; zi_i++) {
              if (zi_depurar > 0) logf(SIGNIFICANT, "%s '%s'", zs_metodo, zd_holiday_fixed[zi_i]);
              zs_holidays += format("%s {} ", z_getDateToString(zd_holiday_fixed[zi_i], zi_depurar, zs_metodo));
         }
         if (zi_depurar > 0) logf(SIGNIFICANT, "%s Feriados: '%s'", zs_metodo, zs_holidays);

         return zs_holidays;
         // Fim do codigo
        ///////////////////////////////////     
    }


  • 8.  Re: Date functions on Spel

    Posted Dec 17, 2018 03:52 PM

    Hi All,

     

    Sorry for my delay to answer, this is what im looking for!

     

    Thanks!!!