/** * Parse one of Pepco's times. Normally, this would be as easy as just calling * pepcoDateFormatter.parseDateTime, but there's some special case stuff that needs to happen on * the first scrape of a new year. Since Pepco doesn't include the year, on the first day of the * year, we might accidentally assume we are scraping data from January. So we have to do some * stuff to adjust for that. See the comment inside this method for what exactly we do. * * @param date The date to parse. * @param now What time it is right now. */ public static DateTime parsePepcoDateTime(final String date, final DateTime now) { final DateTime ret = PEPCO_DATE_FORMATTER.parseDateTime(date); /* * Check whether it is January and we are parsing a date in December. * * Pepco's dates don't include the year. So at the turn of the year (Jan * 1), we'll probably read something like "Dec 31 12:58 PM". It is very * unlikely that this actually represents December 31 of this year. More * likely, this is December 31 of last year (either that or some poor * group of customers is going to have to wait more than a year for * their power to get fixed). */ if (now.getMonthOfYear() == 1 && now.getDayOfMonth() == 1 && ret.getMonthOfYear() == 12 && ret.getDayOfMonth() == 31) { return ret.withYear(now.getYear() - 1); } else { return ret.withYear(now.getYear()); } }
protected static RubyTime s_mload(IRubyObject recv, RubyTime time, IRubyObject from) { Ruby runtime = recv.getRuntime(); DateTime dt = new DateTime(DateTimeZone.UTC); byte[] fromAsBytes = null; fromAsBytes = from.convertToString().getBytes(); if (fromAsBytes.length != 8) { throw runtime.newTypeError("marshaled time format differ"); } int p = 0; int s = 0; for (int i = 0; i < 4; i++) { p |= ((int) fromAsBytes[i] & 0xFF) << (8 * i); } for (int i = 4; i < 8; i++) { s |= ((int) fromAsBytes[i] & 0xFF) << (8 * (i - 4)); } boolean utc = false; if ((p & (1 << 31)) == 0) { dt = dt.withMillis(p * 1000L); time.setUSec((s & 0xFFFFF) % 1000); } else { p &= ~(1 << 31); utc = ((p >>> 30 & 0x1) == 0x1); dt = dt.withYear(((p >>> 14) & 0xFFFF) + 1900); dt = dt.withMonthOfYear(((p >>> 10) & 0xF) + 1); dt = dt.withDayOfMonth(((p >>> 5) & 0x1F)); dt = dt.withHourOfDay((p & 0x1F)); dt = dt.withMinuteOfHour(((s >>> 26) & 0x3F)); dt = dt.withSecondOfMinute(((s >>> 20) & 0x3F)); // marsaling dumps usec, not msec dt = dt.withMillisOfSecond((s & 0xFFFFF) / 1000); time.setUSec((s & 0xFFFFF) % 1000); } time.setDateTime(dt); if (!utc) time.localtime(); from.getInstanceVariables().copyInstanceVariablesInto(time); return time; }
@Test public void testDateFieldTypes() throws Exception { DateTime dt = DateTime.now().minusDays(2).withMillisOfSecond(0); Util.insertRows(testEnv, 10, 1, dt); VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0); Query allRowsQuery = new QueryBuilder("select * from vtgate_test", testEnv.keyspace, "master") .setKeyspaceIds(testEnv.getAllKeyspaceIds()) .build(); Row row = vtgate.execute(allRowsQuery).next(); Assert.assertTrue(dt.equals(row.getDateTime("timestamp_col"))); Assert.assertTrue(dt.equals(row.getDateTime("datetime_col"))); Assert.assertTrue( dt.withHourOfDay(0) .withMinuteOfHour(0) .withSecondOfMinute(0) .equals(row.getDateTime("date_col"))); Assert.assertTrue( dt.withYear(1970).withMonthOfYear(1).withDayOfMonth(1).equals(row.getDateTime("time_col"))); vtgate.close(); }