mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Reject year zero during datetime input, except when it's a 2-digit year
(then it means 2000 AD). Formerly we silently interpreted this as 1 BC, which at best is unwarranted familiarity with the implementation. It's barely possible that some app somewhere expects the old behavior, though, so we won't back-patch this into existing release branches.
This commit is contained in:
		@@ -8,7 +8,7 @@
 | 
				
			|||||||
 *
 | 
					 *
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * IDENTIFICATION
 | 
					 * IDENTIFICATION
 | 
				
			||||||
 *	  $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.186 2008/02/25 23:21:01 tgl Exp $
 | 
					 *	  $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.187 2008/02/25 23:36:28 tgl Exp $
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 *-------------------------------------------------------------------------
 | 
					 *-------------------------------------------------------------------------
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@@ -2138,24 +2138,30 @@ ValidateDate(int fmask, bool is2digits, bool bc, struct pg_tm * tm)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	if (fmask & DTK_M(YEAR))
 | 
						if (fmask & DTK_M(YEAR))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		/* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
 | 
					 | 
				
			||||||
		if (bc)
 | 
							if (bc)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if (tm->tm_year > 0)
 | 
								/* there is no year zero in AD/BC notation */
 | 
				
			||||||
 | 
								if (tm->tm_year <= 0)
 | 
				
			||||||
 | 
									return DTERR_FIELD_OVERFLOW;
 | 
				
			||||||
 | 
								/* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
 | 
				
			||||||
			tm->tm_year = -(tm->tm_year - 1);
 | 
								tm->tm_year = -(tm->tm_year - 1);
 | 
				
			||||||
			else
 | 
					 | 
				
			||||||
				ereport(ERROR,
 | 
					 | 
				
			||||||
						(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
 | 
					 | 
				
			||||||
						 errmsg("inconsistent use of year %04d and \"BC\"",
 | 
					 | 
				
			||||||
								tm->tm_year)));
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else if (is2digits)
 | 
							else if (is2digits)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
 | 
								/* allow 2-digit input for 1970-2069 AD; 00 is allowed */
 | 
				
			||||||
 | 
								if (tm->tm_year < 0)				/* just paranoia */
 | 
				
			||||||
 | 
									return DTERR_FIELD_OVERFLOW;
 | 
				
			||||||
			if (tm->tm_year < 70)
 | 
								if (tm->tm_year < 70)
 | 
				
			||||||
				tm->tm_year += 2000;
 | 
									tm->tm_year += 2000;
 | 
				
			||||||
			else if (tm->tm_year < 100)
 | 
								else if (tm->tm_year < 100)
 | 
				
			||||||
				tm->tm_year += 1900;
 | 
									tm->tm_year += 1900;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							else
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								/* there is no year zero in AD/BC notation */
 | 
				
			||||||
 | 
								if (tm->tm_year <= 0)
 | 
				
			||||||
 | 
									return DTERR_FIELD_OVERFLOW;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* now that we have correct year, decode DOY */
 | 
						/* now that we have correct year, decode DOY */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user