Fork me on GitHub

Time Capacity

The built-in capacity Time provides tools for obtaining the current time from the run-time platform.

Time definition is application-dependent and platform-dependent. In other words, the time values replied by this capacity depends on the run-time environment: it may be the operating system time, or a simulator time.

1. Get the Current Time

For obtaining the current time, the getTime function is provides by the Time capacity:

def getTime(timeUnit : TimeUnit = null) : double

The timeUnit parameter will enable you to specify the unit of the replied value (hours, seconds, milliseconds, etc.). If it is not provided, the values will be expressed in seconds.

agent A {
	uses Time
	def myaction {
		var ct = getTime
		var ct2 = getTime(null)
		var ct3 = getTime(TimeUnit::HOURS)
	}
}

2. Convertion from Operating System Time to the SARL Run-time Time

At least, there is two different time scales:

The function getOSTimeFactor provides the multiplication factor between these two time scales. The functions toOSTime and fromOSTime do the convertion of a time between these two scales. The functions toOSDuration and fromOSDuration do the convertion of a duration between these two scales.

2.1. Get the Factor for Mapping to Operating-System Time

For the time replied by the getTime function may be not the operating-system time, e.g. a simulation time. In order to map the time replied by the getTime function and the operating-system time, the getOSTimeFactor function replies a factor that could be applied so that:

operatingSystemTime = getOSTimeFactor * getTime

The provided function is:

def getOSTimeFactor : double

2.2. Convertion of a Time

For converting a time, you have access to two functions:

// Convertion from OS to SRE
sreTime = fromOSTime(operatingSystemTime)
// Convertion from SRE to OS
operatingSystemTime = toOSTime(sreTime)

2.3. Convertion of a Duration

For converting a duration, you have access to two functions:

// Convertion from OS to SRE
sreTime = fromOSDuration(operatingSystemTime)
// Convertion from SRE to OS
operatingSystemTime = toOSDuration(sreTime)

Copyright © 2014-2024 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.14.0.