<commit_before>
----------------------------------------------------------------------------
--  time.lua - time related classes and functions
--
--  Created: Mon Aug 09 14:11:59 2010 (at Intel Research, Pittsburgh)
--  Copyright  2010  <NAME> [www.niemueller.de]
--
----------------------------------------------------------------------------

-- Licensed under BSD license

--- Time utility class for roslua.
-- This module provides the Time class. It uses the local clock or the
-- /clock topic depending whether simulation time has been initialized or
-- not.
-- @copyright <NAME>, Carnegie Mellon University, Intel Research Pittsburgh
-- @release Released under BSD license
module("roslua.time", package.seeall)

require("pimpedposix")

local sub_clock
local sim_time

--- Local function called on simtime updates, i.e. new messages
-- for /clock.
-- @param message new Clock message
local function simtime_update(message)
   -- Have to actually test with a simulator, until then just give warning
   assert(sim_time < message.values.clock, "The time received from the simulator is smaller "..
	  "than the curerntly set time. This happened because of small increments to the "..
	  "nsec part of the last sim time update. It indicates that the simulator is "..
	  "producing updates too slow for too small increments of time. Check with roslua "..
	  "authors on what to do")
   sim_time = message.values.clock
end

--- Initialize simulation time.
-- This initializes the simulation time by subscribing to the /clock topic.
-- This function is called automatically from roslua.init_node() if required,
-- so you should not call this function directly.
function init_simtime()
   sub_clock = roslua.subscriber("/clock", "roslib/Clock")
   sub_clock.add_listener(simtime_update)
end


Time = { sec = 0, nsec = 0 }

--- Contructor.
-- @param sec seconds, 0 if not supplied
-- @param nsec nano seconds, 0 if not supploed
-- @return new Time instance
function Time:new(sec, nsec)
   local o = {}
   setmetatable(o, self)
   self.__index = self

   o.sec  = sec  or 0
   o.nsec = nsec or 0

   -- for deserialization compatibility
   o[1]   = o.sec
   o[2]   = o.nsec

   return o
end

--- Check if given table is an instance of Time.
-- @param t instance to check
-- @return true if given object t is an instance of Time
function Time.is_instance(t)
   return getmetatable(t) == Time
end

--- Clone an instance (copy constructor).
-- @param time Time instance to clone
-- @return new instances for the same time as the given one
function Time:clone()
   return Time:new(self.sec, self.nsec)
end

--- Get current time.
-- @return new Time instance set to the current time
function Time.now()
   local t = Time:new()
   t:stamp()
   return t
end


--- Set to current time.
function Time:stamp()
   if sim_time then
      -- Add 1 to the nsec value to ensure that two consecutive calls to stamp will
      -- never return the same time, they wouldn't on a real system as well
      sim_time.nsec = sim_time.nsec + 1
      if sim_time.nsec >= 1000000000 then
	 sim_time.sec  = sim_time.sec + 1
	 sim_time.nsec = 0
      end
      self.sec, self.nsec = sim_time.sec, sim_time.nsec
   else
      self.sec, self.nsec = posix.clock_gettime("realtime")
   end
end

--- Create Time from seconds.
-- @param sec seconds since the epoch as a floating point number,
-- the fraction is converted internally to nanoseconds
-- @return new instance for the given time
function Time.from_sec(sec)
   local t = Time:new()
   t.sec  = math.floor(sec)
   t.nsec = (sec - t.sec) * 1000000000
   return t
end


--- Create Time from message array.
-- @param a array that contains two entries, index 1 must be seconds, index 2 must
-- be nano seconds.
-- @return new instance for the given time
function Time.from_message_array(a)
   local t = Time:new()
   t.sec  = a[1]
   t.nsec = math.floor(a[2] / 1000.)
   return t
end

--- Check if time is zero.
-- @return true if sec and nsec fields are zero, false otherwise
function Time:is_zero()
   return self.sec == 0 and self.nsec == 0
end


--- Set sec and nsec values.
-- @param sec new seconds value
-- @param nsec new nano seconds value
function Time:set(sec, nsec)
   self.sec  = sec or 0
   self.nsec = nsec or 0
end


--- Convert time to seconds.
-- @return floating point number in seconds.
function Time:to_sec()
   return self.sec + self.nsec / 1000000
end


--- Add the given times t1 and t2.
-- @param t1 first time to add
-- @param t2 second time to add
-- @return new time instance with the sum of t1 and t2
function Time.__add(t1, t2)
   local t = Time:new()
   t.sec  = t1.sec + t2.sec
   t.nsec = t1.nsec + t2.nsec
   if t.nsec > 1000000 then
      local n = math.floor(t.nsec / 1000000000)
      t.sec  = t.sec  + n<commit_msg>Fix time usec vs. nsec bug<commit_after>
