Converts the SQL Date to binary representation for {@link Oid#DATE}.

@param tz The timezone used.
@param bytes The binary encoded date value.
@param value value
@throws PSQLException If binary format could not be parsed.
15693
public void toBinDate(TimeZone tz, byte[] bytes, Date value) throws PSQLException {
    long millis = value.getTime();

    if (tz == null) {
      tz = getDefaultTz();
    }
    // It "getOffset" is UNTESTED
    // See org.postgresql.jdbc.AbstractJdbc2Statement.setDate(int, java.sql.Date,
    // java.util.Calendar)
    // The problem is we typically do not know for sure what is the exact required date/timestamp
    // type
    // Thus pgjdbc sticks to text transfer.
    millis += tz.getOffset(millis);

    long secs = toPgSecs(millis / 1000);
    ByteConverter.int4(bytes, 0, (int) (secs / 86400));
  }
447413
static public Date getDate(String str, TimeZone tz, SFSession session) throws SFException
  {
    try
    {
      long milliSecsSinceEpoch = Long.valueOf(str) * 86400000;

      SFTimestamp tsInUTC = SFTimestamp.fromDate(new Date(milliSecsSinceEpoch),
                                                 0, TimeZone.getTimeZone("UTC"));

      SFTimestamp tsInClientTZ = tsInUTC.moveToTimeZone(tz);

      if (logger.isDebugEnabled())
      {
        logger.debug("getDate: tz offset={}",
                     tsInClientTZ.getTimeZone().getOffset(tsInClientTZ.getTime()));
      }
      // return the date adjusted to the JVM default time zone
      Date preDate = new Date(tsInClientTZ.getTime());

      // if date is on or before 1582-10-04, apply the difference
      // by (H-H/4-2) where H is the hundreds digit of the year according to:
      // http://en.wikipedia.org/wiki/Gregorian_calendar
      Date newDate = adjustDate(preDate);
      if (logger.isDebugEnabled())
      {
        logger.debug("Adjust date from {} to {}",
                     preDate.toString(), newDate.toString());
      }
      return newDate;
    }
    catch (NumberFormatException ex)
    {
      throw (SFException) IncidentUtil.generateIncidentV2WithException(
          session,
          new SFException(ErrorCode.INTERNAL_ERROR,
                          "Invalid date value: " + str),
          null,
          null);
    }
  }
534138
@Override
    public byte[] getBytes(int columnIndex) throws SQLException {
        checkColumnBounds(columnIndex);
        try {
            if (table.getColumnType(columnIndex - 1) == VoltType.STRING)
                return table.getStringAsBytes(columnIndex - 1);
            else if (table.getColumnType(columnIndex - 1) == VoltType.VARBINARY)
                return table.getVarbinary(columnIndex - 1);
            else
                throw SQLError.get(SQLError.CONVERSION_NOT_FOUND,
                        table.getColumnType(columnIndex - 1), "byte[]");
        } catch (SQLException x) {
            throw x;
        } catch (Exception x) {
            throw SQLError.get(x);
        }
    }
817604
@SuppressWarnings("checkstyle:magicnumber")
	private void writeDBFDate(Date date) throws IOException {
		final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); //$NON-NLS-1$
		writeDBFString(format.format(date), 8, (byte) ' ');
	}
280232
def date_struct(year, month, day, tz = "UTC"):
    
    ymdtz = (year, month, day, tz)
    if None in ymdtz:
        #logger.debug("a year, month, day or tz value was empty: %s" % str(ymdtz))
        return None # return early if we have a bad value
    try:
        return time.strptime("%s-%s-%s %s" % ymdtz,  "%Y-%m-%d %Z")
    except(TypeError, ValueError):
        #logger.debug("date failed to convert: %s" % str(ymdtz))
        pass
287723
private void setValueFromObject(byte[] bytes) {
        if (type.equals(QualifiedName_XSD_BASE64_BINARY)) {
            setValue(ProvFactory.getFactory().base64Encoding(bytes));
        } else if (type.equals(QualifiedName_XSD_HEX_BINARY)) {
            setValue(ProvFactory.getFactory().hexEncoding(bytes));
        }
    }
