Java Code Examples for org.apache.calcite.avatica.util.DateTimeUtils#ymdToUnixDate()

The following examples show how to use org.apache.calcite.avatica.util.DateTimeUtils#ymdToUnixDate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimestampString.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns the number of milliseconds since the epoch. */
public long getMillisSinceEpoch() {
  final int year = Integer.valueOf(v.substring(0, 4));
  final int month = Integer.valueOf(v.substring(5, 7));
  final int day = Integer.valueOf(v.substring(8, 10));
  final int h = Integer.valueOf(v.substring(11, 13));
  final int m = Integer.valueOf(v.substring(14, 16));
  final int s = Integer.valueOf(v.substring(17, 19));
  final int ms = getMillisInSecond();
  final int d = DateTimeUtils.ymdToUnixDate(year, month, day);
  return d * DateTimeUtils.MILLIS_PER_DAY
      + h * DateTimeUtils.MILLIS_PER_HOUR
      + m * DateTimeUtils.MILLIS_PER_MINUTE
      + s * DateTimeUtils.MILLIS_PER_SECOND
      + ms;
}
 
Example 2
Source File: SqlFunctions.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Adds a given number of months to a date, represented as the number of
 * days since the epoch. */
public static int addMonths(int date, int m) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  m0 += m;
  int deltaYear = (int) DateTimeUtils.floorDiv(m0, 12);
  y0 += deltaYear;
  m0 = (int) DateTimeUtils.floorMod(m0, 12);
  if (m0 == 0) {
    y0 -= 1;
    m0 += 12;
  }

  int last = lastDay(y0, m0);
  if (d0 > last) {
    d0 = last;
  }
  return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 3
Source File: TimestampString.java    From Quicksql with MIT License 6 votes vote down vote up
/** Returns the number of milliseconds since the epoch. */
public long getMillisSinceEpoch() {
  final int year = Integer.valueOf(v.substring(0, 4));
  final int month = Integer.valueOf(v.substring(5, 7));
  final int day = Integer.valueOf(v.substring(8, 10));
  final int h = Integer.valueOf(v.substring(11, 13));
  final int m = Integer.valueOf(v.substring(14, 16));
  final int s = Integer.valueOf(v.substring(17, 19));
  final int ms = getMillisInSecond();
  final int d = DateTimeUtils.ymdToUnixDate(year, month, day);
  return d * DateTimeUtils.MILLIS_PER_DAY
      + h * DateTimeUtils.MILLIS_PER_HOUR
      + m * DateTimeUtils.MILLIS_PER_MINUTE
      + s * DateTimeUtils.MILLIS_PER_SECOND
      + ms;
}
 
