Add Lua function that returns the current time

This commit is contained in:
Alexander Bock
2016-04-27 16:17:50 +02:00
parent b46f40030b
commit 25702a787f
2 changed files with 34 additions and 0 deletions
+7
View File
@@ -215,6 +215,13 @@ scripting::ScriptEngine::LuaLibrary Time::luaLibrary() {
"",
"Returns the current time as an ISO 8601 date string "
"(YYYY-MM-DDTHH:MN:SS)"
},
{
"currentWallTime",
&luascriptfunctions::time_currentWallTime,
"",
"Returns the current wall time as an ISO 8601 date string "
"(YYYY-MM-DDTHH-MN-SS) in the UTC timezone"
}
}
};
+27
View File
@@ -22,6 +22,10 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <ghoul/misc/assert.h>
#include <ctime>
#include <cppformat/format.h>
namespace openspace {
namespace luascriptfunctions {
@@ -149,6 +153,29 @@ int time_currentTimeUTC(lua_State* L) {
return 1;
}
/**
* \ingroup LuaScripts
* currentWallTime():
* Returns the current wallclock time as a structured ISO 8601 string in the UTC timezone.
*/
int time_currentWallTime(lua_State* L) {
std::time_t t = std::time(nullptr);
std::tm* utcTime = std::gmtime(&t);
ghoul_assert(utcTime, "Conversion to UTC failed");
std::string time = fmt::format(
"{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}",
utcTime->tm_year + 1900,
utcTime->tm_mon + 1,
utcTime->tm_mday,
utcTime->tm_hour,
utcTime->tm_min,
utcTime->tm_sec
);
lua_pushstring(L, time.c_str());
return 1;
}
} // namespace luascriptfunctions
} // namespace openspace