Esempio n. 1
0
std::string terrama2::core::DataStoragerTiff::replaceMask(const std::string& mask,
    std::shared_ptr<te::dt::DateTime> timestamp,
    terrama2::core::DataSetPtr dataSet) const
{
  if(!timestamp.get())
    return mask;

  long year = 0;
  long month = 0;
  long day = 0;
  long hour = 0;
  long minutes = 0;
  long seconds = 0;

  if(timestamp->getDateTimeType() == te::dt::TIME_INSTANT)
  {
    auto dateTime = std::dynamic_pointer_cast<te::dt::TimeInstant>(timestamp);
    //invalid date type
    if(dateTime->getTimeInstant().is_not_a_date_time())
      return mask;


    auto date = dateTime->getDate();
    year = date.getYear();
    month = date.getMonth().as_number();
    day = date.getDay().as_number();

    auto time = dateTime->getTime();
    hour = time.getHours();
    minutes = time.getMinutes();
    seconds = time.getSeconds();
  }
  else if(timestamp->getDateTimeType() == te::dt::TIME_INSTANT_TZ)
  {
    auto dateTime = std::dynamic_pointer_cast<te::dt::TimeInstantTZ>(timestamp);
    auto boostLocalTime = dateTime->getTimeInstantTZ();
    //invalid date type
    if(boostLocalTime.is_not_a_date_time())
      return mask;

    std::string timezone;
    try
    {
      //get dataset timezone
      timezone = getTimezone(dataSet);
    }
    catch(const terrama2::core::UndefinedTagException&)
    {
      //if no timezone is set use UTC
      timezone = "UTC+00";
    }

    boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone(timezone));
    auto localTime = boostLocalTime.local_time_in(zone);
    auto date = localTime.date();
    year = date.year();
    month = date.month().as_number();
    day = date.day();

    auto time = localTime.time_of_day();
    hour = time.hours();
    minutes = time.minutes();
    seconds = time.seconds();
  }
  else
  {
    //This method expects a valid Date/Time, other formats are not valid.
    QString errMsg = QObject::tr("Unknown date format.");
    TERRAMA2_LOG_ERROR() << errMsg;
    throw terrama2::core::DataAccessorException() << ErrorDescription(errMsg);
  }

  //replace wildcards in mask
  std::string fileName = mask;
  size_t pos = fileName.find("yyyy");
  if(pos != std::string::npos)
    fileName.replace(pos, 4, zeroPadNumber(year, 4));

  pos = fileName.find("yy");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(year, 2));

  pos = fileName.find("MM");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(month, 2));

  pos = fileName.find("dd");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(day, 2));

  pos = fileName.find("hh");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(hour, 2));

  pos = fileName.find("mm");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(minutes, 2));

  pos = fileName.find("ss");
  if(pos != std::string::npos)
    fileName.replace(pos, 2, zeroPadNumber(seconds, 2));

  //if no extension in the mask, add extension
  pos = fileName.find(".tif");
  if(pos != std::string::npos)
    fileName += ".tif";
  return fileName;
}
Esempio n. 2
0
 bool is_special()const
 {
   return(is_pos_infinity() || is_neg_infinity() || is_not_a_date_time());
 }
Esempio n. 3
0
void
change_c::parse_date_time() {
  //                 1          2          3                     4          5          6             7     8      9          10
  boost::regex re{"^ (\\d{4}) - (\\d{2}) - (\\d{2}) (?: T | \\h) (\\d{2}) : (\\d{2}) : (\\d{2}) \\h* ( Z | ([+-]) (\\d{2}) : (\\d{2}) ) $", boost::regex::perl | boost::regex::mod_x};
  boost::smatch matches;
  int64_t year, month, day, hours, minutes, seconds;
  int64_t offset_hours = 0, offset_minutes = 0, offset_mult = 1;

  auto valid = boost::regex_match(m_value, matches, re);

  if (valid)
    valid = parse_number(matches[1].str(), year)
         && parse_number(matches[2].str(), month)
         && parse_number(matches[3].str(), day)
         && parse_number(matches[4].str(), hours)
         && parse_number(matches[5].str(), minutes)
         && parse_number(matches[6].str(), seconds);

  if (valid && (matches[7].str() != "Z")) {
    valid = parse_number(matches[9].str(),  offset_hours)
         && parse_number(matches[10].str(), offset_minutes);

    if (matches[8].str() == "-")
      offset_mult = -1;
  }

  valid = valid
    && (year           >= 1900)
    && (month          >=   1)
    && (month          <=  12)
    && (day            >=   1)
    && (day            <=  31)
    && (hours          >=   0)
    && (hours          <=  23)
    && (minutes        >=   0)
    && (minutes        <=  59)
    && (offset_hours   >=   0)
    && (offset_hours   <=  23)
    && (offset_minutes >=   0)
    && (offset_minutes <=  59);

  if (valid) {
    try {
      auto date_time = boost::posix_time::ptime{ boost::gregorian::date(year, month, day), boost::posix_time::time_duration(hours, minutes, seconds) };

      if (!date_time.is_not_a_date_time()) {
        auto tz_offset  = (offset_hours * 60 + offset_minutes) * offset_mult;
        date_time      -= boost::posix_time::minutes(tz_offset);
      }

      if (!date_time.is_not_a_date_time())
        m_ui_value = mtx::date_time::to_time_t(date_time);

      return;

    } catch (std::out_of_range &) {
    }
  }

  mxerror(boost::format("%1% %2% %3% %4%\n")
          % (boost::format(Y("The property value is not a valid date & time string in '%1%'.")) % get_spec()).str()
          % Y("The recognized format is 'YYYY-mm-ddTHH:MM:SS+zz:zz': the year, month, day, letter 'T', hours, minutes, seconds and the time zone's offset from UTC; example: 2017-03-28T17:28-02:00.")
          % Y("The letter 'Z' can be used instead of the time zone's offset from UTC to indicate UTC aka Zulu time.")
          % FILE_NOT_MODIFIED);
}