PeriodType getPeriodType() { return getField().p; }
private CalendarDateUnit(Calendar calt, CalendarPeriod.Field periodField, CalendarDate baseDate) { this.cal = calt; this.periodField = periodField; this.period = CalendarPeriod.of(1, periodField); this.baseDate = baseDate; if (periodField == CalendarPeriod.Field.Month || periodField == CalendarPeriod.Field.Year) { isCalendarField = true; } else { isCalendarField = false; } }
public static void main(String[] args) { CalendarPeriod cp = CalendarPeriod.of(1, Field.Day); CalendarDate start = CalendarDate.parseUdunits(null, "3 days since 1970-01-01 12:00"); CalendarDate end = CalendarDate.parseUdunits(null, "6 days since 1970-01-01 12:00"); int offset = cp.getOffset(start, end); System.out.printf("offset=%d%n", offset); } }
public CalendarPeriod getTimeUnit() { return CalendarPeriod.of(1, CalendarPeriod.fromUnitString(unitString)); }
public static void main(String[] args) { CalendarPeriod cp = new CalendarPeriod(1, Field.Day); CalendarDate start = CalendarDate.parseUdunits(null, "3 days since 1970-01-01 12:00"); CalendarDate end = CalendarDate.parseUdunits(null, "6 days since 1970-01-01 12:00"); int offset = cp.getOffset(start, end); System.out.printf("offset=%d%n", offset); } }
public String getTimeIntervalName() { if (!isInterval()) return null; // are they the same length ? int firstValue = -1; boolean same = true; for (Tinv tinv : intervals) { int value = (tinv.b2 - tinv.b1); if (firstValue < 0) firstValue = value; else if (value != firstValue) same = false; } if (same) { firstValue = (firstValue * calendarPeriod.getValue()); return firstValue + "_" + calendarPeriod.getField().toString(); } else { return CoordinateTimeAbstract.MIXED_INTERVALS; } }
private List<CollectionManager> makePartitionsByPeriod() throws IOException { this.type = Type.timePeriod; List<DatedMFile> files = new ArrayList<DatedMFile>(); for (MFile mfile : getFiles()) { CalendarDate cdate = dateExtractor.getCalendarDate(mfile); if (cdate == null) logger.error("Date extraction failed on file= {} dateExtractor = {}", mfile.getPath(), dateExtractor); files.add( new DatedMFile(mfile, cdate)); } Collections.sort(files); CalendarDateFormatter cdf = new CalendarDateFormatter("yyyyMMdd"); List<CollectionManager> result = new ArrayList<CollectionManager>(); TimePartitionCollectionManager curr = null; for (DatedMFile dmf : files) { if ((curr == null) || (!curr.endPartition.isAfter(dmf.cdate))) { CalendarPeriod period = CalendarPeriod.of(config.timePartition); CalendarDate start = dmf.cdate.truncate(period.getField()); // start on a boundary CalendarDate end = start.add( period); String name = collectionName + "-"+ cdf.toString(dmf.cdate); curr = new TimePartitionCollectionManager(name, start, end, getRoot(), this.auxInfo, this.logger); result.add(curr); } curr.add(dmf); } return result; }
public Tinv convertReferenceDate(CalendarDate refDate, CalendarPeriod timeUnit) { if (timeUnit == null) return null; int startOffset = timeUnit.getOffset(refDate, start); // LOOK wrong - not dealing with value ?? int endOffset = timeUnit.getOffset(refDate, end); return new TimeCoord.Tinv(startOffset, endOffset); }
public int getOffset(CalendarDate start, CalendarDate end) { if (start.equals(end)) return 0; long start_millis = start.getDateTime().getMillis(); long end_millis = end.getDateTime().getMillis(); // 5 second slop Period p; if (start_millis < end_millis) p = new Period(start_millis, end_millis + 5000, getPeriodType()); else p = new Period(start_millis+5000, end_millis, getPeriodType()); return p.get(getDurationFieldType()); }
/** * Get the duration in seconds -+ * @return the duration in seconds * @deprecated dont use because these are fixed length and thus approximate. */ public double getValueInMillisecs() { if (field == CalendarPeriod.Field.Month) return 30.0 * 24.0 * 60.0 * 60.0 * 1000.0 * value; else if (field == CalendarPeriod.Field.Year) return 365.0 * 24.0 * 60.0 * 60.0 * 1000.0 * value; else return millisecs(); }
if (timeUnitPeriod.equals(CalendarPeriod.Hour)) return range; if (timeUnitPeriod.getField() == CalendarPeriod.Field.Month) { fac = 30.0 * 24.0; // nominal hours in a month } else if (timeUnitPeriod.getField() == CalendarPeriod.Field.Year) { fac = 365.0 * 24.0; // nominal hours in a year } else { fac = CalendarPeriod.Hour.getConvertFactor(timeUnitPeriod);
@Override public CalendarDateRange makeCalendarDateRange(ucar.nc2.time.Calendar cal) { CalendarDateUnit cdu = CalendarDateUnit.withCalendar(cal, periodName + " since " + refDate.toString()); CalendarDate start = cdu.makeCalendarDate(timeUnit.getValue() * offsetSorted.get(0)); CalendarDate end = cdu.makeCalendarDate(timeUnit.getValue() * offsetSorted.get(getSize() - 1)); return CalendarDateRange.of(start, end); }
/** * Multiply the period by an integer * @param value multiply by this * @return new period */ public CalendarPeriod multiply(int value) { return new CalendarPeriod(this.value * value, this.field); }
public static CalendarDuration fromUnitString(String unitString) { return new CalendarDuration(1, CalendarPeriod.fromUnitString(unitString)); }
public static int getOffset(CalendarDate refDate, CalendarDate cd, CalendarPeriod timeUnit) { long msecs = cd.getDifferenceInMsecs(refDate); return (int) Math.round(msecs / timeUnit.getValueInMillisecs()); }
/** * Check if we all time intervals have the same length. * @return time interval name or MIXED_INTERVALS */ public String getTimeIntervalName() { // are they the same length ? int firstValue = -1; for (TimeCoord.Tinv tinv : timeIntervals) { int value = (tinv.getBounds2() - tinv.getBounds1()); if (firstValue < 0) firstValue = value; else if (value != firstValue) return MIXED_INTERVALS; } firstValue = (firstValue * timeUnit.getValue()); return firstValue + "_" + timeUnit.getField().toString(); }
public CalendarPeriod getTimeUnit() { return CalendarPeriod.of(1, CalendarPeriod.fromUnitString(unitString)); }
@Override public TimeCoord.TinvDate getForecastTimeInterval(Grib2Record gr) { Grib2Pds pds = gr.getPDS(); if (!pds.isTimeInterval()) return null; int[] intv = getForecastTimeIntervalOffset(gr); assert intv != null; int intvLen = intv[1]-intv[0]; int timeUnitOrg = pds.getTimeUnit(); int timeUnitConvert = convertTimeUnit(timeUnitOrg); CalendarPeriod unitPeriod = Grib2Utils.getCalendarPeriod(timeUnitConvert); if (unitPeriod == null) throw new IllegalArgumentException("unknown CalendarPeriod "+timeUnitConvert+ " org="+timeUnitOrg); CalendarPeriod.Field fld = unitPeriod.getField(); CalendarDate start = gr.getReferenceDate().add(intv[0], fld); CalendarPeriod period = CalendarPeriod.of(intvLen, fld); return new TimeCoord.TinvDate(start, period); }
public double getOffsetInTimeUnits(CalendarDate start) { return timeUnit.getOffset(start, getRefDate()); } ////////////////////////////////////////
public int getOffset(CalendarDate start, CalendarDate end) { Period p = new Period(start.getDateTime(), end.getDateTime(), getPeriodType()); return p.get(getDurationFieldType()); }