List of usage examples for org.joda.time DateTime getYear
public int getYear()
From source file:org.jruby.ext.date.TimeExt.java
License:LGPL
@JRubyMethod(name = "to_date") public static RubyDate to_date(ThreadContext context, IRubyObject self) { final DateTime dt = ((RubyTime) self).getDateTime(); long jd = civil_to_jd(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), GREGORIAN); return new RubyDate(context, getDate(context.runtime), jd_to_ajd(context, jd), CHRONO_ITALY_UTC, 0); }
From source file:org.jruby.ext.date.TimeExt.java
License:LGPL
@JRubyMethod(name = "to_datetime") public static RubyDateTime to_datetime(ThreadContext context, IRubyObject self) { final RubyTime time = (RubyTime) self; DateTime dt = ((RubyTime) self).getDateTime(); long subMillisNum = 0, subMillisDen = 1; if (time.getNSec() != 0) { IRubyObject subMillis = RubyRational.newRationalCanonicalize(context, time.getNSec(), 1_000_000); if (subMillis instanceof RubyRational) { subMillisNum = ((RubyRational) subMillis).getNumerator().getLongValue(); subMillisDen = ((RubyRational) subMillis).getDenominator().getLongValue(); } else {/* w w w .ja va 2 s . com*/ subMillisNum = ((RubyInteger) subMillis).getLongValue(); } } final int off = dt.getZone().getOffset(dt.getMillis()) / 1000; int year = dt.getYear(); if (year <= 0) year--; // JODA's Julian chronology (no year 0) if (year == 1582) { // take the "slow" path - JODA isn't adjusting for missing (reform) dates return calcAjdFromCivil(context, dt, off, subMillisNum, subMillisDen); } dt = new DateTime(year, dt.getMonthOfYear(), dt.getDayOfMonth(), dt.getHourOfDay(), dt.getMinuteOfHour(), dt.getSecondOfMinute(), dt.getMillisOfSecond(), getChronology(context, ITALY, dt.getZone())); return new RubyDateTime(context.runtime, getDateTime(context.runtime), dt, off, ITALY, subMillisNum, subMillisDen); }
From source file:org.jruby.ext.date.TimeExt.java
License:LGPL
private static RubyDateTime calcAjdFromCivil(ThreadContext context, final DateTime dt, final int off, final long subMillisNum, final long subMillisDen) { final Ruby runtime = context.runtime; long jd = civil_to_jd(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), ITALY); RubyNumeric fr = timeToDayFraction(context, dt.getHourOfDay(), dt.getMinuteOfHour(), dt.getSecondOfMinute());//from w w w .j a v a 2s . c om final RubyNumeric ajd = jd_to_ajd(context, jd, fr, off); RubyDateTime dateTime = new RubyDateTime(context, getDateTime(runtime), ajd, off, ITALY); dateTime.dt = dateTime.dt.withMillisOfSecond(dt.getMillisOfSecond()); dateTime.subMillisNum = subMillisNum; dateTime.subMillisDen = subMillisDen; return dateTime; }
From source file:org.jruby.ext.jvyaml.JRubyConstructor.java
public static Object constructYamlTimestampYMD(final Constructor ctor, final Node node) { DateTime dt = (DateTime) (((Object[]) SafeConstructorImpl.constructYamlTimestamp(ctor, node))[0]); Ruby runtime = ((JRubyConstructor) ctor).runtime; return RuntimeHelpers.invoke(runtime.getCurrentContext(), runtime.fastGetClass("Date"), "new", runtime.newFixnum(dt.getYear()), runtime.newFixnum(dt.getMonthOfYear()), runtime.newFixnum(dt.getDayOfMonth())); }
From source file:org.jruby.CompatVersion.java
License:LGPL
public RubyObject mdump(final IRubyObject[] args) { RubyTime obj = (RubyTime) args[0]; DateTime dateTime = obj.dt.withZone(DateTimeZone.UTC); byte dumpValue[] = new byte[8]; int pe = 0x1 << 31 | (dateTime.getYear() - 1900) << 14 | (dateTime.getMonthOfYear() - 1) << 10 | dateTime.getDayOfMonth() << 5 | dateTime.getHourOfDay(); int se = dateTime.getMinuteOfHour() << 26 | dateTime.getSecondOfMinute() << 20 | (dateTime.getMillisOfSecond() * 1000 + (int) usec); // dump usec, not msec for (int i = 0; i < 4; i++) { dumpValue[i] = (byte) (pe & 0xFF); pe >>>= 8;/*from w w w . ja v a2s. co m*/ } for (int i = 4; i < 8; i++) { dumpValue[i] = (byte) (se & 0xFF); se >>>= 8; } return RubyString.newString(obj.getRuntime(), new ByteList(dumpValue, false)); }
From source file:org.jruby.RubyTime.java
License:LGPL
public RubyObject mdump() { Ruby runtime = getRuntime();/*from w w w. j a v a2s .co m*/ RubyTime obj = this; DateTime dateTime = obj.dt.toDateTime(DateTimeZone.UTC); byte dumpValue[] = new byte[8]; long nanos = this.nsec; long usec = this.nsec / 1000; long nanosec = this.nsec % 1000; int pe = 0x1 << 31 | ((obj.gmt().isTrue()) ? 0x1 : 0x0) << 30 | (dateTime.getYear() - 1900) << 14 | (dateTime.getMonthOfYear() - 1) << 10 | dateTime.getDayOfMonth() << 5 | dateTime.getHourOfDay(); int se = dateTime.getMinuteOfHour() << 26 | dateTime.getSecondOfMinute() << 20 | (dateTime.getMillisOfSecond() * 1000 + (int) usec); // dump usec, not msec for (int i = 0; i < 4; i++) { dumpValue[i] = (byte) (pe & 0xFF); pe >>>= 8; } for (int i = 4; i < 8; i++) { dumpValue[i] = (byte) (se & 0xFF); se >>>= 8; } RubyString string = RubyString.newString(obj.getRuntime(), new ByteList(dumpValue)); // 1.9 includes more nsecs copyInstanceVariablesInto(string); // nanos in numerator/denominator form if (nanosec != 0) { string.setInternalVariable("nano_num", runtime.newFixnum(nanosec)); string.setInternalVariable("nano_den", runtime.newFixnum(1)); } // submicro for 1.9.1 compat byte[] submicro = new byte[2]; int len = 2; submicro[1] = (byte) ((nanosec % 10) << 4); nanosec /= 10; submicro[0] = (byte) (nanosec % 10); nanosec /= 10; submicro[0] |= (byte) ((nanosec % 10) << 4); if (submicro[1] == 0) len = 1; string.setInternalVariable("submicro", RubyString.newString(runtime, submicro, 0, len)); // time zone if (dt.getZone() != DateTimeZone.UTC) { long offset = dt.getZone().getOffset(dt.getMillis()); string.setInternalVariable("offset", runtime.newFixnum(offset / 1000)); String zone = dt.getZone().getShortName(dt.getMillis()); if (!TIME_OFFSET_PATTERN.matcher(zone).matches()) { string.setInternalVariable("zone", runtime.newString(zone)); } } return string; }
From source file:org.jruby.truffle.core.time.RubyDateFormatter.java
License:LGPL
public ByteList formatToByteList(List<Token> compiledPattern, DateTime dt, long nsec) { RubyTimeOutputFormatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; ByteList toAppendTo = new ByteList(); for (Token token : compiledPattern) { String output = null;//from w w w .ja va2s.c o m long value = 0; FieldType type = TEXT; Format format = token.getFormat(); switch (format) { case FORMAT_ENCODING: toAppendTo.setEncoding((Encoding) token.getData()); continue; // go to next token case FORMAT_OUTPUT: formatter = (RubyTimeOutputFormatter) token.getData(); continue; // go to next token case FORMAT_STRING: output = token.getData().toString(); break; case FORMAT_WEEK_LONG: // This is GROSS, but Java API's aren't ISO 8601 compliant at all int v = (dt.getDayOfWeek() + 1) % 8; if (v == 0) { v++; } output = FORMAT_SYMBOLS.getWeekdays()[v]; break; case FORMAT_WEEK_SHORT: // This is GROSS, but Java API's aren't ISO 8601 compliant at all v = (dt.getDayOfWeek() + 1) % 8; if (v == 0) { v++; } output = FORMAT_SYMBOLS.getShortWeekdays()[v]; break; case FORMAT_MONTH_LONG: output = FORMAT_SYMBOLS.getMonths()[dt.getMonthOfYear() - 1]; break; case FORMAT_MONTH_SHORT: output = FORMAT_SYMBOLS.getShortMonths()[dt.getMonthOfYear() - 1]; break; case FORMAT_DAY: type = NUMERIC2; value = dt.getDayOfMonth(); break; case FORMAT_DAY_S: type = NUMERIC2BLANK; value = dt.getDayOfMonth(); break; case FORMAT_HOUR: type = NUMERIC2; value = dt.getHourOfDay(); break; case FORMAT_HOUR_BLANK: type = NUMERIC2BLANK; value = dt.getHourOfDay(); break; case FORMAT_HOUR_M: case FORMAT_HOUR_S: value = dt.getHourOfDay(); if (value == 0) { value = 12; } else if (value > 12) { value -= 12; } type = (format == Format.FORMAT_HOUR_M) ? NUMERIC2 : NUMERIC2BLANK; break; case FORMAT_DAY_YEAR: type = NUMERIC3; value = dt.getDayOfYear(); break; case FORMAT_MINUTES: type = NUMERIC2; value = dt.getMinuteOfHour(); break; case FORMAT_MONTH: type = NUMERIC2; value = dt.getMonthOfYear(); break; case FORMAT_MERIDIAN: output = dt.getHourOfDay() < 12 ? "AM" : "PM"; break; case FORMAT_MERIDIAN_LOWER_CASE: output = dt.getHourOfDay() < 12 ? "am" : "pm"; break; case FORMAT_SECONDS: type = NUMERIC2; value = dt.getSecondOfMinute(); break; case FORMAT_WEEK_YEAR_M: type = NUMERIC2; value = formatWeekYear(dt, Calendar.MONDAY); break; case FORMAT_WEEK_YEAR_S: type = NUMERIC2; value = formatWeekYear(dt, Calendar.SUNDAY); break; case FORMAT_DAY_WEEK: type = NUMERIC; value = dt.getDayOfWeek() % 7; break; case FORMAT_DAY_WEEK2: type = NUMERIC; value = dt.getDayOfWeek(); break; case FORMAT_YEAR_LONG: value = year(dt, dt.getYear()); type = (value >= 0) ? NUMERIC4 : NUMERIC5; break; case FORMAT_YEAR_SHORT: type = NUMERIC2; value = year(dt, dt.getYear()) % 100; break; case FORMAT_COLON_ZONE_OFF: // custom logic because this is so weird value = dt.getZone().getOffset(dt.getMillis()) / 1000; int colons = (Integer) token.getData(); output = formatZone(colons, (int) value, formatter); break; case FORMAT_ZONE_ID: output = getRubyTimeZoneName(dt); break; case FORMAT_CENTURY: type = NUMERIC; value = year(dt, dt.getYear()) / 100; break; case FORMAT_EPOCH: type = NUMERIC; value = dt.getMillis() / 1000; break; case FORMAT_WEEK_WEEKYEAR: type = NUMERIC2; value = dt.getWeekOfWeekyear(); break; case FORMAT_MILLISEC: case FORMAT_NANOSEC: int defaultWidth = (format == Format.FORMAT_NANOSEC) ? 9 : 3; int width = formatter.getWidth(defaultWidth); output = RubyTimeOutputFormatter.formatNumber(dt.getMillisOfSecond(), 3, '0'); if (width > 3) { output += RubyTimeOutputFormatter.formatNumber(nsec, 6, '0'); } if (width < output.length()) { output = output.substring(0, width); } else { // Not enough precision, fill with 0 while (output.length() < width) output += "0"; } formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; // no more formatting break; case FORMAT_WEEKYEAR: value = year(dt, dt.getWeekyear()); type = (value >= 0) ? NUMERIC4 : NUMERIC5; break; case FORMAT_WEEKYEAR_SHORT: type = NUMERIC2; value = year(dt, dt.getWeekyear()) % 100; break; case FORMAT_MICROSEC_EPOCH: // only available for Date type = NUMERIC; value = dt.getMillis(); break; case FORMAT_SPECIAL: throw new Error("FORMAT_SPECIAL is a special token only for the lexer."); } try { output = formatter.format(output, value, type); } catch (IndexOutOfBoundsException ioobe) { throw new RaiseException( context.getCoreExceptions().errnoError(Errno.ERANGE.intValue(), "strftime", currentNode)); } // reset formatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; toAppendTo.append( output.getBytes(context.getEncodingManager().charsetForEncoding(toAppendTo.getEncoding()))); } return toAppendTo; }
From source file:org.jruby.util.RubyDateFormatter.java
License:LGPL
public ByteList formatToByteList(List<Token> compiledPattern, DateTime dt, long nsec, IRubyObject sub_millis) { RubyTimeOutputFormatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; ByteList toAppendTo = new ByteList(); for (Token token : compiledPattern) { String output = null;//from w ww. j av a 2s . c o m long value = 0; FieldType type = TEXT; Format format = token.getFormat(); switch (format) { case FORMAT_ENCODING: toAppendTo.setEncoding((Encoding) token.getData()); continue; // go to next token case FORMAT_OUTPUT: formatter = (RubyTimeOutputFormatter) token.getData(); continue; // go to next token case FORMAT_STRING: output = token.getData().toString(); break; case FORMAT_WEEK_LONG: // This is GROSS, but Java API's aren't ISO 8601 compliant at all int v = (dt.getDayOfWeek() + 1) % 8; if (v == 0) { v++; } output = FORMAT_SYMBOLS.getWeekdays()[v]; break; case FORMAT_WEEK_SHORT: // This is GROSS, but Java API's aren't ISO 8601 compliant at all v = (dt.getDayOfWeek() + 1) % 8; if (v == 0) { v++; } output = FORMAT_SYMBOLS.getShortWeekdays()[v]; break; case FORMAT_MONTH_LONG: output = FORMAT_SYMBOLS.getMonths()[dt.getMonthOfYear() - 1]; break; case FORMAT_MONTH_SHORT: output = FORMAT_SYMBOLS.getShortMonths()[dt.getMonthOfYear() - 1]; break; case FORMAT_DAY: type = NUMERIC2; value = dt.getDayOfMonth(); break; case FORMAT_DAY_S: type = NUMERIC2BLANK; value = dt.getDayOfMonth(); break; case FORMAT_HOUR: type = NUMERIC2; value = dt.getHourOfDay(); break; case FORMAT_HOUR_BLANK: type = NUMERIC2BLANK; value = dt.getHourOfDay(); break; case FORMAT_HOUR_M: case FORMAT_HOUR_S: value = dt.getHourOfDay(); if (value == 0) { value = 12; } else if (value > 12) { value -= 12; } type = (format == Format.FORMAT_HOUR_M) ? NUMERIC2 : NUMERIC2BLANK; break; case FORMAT_DAY_YEAR: type = NUMERIC3; value = dt.getDayOfYear(); break; case FORMAT_MINUTES: type = NUMERIC2; value = dt.getMinuteOfHour(); break; case FORMAT_MONTH: type = NUMERIC2; value = dt.getMonthOfYear(); break; case FORMAT_MERIDIAN: output = dt.getHourOfDay() < 12 ? "AM" : "PM"; break; case FORMAT_MERIDIAN_LOWER_CASE: output = dt.getHourOfDay() < 12 ? "am" : "pm"; break; case FORMAT_SECONDS: type = NUMERIC2; value = dt.getSecondOfMinute(); break; case FORMAT_WEEK_YEAR_M: type = NUMERIC2; value = formatWeekYear(dt, java.util.Calendar.MONDAY); break; case FORMAT_WEEK_YEAR_S: type = NUMERIC2; value = formatWeekYear(dt, java.util.Calendar.SUNDAY); break; case FORMAT_DAY_WEEK: type = NUMERIC; value = dt.getDayOfWeek() % 7; break; case FORMAT_DAY_WEEK2: type = NUMERIC; value = dt.getDayOfWeek(); break; case FORMAT_YEAR_LONG: value = year(dt, dt.getYear()); type = (value >= 0) ? NUMERIC4 : NUMERIC5; break; case FORMAT_YEAR_SHORT: type = NUMERIC2; value = year(dt, dt.getYear()) % 100; break; case FORMAT_COLON_ZONE_OFF: // custom logic because this is so weird value = dt.getZone().getOffset(dt.getMillis()) / 1000; int colons = (Integer) token.getData(); output = formatZone(colons, (int) value, formatter); break; case FORMAT_ZONE_ID: output = dt.getZone().getShortName(dt.getMillis()); break; case FORMAT_CENTURY: type = NUMERIC; value = year(dt, dt.getYear()) / 100; break; case FORMAT_EPOCH: type = NUMERIC; value = dt.getMillis() / 1000; break; case FORMAT_WEEK_WEEKYEAR: type = NUMERIC2; value = dt.getWeekOfWeekyear(); break; case FORMAT_MILLISEC: case FORMAT_NANOSEC: int defaultWidth = (format == Format.FORMAT_NANOSEC) ? 9 : 3; int width = formatter.getWidth(defaultWidth); output = RubyTimeOutputFormatter.formatNumber(dt.getMillisOfSecond(), 3, '0'); if (width > 3) { if (sub_millis == null || sub_millis.isNil()) { // Time output += RubyTimeOutputFormatter.formatNumber(nsec, 6, '0'); } else { // Date, DateTime int prec = width - 3; IRubyObject power = context.runtime.newFixnum(10).callMethod("**", context.runtime.newFixnum(prec)); IRubyObject truncated = sub_millis.callMethod(context, "numerator").callMethod(context, "*", power); truncated = truncated.callMethod(context, "/", sub_millis.callMethod(context, "denominator")); long decimals = truncated.convertToInteger().getLongValue(); output += RubyTimeOutputFormatter.formatNumber(decimals, prec, '0'); } } if (width < output.length()) { output = output.substring(0, width); } else { // Not enough precision, fill with 0 while (output.length() < width) output += "0"; } formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; // no more formatting break; case FORMAT_WEEKYEAR: value = year(dt, dt.getWeekyear()); type = (value >= 0) ? NUMERIC4 : NUMERIC5; break; case FORMAT_WEEKYEAR_SHORT: type = NUMERIC2; value = year(dt, dt.getWeekyear()) % 100; break; case FORMAT_MICROSEC_EPOCH: // only available for Date type = NUMERIC; value = dt.getMillis(); break; case FORMAT_SPECIAL: throw new Error("FORMAT_SPECIAL is a special token only for the lexer."); } output = formatter.format(output, value, type); // reset formatter formatter = RubyTimeOutputFormatter.DEFAULT_FORMATTER; toAppendTo.append(output .getBytes(context.runtime.getEncodingService().charsetForEncoding(toAppendTo.getEncoding()))); } return toAppendTo; }
From source file:org.jtotus.common.DayisHoliday.java
License:Open Source License
public static boolean isHoliday(DateTime date) { if (date == null || date.getDayOfWeek() == DateTimeConstants.SATURDAY || date.getDayOfWeek() == DateTimeConstants.SUNDAY) { return true; }/*from w ww.j av a 2 s . c o m*/ //int toSearch = date.get(Calendar.DATE)*1000000+(date.get(Calendar.MONTH)+1)*10000+date.get(Calendar.YEAR); int toSearch = date.getDayOfMonth() * 1000000 + date.getMonthOfYear() * 10000 + date.getYear(); //System.out.printf("To search:%d == %d:%d:%d\n", toSearch, date.get(Calendar.DATE), date.get(Calendar.MONTH)+1, date.get(Calendar.YEAR)); for (int day : days) { if (day == toSearch) { return true; } } return false; }
From source file:org.jtotus.database.LocalJDBC.java
License:Open Source License
public double[] fetchPeriod(String tableName, DateTime startDate, DateTime endDate, String type) { BigDecimal retValue = null;//from w ww .ja va2 s . c om PreparedStatement pstm = null; java.sql.Date retDate = null; ResultSet results = null; ArrayList<Double> closingPrices = new ArrayList<Double>(600); Connection connection = null; try { String query = "SELECT " + type + ", DATE FROM " + this.normTableName(tableName) + " WHERE DATE>=? AND DATE<=? ORDER BY DATE ASC"; // this.createTable(connection, this.normTableName(tableName)); connection = this.getConnection(); pstm = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); java.sql.Date startSqlDate = new java.sql.Date(startDate.getMillis()); pstm.setDate(1, startSqlDate); java.sql.Date endSqlDate = new java.sql.Date(endDate.getMillis()); pstm.setDate(2, endSqlDate); DateIterator dateIter = new DateIterator(startDate, endDate); results = pstm.executeQuery(); DateTime dateCheck; if (debug) { System.out.printf("start data %s end date: %s\n", startSqlDate.toString(), endSqlDate.toString()); } while (dateIter.hasNext()) { dateCheck = dateIter.nextInCalendar(); if (results.next()) { retValue = results.getBigDecimal(1); retDate = results.getDate(2); DateTime compCal = new DateTime(retDate.getTime()); if (compCal.getDayOfMonth() == dateCheck.getDayOfMonth() && compCal.getMonthOfYear() == dateCheck.getMonthOfYear() && compCal.getYear() == dateCheck.getYear()) { closingPrices.add(retValue.doubleValue()); continue; } else { results.previous(); } } BigDecimal failOverValue = getFetcher().fetchData(tableName, dateCheck, type); if (failOverValue != null) { closingPrices.add(failOverValue.doubleValue()); } } } catch (SQLException ex) { System.err.printf("LocalJDBC Unable to find date for:'%s' from'%s' Time" + startDate.toDate() + "\n", "Cosing Price", tableName); ex.printStackTrace(); SQLException xp = null; while ((xp = ex.getNextException()) != null) { xp.printStackTrace(); } } finally { try { if (results != null) results.close(); if (pstm != null) pstm.close(); if (connection != null) connection.close(); // System.out.printf("Max connect:%d in use:%d\n",mainPool.getMaxConnections(), mainPool.getActiveConnections()); // mainPool.dispose(); } catch (SQLException ex) { Logger.getLogger(LocalJDBC.class.getName()).log(Level.SEVERE, null, ex); } } return ArrayUtils.toPrimitive(closingPrices.toArray(new Double[0])); }