----------------------------------------------------------------------------
--  time.lua - time related classes and functions
--
--  Created: Mon Aug 09 14:11:59 2010 (at Intel Research, Pittsburgh)
--  Copyright  2010  <NAME> [www.niemueller.de]
--
----------------------------------------------------------------------------

-- Licensed under BSD license

--- Time utility class for roslua.
-- This module provides the Time class. It uses the local clock or the
-- /clock topic depending whether simulation time has been initialized or
-- not.
-- @copyright <NAME>, Carnegie Mellon University, Intel Research Pittsburgh
-- @release Released under BSD license
module("roslua.time", package.seeall)

require("pimpedposix")

local sub_clock
local sim_time

--- Local function called on simtime updates, i.e. new messages
-- for /clock.
-- @param message new Clock message
local function simtime_update(message)
   -- Have to actually test with a simulator, until then just give warning
   assert(sim_time < message.values.clock, "The time received from the simulator is smaller "..
	  "than the curerntly set time. This happened because of small increments to the "..
	  "nsec part of the last sim time update. It indicates that the simulator is "..
	  "producing updates too slow for too small increments of time. Check with roslua "..
	  "authors on what to do")
   sim_time = message.values.clock
end

--- Initialize simulation time.
-- This initializes the simulation time by subscribing to the /clock topic.
-- This function is called automatically from roslua.init_node() if required,
-- so you should not call this function directly.
function init_simtime()
   sub_clock = roslua.subscriber("/clock", "roslib/Clock")
   sub_clock.add_listener(simtime_update)
end


Time = { sec = 0, nsec = 0 }

--- Contructor.
-- @param sec seconds, 0 if not supplied
-- @param nsec nano seconds, 0 if not supploed
-- @return new Time instance
function Time:new(sec, nsec)
   local o = {}
   setmetatable(o, self)
   self.__index = self

   o.sec  = sec  or 0
   o.nsec = nsec or 0

   -- for deserialization compatibility
   o[1]   = o.sec
   o[2]   = o.nsec

   return o
end

--- Check if given table is an instance of Time.
-- @param t instance to check
-- @return true if given object t is an instance of Time
function Time.is_instance(t)
   return getmetatable(t) == Time
end

--- Clone an instance (copy constructor).
-- @param time Time instance to clone
-- @return new instances for the same time as the given one
function Time:clone()
   return Time:new(self.sec, self.nsec)
end

--- Get current time.
-- @return new Time instance set to the current time
function Time.now()
   local t = Time:new()
   t:stamp()
   return t
end


--- Set to current time.
function Time:stamp()
   if sim_time then
      -- Add 1 to the nsec value to ensure that two consecutive calls to stamp will
      -- never return the same time, they wouldn't on a real system as well
      sim_time.nsec = sim_time.nsec + 1
      if sim_time.nsec >= 1000000000 then
	 sim_time.sec  = sim_time.sec + 1
	 sim_time.nsec = 0
      end
      self.sec, self.nsec = sim_time.sec, sim_time.nsec
   else
      self.sec, self.nsec = posix.clock_gettime("realtime")
   end
end

--- Create Time from seconds.
-- @param sec seconds since the epoch as a floating point number,
-- the fraction is converted internally to nanoseconds
-- @return new instance for the given time
function Time.from_sec(sec)
   local t = Time:new()
   t.sec  = math.floor(sec)
   t.nsec = (sec - t.sec) * 1000000000
   return t
end


--- Create Time from message array.
-- @param a array that contains two entries, index 1 must be seconds, index 2 must
-- be nano seconds.
-- @return new instance for the given time
function Time.from_message_array(a)
   local t = Time:new()
   t.sec  = a[1]
   t.nsec = math.floor(a[2] / 1000.)
   return t
end

--- Check if time is zero.
-- @return true if sec and nsec fields are zero, false otherwise
function Time:is_zero()
   return self.sec == 0 and self.nsec == 0
end


--- Set sec and nsec values.
-- @param sec new seconds value
-- @param nsec new nano seconds value
function Time:set(sec, nsec)
   self.sec  = sec or 0
   self.nsec = nsec or 0
end


--- Convert time to seconds.
-- @return floating point number in seconds.
function Time:to_sec()
   return self.sec + self.nsec / 1000000000
end


--- Add the given times t1 and t2.
-- @param t1 first time to add
-- @param t2 second time to add
-- @return new time instance with the sum of t1 and t2
function Time.__add(t1, t2)
   local t = Time:new()
   t.sec  = t1.sec + t2.sec
   t.nsec = t1.nsec + t2.nsec
   if t.nsec > 1000000000 then
      local n = math.floor(t.nsec / 1000000000)
      t.sec  = t.sec  + n
{'fraction_non_alphanumeric': 0.12566705032960135, 'fraction_numerical': 0.02071779847232395, 'mean_word_length': 2.60271390878251, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2087788', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2850, 'n_words': 1208}