1048743
@Override
    public void psSetBytes(PreparedStatement pstmtImpl, int i, byte[] x) throws SQLException {
        int length = (x == null ? 0 : x.length);
        if (tc.isDebugEnabled())
            Tr.debug(this, tc, "psSetBytes: " + length);

        if ((x != null) && (length > 2000)) {
            if (tc.isDebugEnabled())
                Tr.debug(this, tc, "ORACLE setBytes byte array length > 2000 workaround.");
            pstmtImpl.setBinaryStream(i, new ByteArrayInputStream(x), length);
        } else
            pstmtImpl.setBytes(i, x);
    }
924268
public OffsetTime getInternalOffsetTime(ColumnInformation columnInfo, TimeZone timeZone)
      throws SQLException {
    if (lastValueWasNull()) {
      return null;
    }
    if (length == 0) {
      lastValueNull |= BIT_LAST_FIELD_NULL;
      return null;
    }

    ZoneId zoneId = timeZone.toZoneId().normalized();
    if (zoneId instanceof ZoneOffset) {
      ZoneOffset zoneOffset = (ZoneOffset) zoneId;

      int day = 0;
      int hour = 0;
      int minutes = 0;
      int seconds = 0;
      int microseconds = 0;

      switch (columnInfo.getColumnType().getSqlType()) {
        case Types.TIMESTAMP:
          int year = ((buf[pos] & 0xff) | (buf[pos + 1] & 0xff) << 8);
          int month = buf[pos + 2];
          day = buf[pos + 3];

          if (length > 4) {
            hour = buf[pos + 4];
            minutes = buf[pos + 5];
            seconds = buf[pos + 6];

            if (length > 7) {
              microseconds = ((buf[pos + 7] & 0xff)
                  + ((buf[pos + 8] & 0xff) << 8)
                  + ((buf[pos + 9] & 0xff) << 16)
                  + ((buf[pos + 10] & 0xff) << 24));
            }
          }

          return ZonedDateTime
              .of(year, month, day, hour, minutes, seconds, microseconds * 1000, zoneOffset)
              .toOffsetDateTime().toOffsetTime();

        case Types.TIME:

          final boolean negate = (buf[pos] & 0xff) == 0x01;

          if (length > 4) {
            day = ((buf[pos + 1] & 0xff)
                + ((buf[pos + 2] & 0xff) << 8)
                + ((buf[pos + 3] & 0xff) << 16)
                + ((buf[pos + 4] & 0xff) << 24));
          }

          if (length > 7) {
            hour = buf[pos + 5];
            minutes = buf[pos + 6];
            seconds = buf[pos + 7];
          }

          if (length > 8) {
            microseconds = ((buf[pos + 8] & 0xff)
                + ((buf[pos + 9] & 0xff) << 8)
                + ((buf[pos + 10] & 0xff) << 16)
                + ((buf[pos + 11] & 0xff) << 24));
          }

          return OffsetTime
              .of((negate ? -1 : 1) * (day * 24 + hour), minutes, seconds, microseconds * 1000,
                  zoneOffset);

        case Types.VARCHAR:
        case Types.LONGVARCHAR:
        case Types.CHAR:
          String raw = new String(buf, pos, length, StandardCharsets.UTF_8);
          try {
            return OffsetTime.parse(raw, DateTimeFormatter.ISO_OFFSET_TIME);
          } catch (DateTimeParseException dateParserEx) {
            throw new SQLException(raw
                + " cannot be parse as OffsetTime (format is \"HH:mm:ss[.S]\" with offset for data type \""
                + columnInfo.getColumnType() + "\")");
          }

        default:
          throw new SQLException("Cannot read " + OffsetTime.class.getName() + " using a "
              + columnInfo.getColumnType().getJavaTypeName() + " field");
      }
    }

    if (options.useLegacyDatetimeCode) {
      //system timezone is not an offset
      throw new SQLException(
          "Cannot return an OffsetTime for a TIME field when default timezone is '" + zoneId
              + "' (only possible for time-zone offset from Greenwich/UTC, such as +02:00)");
    }

    //server timezone is not an offset
    throw new SQLException(
        "Cannot return an OffsetTime for a TIME field when server timezone '" + zoneId
            + "' (only possible for time-zone offset from Greenwich/UTC, such as +02:00)");

  }