Example 4
Source File: SqlFunctionUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
public static int addMonths(int date, int m) {
	int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
	int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
	int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
	m0 += m;
	int deltaYear = (int) DateTimeUtils.floorDiv(m0, 12);
	y0 += deltaYear;
	m0 = (int) DateTimeUtils.floorMod(m0, 12);
	if (m0 == 0) {
		y0 -= 1;
		m0 += 12;
	}
	int last = lastDay(y0, m0);
	if (d0 > last) {
		d0 = last;
	}
	return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 5
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the epoch days since 1970-01-01.
 */
public static int strToDate(String dateStr, String fromFormat) {
	// It is OK to use UTC, we just want get the epoch days
	// TODO  use offset, better performance
	long ts = parseToTimeMillis(dateStr, fromFormat, TimeZone.getTimeZone("UTC"));
	ZoneId zoneId = ZoneId.of("UTC");
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	return DateTimeUtils.ymdToUnixDate(zdt.getYear(), zdt.getMonthValue(), zdt.getDayOfMonth());
}
 
Example 6
Source File: DateString.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the number of days since the epoch. */
public int getDaysSinceEpoch() {
  int year = getYear();
  int month = getMonth();
  int day = getDay();
  return DateTimeUtils.ymdToUnixDate(year, month, day);
}
 
Example 7
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param timestamp milliseconds from epoch
 * @return milliseconds of the last day of the month since epoch
 */
public static int lastDay(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 8
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param date days since epoch
 * @return days of the last day of the month since epoch
 */
public static int lastDay(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 9
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static long julianDateFloor(TimeUnitRange range, int julian, boolean floor) {
	// Algorithm the book "Astronomical Algorithms" by Jean Meeus, 1998
	int b = 0;
	int c = 0;
	if (julian > 2299160) {
		int a = julian + 32044;
		b = (4 * a + 3) / 146097;
		c = a - b * 146097 / 4;
	}
	else {
		b = 0;
		c = julian + 32082;
	}
	int d = (4 * c + 3) / 1461;
	int e = c - (1461 * d) / 4;
	int m = (5 * e + 2) / 153;
	int day = e - (153 * m + 2) / 5 + 1;
	int month = m + 3 - 12 * (m / 10);
	int quarter = (month + 2) / 3;
	int year = b * 100 + d - 4800 + (m / 10);
	switch (range) {
		case YEAR:
			if (!floor && (month > 1 || day > 1)) {
				year += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, 1, 1);
		case MONTH:
			if (!floor && day > 1) {
				month += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, month, 1);
		case QUARTER:
			if (!floor && (month > 1 || day > 1)) {
				quarter += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, quarter * 3 - 2, 1);
		default:
			throw new AssertionError(range);
	}
}
 
Example 10
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the epoch days since 1970-01-01.
 */
public static int strToDate(String dateStr, String fromFormat) {
	// It is OK to use UTC, we just want get the epoch days
	// TODO  use offset, better performance
	long ts = parseToTimeMillis(dateStr, fromFormat, TimeZone.getTimeZone("UTC"));
	ZoneId zoneId = ZoneId.of("UTC");
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	return DateTimeUtils.ymdToUnixDate(zdt.getYear(), zdt.getMonthValue(), zdt.getDayOfMonth());
}
 
Example 11
Source File: DateString.java    From Quicksql with MIT License 5 votes vote down vote up
/** Returns the number of days since the epoch. */
public int getDaysSinceEpoch() {
  int year = getYear();
  int month = getMonth();
  int day = getDay();
  return DateTimeUtils.ymdToUnixDate(year, month, day);
}
 
Example 12
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** Adds a given number of months to a date, represented as the number of
 * days since the epoch. */
public static int addMonths(int date, int m) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
  int y = m / 12;
  y0 += y;
  m0 += m - y * 12;
  int last = lastDay(y0, m0);
  if (d0 > last) {
    d0 = last;
  }
  return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
}
 
Example 13
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param timestamp milliseconds from epoch
 * @return milliseconds of the last day of the month since epoch
 */
public static int lastDay(long timestamp) {
  int date = (int) (timestamp / DateTimeUtils.MILLIS_PER_DAY);
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 14
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * SQL {@code LAST_DAY} function.
 *
 * @param date days since epoch
 * @return days of the last day of the month since epoch
 */
public static int lastDay(int date) {
  int y0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.YEAR, date);
  int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
  int last = lastDay(y0, m0);
  return DateTimeUtils.ymdToUnixDate(y0, m0, last);
}
 
Example 15
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static long julianDateFloor(TimeUnitRange range, int julian, boolean floor) {
	// Algorithm the book "Astronomical Algorithms" by Jean Meeus, 1998
	int b = 0;
	int c = 0;
	if (julian > 2299160) {
		int a = julian + 32044;
		b = (4 * a + 3) / 146097;
		c = a - b * 146097 / 4;
	}
	else {
		b = 0;
		c = julian + 32082;
	}
	int d = (4 * c + 3) / 1461;
	int e = c - (1461 * d) / 4;
	int m = (5 * e + 2) / 153;
	int day = e - (153 * m + 2) / 5 + 1;
	int month = m + 3 - 12 * (m / 10);
	int quarter = (month + 2) / 3;
	int year = b * 100 + d - 4800 + (m / 10);
	switch (range) {
		case YEAR:
			if (!floor && (month > 1 || day > 1)) {
				year += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, 1, 1);
		case MONTH:
			if (!floor && day > 1) {
				month += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, month, 1);
		case QUARTER:
			if (!floor && (month > 1 || day > 1)) {
				quarter += 1;
			}
			return DateTimeUtils.ymdToUnixDate(year, quarter * 3 - 2, 1);
		default:
			throw new AssertionError(range);
	}
}
 
Example 16
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static Integer dateStringToUnixDate(String s) {
	// allow timestamp str to date, e.g. 2017-12-12 09:30:00.0
	int ws1 = s.indexOf(" ");
	if (ws1 > 0) {
		s = s.substring(0, ws1);
	}
	int hyphen1 = s.indexOf('-');
	int y;
	int m;
	int d;
	if (hyphen1 < 0) {
		if (!isInteger(s.trim())) {
			return null;
		}
		y = Integer.parseInt(s.trim());
		m = 1;
		d = 1;
	} else {
		if (!isInteger(s.substring(0, hyphen1).trim())) {
			return null;
		}
		y = Integer.parseInt(s.substring(0, hyphen1).trim());
		final int hyphen2 = s.indexOf('-', hyphen1 + 1);
		if (hyphen2 < 0) {
			if (!isInteger(s.substring(hyphen1 + 1).trim())) {
				return null;
			}
			m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
			d = 1;
		} else {
			if (!isInteger(s.substring(hyphen1 + 1, hyphen2).trim())) {
				return null;
			}
			m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
			if (!isInteger(s.substring(hyphen2 + 1).trim())) {
				return null;
			}
			d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
		}
	}
	if (!isIllegalDate(y, m, d)) {
		return null;
	}
	return DateTimeUtils.ymdToUnixDate(y, m, d);
}
 
Example 17
Source File: SqlDateTimeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static Integer dateStringToUnixDate(String s) {
	// allow timestamp str to date, e.g. 2017-12-12 09:30:00.0
	int ws1 = s.indexOf(" ");
	if (ws1 > 0) {
		s = s.substring(0, ws1);
	}
	int hyphen1 = s.indexOf('-');
	int y;
	int m;
	int d;
	if (hyphen1 < 0) {
		if (!isInteger(s.trim())) {
			return null;
		}
		y = Integer.parseInt(s.trim());
		m = 1;
		d = 1;
	} else {
		if (!isInteger(s.substring(0, hyphen1).trim())) {
			return null;
		}
		y = Integer.parseInt(s.substring(0, hyphen1).trim());
		final int hyphen2 = s.indexOf('-', hyphen1 + 1);
		if (hyphen2 < 0) {
			if (!isInteger(s.substring(hyphen1 + 1).trim())) {
				return null;
			}
			m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
			d = 1;
		} else {
			if (!isInteger(s.substring(hyphen1 + 1, hyphen2).trim())) {
				return null;
			}
			m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
			if (!isInteger(s.substring(hyphen2 + 1).trim())) {
				return null;
			}
			d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
		}
	}
	if (!isIllegalDate(y, m, d)) {
		return null;
	}
	return DateTimeUtils.ymdToUnixDate(y, m, d);
}