Por que os dias de semana do Excel estão errados em 1900?

26

Esta questão é baseada nas observações de AdamV em sua resposta em Como faço para obter o nome do dia em uma célula no Excel?

Quando A1 tem o valor 2009-08-01, então:

  • =WEEKDAY(A1) obterá 7
  • =TEXT(7, "dddd") obterá Saturday
  • =TEXT(7,"dddd, yyyy-mm-dd") obterá Saturday, 1900-01-07
  • =TEXT(1,"dddd, yyyy-mm-dd") obterá Sunday, 1900-01-01
  • =TEXT("1900-01-01","dddd, yyyy-mm-dd") também obterá Sunday, 1900-01-01

Os dois últimos estão errados: 1 de janeiro de 1900 é na verdade uma segunda-feira.
Várias fontes parecem confirmar que:

O que estou perdendo? Por que o Excel está fazendo isso errado?

    
por Jeroen Wiert Pluimers 30.09.2012 / 16:43

2 respostas

40

Conforme descrito na KB 214058 :

da Microsoft

Days of the week before March 1, 1900 are incorrect in Excel

MORE INFORMATION

When the date system in Microsoft Excel was originally created, it was designed to be fully compatible with date systems used by other spreadsheet programs.

However, in this date system, the year 1900 is incorrectly interpreted as a leap year. Because there is no February 29 ("leap day") in the year 1900, the day of the week for any date before March 1, 1900 (the day after the "leap day"), is not computed correctly.

Os "outros programas de planilha" referem-se ao Lotus 1-2-3 , bastante popular naquela época, e assumiu incorretamente que o ano de 1900 foi um ano bissexto. Isso é explicado com mais detalhes em KB 214326 :

Excel 2000 incorrectly assumes that the year 1900 is a leap year

MORE INFORMATION

When Lotus 1-2-3 was first released, the program assumed that the year 1900 was a leap year, even though it actually was not a leap year. This made it easier for the program to handle leap years and caused no harm to almost all date calculations in Lotus 1-2-3.

When Microsoft Multiplan and Microsoft Excel were released, they also assumed that 1900 was a leap year. This assumption allowed Microsoft Multiplan and Microsoft Excel to use the same serial date system used by Lotus 1-2-3 and provide greater compatibility with Lotus 1-2-3. Treating 1900 as a leap year also made it easier for users to move worksheets from one program to the other.

Although it is technically possible to correct this behavior so that current versions of Microsoft Excel do not assume that 1900 is a leap year, the disadvantages of doing so outweigh the advantages.

If this behavior were to be corrected, many problems would arise, including the following:

  • Almost all dates in current Microsoft Excel worksheets and other documents would be decreased by one day. Correcting this shift would take considerable time and effort, especially in formulas that use dates.
  • Some functions, such as the WEEKDAY function, would return different values; this might cause formulas in worksheets to work incorrectly.
  • Correcting this behavior would break serial date compatibility between Microsoft Excel and other programs that use dates.

If the behavior remains uncorrected, only one problem occurs:

  • The WEEKDAY function returns incorrect values for dates before March 1, 1900. Because most users do not use dates before March 1, 1900, this problem is rare.
    
por 30.09.2012 / 16:50
12

Aqui está o motivo explicado pelo próprio Joel: Minha primeira revisão do BillG

Basic uses December 31, 1899 as the epoch instead of January 1, 1900, but for some reason, today's date was the same in Excel as it was in Basic.

Huh?

I went to find an Excel developer who was old enough to remember why. Ed Fries seemed to know the answer.

"Oh," he told me. "Check out February 28th, 1900."

"It's 59," I said.

"Now try March 1st."

"It's 61!"

"What happened to 60?" Ed asked.

"February 29th. 1900 was a leap year! It's divisible by 4!"

"Good guess, but no cigar," Ed said, and left me wondering for a while.

Oops. I did some research. Years that are divisible by 100 are not leap years, unless they're also divisible by 400.

1900 wasn't a leap year.

"It's a bug in Excel!" I exclaimed.

"Well, not really," said Ed. "We had to do it that way because we need to be able to import Lotus 123 worksheets."

"So, it's a bug in Lotus 123?"

"Yeah, but probably an intentional one. Lotus had to fit in 640K. That's not a lot of memory. If you ignore 1900, you can figure out if a given year is a leap year just by looking to see if the rightmost two bits are zero. That's really fast and easy. The Lotus guys probably figured it didn't matter to be wrong for those two months way in the past.

    
por 01.10.2012 / 20:13