430686
@Override
  public final Date fromIso8601FullNoTz(final String pDateStr,
    final Map<String, Object> pAddParam) throws Exception {
    return this.dateTimeFullNoTzFormatIso8601.parse(pDateStr);
  }
1322621
public static Date parseDate(String value, ParsePosition pos) {
        Long timestamp = parseTimestamp(value);
        if (timestamp != null) {
            return new Date(timestamp);
        }
        if (value.contains(" ")) {
            value = value.replace(" ", "+");
        }
        if (!(value.contains("-") || value.contains("+")) && !value.endsWith("Z")) {
            value += SYS_TZ;
        }
        try {
            return ISO8601Utils.parse(value, pos);
        } catch (ParseException e) {
            throw new ExtractorException(e);
        }
    }
785834
public void setObjectForStatement(PreparedStatement ps, int index, Object value, int sqlType) throws SQLException
    {
        switch (sqlType)
        {
            case Types.BIT :
                ps.setObject(index, value);
                break;

            case Types.BLOB :
            case Types.LONGVARBINARY :
            case Types.VARBINARY :
                if (value instanceof byte[])
                {
                    byte buf[] = (byte[]) value;
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
                    ps.setBinaryStream(index, inputStream, buf.length);

                    break;
                }

            case Types.CLOB :
                Reader reader = null;
                int length = 0;

                if (value instanceof String)
                {
                    reader = new StringReader((String) value);
                    length = (((String) value)).length();
                }
                else if (value instanceof char[])
                {
                    String string = new String((char[])value);
                    reader = new StringReader(string);
                    length = string.length();
                }
                else if (value instanceof byte[])
                {
                    byte buf[] = (byte[]) value;
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
                    reader = new InputStreamReader(inputStream);
                }
224118
@Override
  public final String toIso8601DateNoTz(final Date pDate,
    final Map<String, Object> pAddParam) throws Exception {
    return this.dateNoTzFormatIso8601.format(pDate);
  }
295337
@SuppressWarnings("unchecked")
    public static <T extends BaseBo> T fromBytes(byte[] bytes, Class<T> clazz,
            ClassLoader classLoader) {
        if (bytes == null || clazz == null) {
            return null;
        }
        try {
            Map<String, Object> data = SerializationUtils.fromByteArray(bytes, Map.class);
            String boClassName = DPathUtils.getValue(data, FIELD_CLASSNAME, String.class);
            T bo = createObject(boClassName, classLoader, clazz);
            if (bo != null) {
                bo.fromByteArray(DPathUtils.getValue(data, FIELD_BODATA, byte[].class));
                return bo;
            } else {
                return null;
            }
        } catch (Exception e) {
            throw e instanceof DeserializationException ? (DeserializationException) e
                    : new DeserializationException(e);
        }
    }
1058232
@Override
    public void setByte(int parameterIndex, byte x) throws SQLException
    {
        checkParameterBounds(parameterIndex);
        this.parameters[parameterIndex-1] = x;
    }
695750
public Timestamp getInternalTimestamp(ColumnInformation columnInfo, Calendar userCalendar,
      TimeZone timeZone) throws SQLException {
    if (lastValueWasNull()) {
      return null;
    }
    if (length == 0) {
      lastValueNull |= BIT_LAST_FIELD_NULL;
      return null;
    }

    int year = 1970;
    int month = 0;
    int day = 0;
    int hour = 0;
    int minutes = 0;
    int seconds = 0;
    int microseconds = 0;

    switch (columnInfo.getColumnType()) {
      case TIME:
        Calendar calendar = userCalendar != null ? userCalendar : Calendar.getInstance();

        boolean negate = false;
        if (length > 0) {
          negate = (buf[pos] & 0xff) == 0x01;
        }
        if (length > 4) {
          day = ((buf[pos + 1] & 0xff)
              + ((buf[pos + 2] & 0xff) << 8)
              + ((buf[pos + 3] & 0xff) << 16)
              + ((buf[pos + 4] & 0xff) << 24));
        }
        if (length > 7) {
          hour = buf[pos + 5];
          minutes = buf[pos + 6];
          seconds = buf[pos + 7];
        }

        if (length > 8) {
          microseconds = ((buf[pos + 8] & 0xff)
              + ((buf[pos + 9] & 0xff) << 8)
              + ((buf[pos + 10] & 0xff) << 16)
              + ((buf[pos + 11] & 0xff) << 24));
        }
        year = 1970;
        month = 1;
        day = ((negate ? -1 : 1) * day) + 1;
        hour = (negate ? -1 : 1) * hour;
        break;

      case STRING:
      case VARSTRING:
        String rawValue = new String(buf, pos, length, StandardCharsets.UTF_8);
        if (rawValue.startsWith("0000-00-00 00:00:00")) {
          lastValueNull |= BIT_LAST_ZERO_DATE;
          return null;
        }

        if (rawValue.length() >= 4) {
          year = Integer.parseInt(rawValue.substring(0, 4));
          if (rawValue.length() >= 7) {
            month = Integer.parseInt(rawValue.substring(5, 7));
            if (rawValue.length() >= 10) {
              day = Integer.parseInt(rawValue.substring(8, 10));
              if (rawValue.length() >= 19) {
                hour = Integer.parseInt(rawValue.substring(11, 13));
                minutes = Integer.parseInt(rawValue.substring(14, 16));
                seconds = Integer.parseInt(rawValue.substring(17, 19));
              }
              microseconds = extractNanos(rawValue) / 1000000;
            }
          }
        }
        break;

      default:
        year = ((buf[pos] & 0xff) | (buf[pos + 1] & 0xff) << 8);
        month = buf[pos + 2];
        day = buf[pos + 3];
        if (length > 4) {
          hour = buf[pos + 4];
          minutes = buf[pos + 5];
          seconds = buf[pos + 6];

          if (length > 7) {
            microseconds = ((buf[pos + 7] & 0xff)
                + ((buf[pos + 8] & 0xff) << 8)
                + ((buf[pos + 9] & 0xff) << 16)
                + ((buf[pos + 10] & 0xff) << 24));
          }
        }
    }

    Calendar calendar;
    if (userCalendar != null) {
      calendar = userCalendar;
    } else if (columnInfo.getColumnType().getSqlType() == Types.TIMESTAMP) {
      calendar = Calendar.getInstance(timeZone);
    } else {
      calendar = Calendar.getInstance();
    }

    Timestamp tt;
    synchronized (calendar) {
      calendar.clear();
      calendar.set(year, month - 1, day, hour, minutes, seconds);
      tt = new Timestamp(calendar.getTimeInMillis());
    }
    tt.setNanos(microseconds * 1000);
    return tt;
  }
1283319
public static Date parseBasicTimestamp(String value) throws ParseException
   {
      Date result = null;

      if (value.length() > 0)
      {
         if (!value.equals("-1 -1") && !value.equals("0"))
         {
            DateFormat df;
            if (value.endsWith(" 0"))
            {
               df = DATE_FORMAT1.get();
            }
            else
            {
               if (value.indexOf(' ') == -1)
               {
                  df = DATE_FORMAT2.get();
               }
               else
               {
                  df = TIMESTAMP_FORMAT.get();
                  int timeIndex = value.indexOf(' ') + 1;
                  if (timeIndex + 6 > value.length())
                  {
                     String time = value.substring(timeIndex);
                     value = value.substring(0, timeIndex) + "0" + time;
                  }
               }
            }

            result = df.parse(value);
         }
      }

      //System.out.println(value + "=>" + result);
      return result;
   }
577196
public static String isoDateTime(final Date val, final TimeZone tz) {
    synchronized (isoDateTimeTZFormat) {
      isoDateTimeTZFormat.setTimeZone(tz);
      return isoDateTimeTZFormat.format(val);
    }
  }