Example #1
0
// static
Time Time::FromExploded(bool is_local, const Exploded& exploded)
{
    // 创建系统拆分时间结构, 代表本地时间或者UTC.
    SYSTEMTIME st;
    st.wYear = exploded.year;
    st.wMonth = exploded.month;
    st.wDayOfWeek = exploded.day_of_week;
    st.wDay = exploded.day_of_month;
    st.wHour = exploded.hour;
    st.wMinute = exploded.minute;
    st.wSecond = exploded.second;
    st.wMilliseconds = exploded.millisecond;

    // 转换到FILETIME.
    FILETIME ft;
    if(!SystemTimeToFileTime(&st, &ft))
    {
        NOTREACHED() << "Unable to convert time";
        return Time(0);
    }

    // 确保是UTC.
    if(is_local)
    {
        FILETIME utc_ft;
        LocalFileTimeToFileTime(&ft, &utc_ft);
        return Time(FileTimeToMicroseconds(utc_ft));
    }
    return Time(FileTimeToMicroseconds(ft));
}
Example #2
0
// static
Time Time::Now()
{
    if(initial_time == 0)
    {
        InitializeClock();
    }

    // 高精度计数器实现计时, 能够得到比10-15ms小的超时. 仅通过
    // CurrentWallclockMicroseconds(), 无法得到细粒度的定时器.
    //
    // 使用时, 初始化时钟(initial_time)和计数器(initial_ctr). 通过
    // 和初始时钟比较能得到消逝的时钟数, 然后就能得出时间差.
    //
    // 为避免误差, 计数器定期的和系统时钟同步.
    while(true)
    {
        TimeTicks ticks = TimeTicks::Now();

        // 计算自开始以来的时间计数.
        TimeDelta elapsed = ticks - initial_ticks;

        // 检查是否需要同步时钟.
        if(elapsed.InMilliseconds() > kMaxMillisecondsToAvoidDrift)
        {
            InitializeClock();
            continue;
        }

        return Time(elapsed + Time(initial_time));
    }
}
Example #3
0
// static
Time Time::FromFileTime(FILETIME ft)
{
    return Time(FileTimeToMicroseconds(ft));
}
Example #4
0
// static
Time Time::NowFromSystemTime()
{
    // 强制同步.
    InitializeClock();
    return Time(initial_time);
}
void ManipulatorJointTrajectory::externalize(Externalizer& e, String format, Real version)
{
  if (format=="") format = String("xml");

  if (!formatSupported(format,version,e.ioType()))
    throw std::invalid_argument(Exception(String("format ")+format+" "+base::realToString(version)+" unsupported"));

  if (format == "txt") {

    if (e.isInput()) {
      array<Vector> qs;
      array<Time> times;
      bool first = true;
      bool deltas = false;
      
      while (e.moreInput()) {
	
	String line(e.readLine());
	if (line.empty()) break; // empty line or eof indicates end of list
	while (line[0] == '#') line = e.readLine(); // skip comment lines
	
	if (first) {
	  if (!( (line.substr(0,8) == "absolute") || (line.substr(0,8) == "relative")))
	    throw std::invalid_argument(Exception(String("joint waypoint text file must indicate 'absolute' or 'relative'")));
	  
	  deltas = (line.substr(0,8) == String("relative"));
	  first = false;
	}
	else {
	  // count separating spaces to determine dof
	  Int spaces=0;
	  for(Int i=0; i<line.size(); i++) 
	    if (line[i]==' ') { 
	      spaces++;
	      while ( (i<line.size()-1) && (line[i]==' ')) i++;
	    }
	  if (line[0]==' ') spaces--; // don't count leading space(s)
	  
	  std::istringstream iss(line);
	  iss.setf(std::ios_base::skipws | std::ios_base::dec);
	  
	  const Int dof = spaces + 1 - 1; // don't count time at end as a dof
	  
	  Vector q(dof);
	  
	  for(Int i=0; i<dof; i++)
	    iss >> q[i];
	  
	  Real t;
	  iss >> t;
	  
	  qs.push_back(q);
	  
	  times.push_back(Time(t));
	}
      }
      
      if (first)
	throw std::invalid_argument(Exception(String("joint waypoint text file contains no data.")));
      
      if (qs.size() < 2)
	throw std::invalid_argument(Exception(String("joint waypoint text file must contain at least 2 'waypoint' vectors")));
      
      init(qs,times,deltas);
      
    }
    else { // output
Time ManipulatorJointTrajectory::time(Real s) const
{
  Math::bound<Real>(s,0,1);
  return Time( times[0].seconds() +  ( (times[times.size()-1] - times[0]).seconds() * s ) );
}