Check whether the previous day is a holiday and/or Sunday

Asked

Viewed 2,606 times

6

I’m having trouble developing a date validation logic. The variable dataFecha is called with -1 day to already validate the previous day.

But when I go to test the validations, he did not subtract (-1) day from variable, to validate Saturday and/or before holiday.

Validations

  1. Validate if the previous day is not Sunday. If so, pick up the box from saturday.
  2. Validate if the previous day is not a holiday. If so, take the day before the holiday. ¹ If the day before the holiday is Sunday, take the Saturday box.

Code.

  //Verificar se houve fechamento para caixa no dia anterior e/ou se esta aberto.
  FechamentoCaixa objFec = new FechamentoCaixa();
  Feriados_Nacionais objFeriado = new Feriados_Nacionais();
  //Instancia a varíavel com -1 dia para validar o dia anterior
  dataFecha = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

  ////Verifica se o dia anterior é domingo. Caso for, irá verificar o caixa de sabado.
  if (Convert.ToDateTime(dataFecha).DayOfWeek == DayOfWeek.Sunday) {
   dataFecha = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
  }
  //Verifica se o dia anterior é feriado. Caso for, irá verificar o dia antes de ontem
  if (objFeriado.ConsultarFeriado(dataFecha) > 0) {
   dataFecha = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
   //Verifica se o dia anterior ao feriado é domingo. Caso for, irá verificar o de sabado.
   if (Convert.ToDateTime(dataFecha).DayOfWeek == DayOfWeek.Sunday) {
    dataFecha = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
   }
  }
  • 1

    I think all you have to do is pick up the biggest box before the current date. If by chance the company does not work in the middle of the week, or run on a holiday or Sunday, as it would ?!

2 answers

8


You are not taking the closing date to subtract 1 day. It has to be cumulative. Note that I have greatly simplified the code.

using System;

public class Program {
    public static void Main() {
        var dataFechamento = DateTime.Now.AddDays(-1);
        dataFechamento = isSunday(dataFechamento);
        if (new Feriados_Nacionais().ConsultarFeriado(dataFecha) > 0) {
            dataFechamento = dataFechamento.AddDays(-1);
            dataFechamento = isSunday(dataFechamento);
        }
        Console.WriteLine(dataFechamento);
        DateTime isSunday(DateTime date) => date.DayOfWeek == DayOfWeek.Sunday ? date.AddDays(-1) : date;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

To facilitate the test and not have create the holiday object I switched to true in Ides online, and I used a fixed date, but here’s how your code should be.

I think there are other problems in this code, including concept. Mainly in Feriados_Nacionais.

  • bigown, the method ConsultarFeriado wouldn’t look cool as an extension method?

  • 1

    @Marconi I do not know, almost always this is abused, if it is very well thought out, maybe, but it does not seem to be the case. Actually it would be more a static method, and even this version C# does not allow static extension method, it may be that it has in 8.

  • I was going to do one thing and did another, the local function needs to have another name, and you can simplify more, then I arrange :)

2

First I tried to use only the type Datetime in its algorithm. Another detail, the Datetime.Now returns the current day, soon if it is Monday, the previous day is Sunday, to catch Saturday, you have to decrease two of the Datetime.Now.

//Verificar se houve fechamento para caixa no dia anterior e/ou se esta aberto.
  FechamentoCaixa objFec = new FechamentoCaixa();
  Feriados_Nacionais objFeriado = new Feriados_Nacionais();
  //Instancia a varíavel com -1 dia para validar o dia anterior
  DateTime dataFecha = DateTime.Now.AddDays(-1);

  ////Verifica se o dia anterior é domingo. Caso for, irá verificar o caixa de sabado.
  if (dataFecha.DayOfWeek == DayOfWeek.Sunday) {
   dataFecha = DateTime.Now.AddDays(-2); //retorna sábado
  }
  //Verifica se o dia anterior é feriado. Caso for, irá verificar o dia antes de ontem
  if (objFeriado.ConsultarFeriado(dataFecha.ToString("yyyy-MM-dd")) > 0) {
   dataFecha = DateTime.Now.AddDays(-1);
   //Verifica se o dia anterior ao feriado é domingo. Caso for, irá verificar o de sabado.
   if (dataFecha.DayOfWeek == DayOfWeek.Sunday) {
    dataFecha = DateTime.Now.AddDays(-2); //retorna sábado
   }
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.