Time

Time is a fickle mistress, the same is true in Superbase NG. We’re going to cover some of the basics of time in SIMPOL in this tutorial, we want to be able to get the current time, an arbitrary time from a string and be able to print these off in nicely formatted strings. We’re going to be using an included library for some of this, SBLTimLib

Time Declaration

Time is a strange type in SIMPOL, most base types in SIMPOL only require declaration at the top of the main function. Time in most cases requires a time.new() declaration on top of this to become useful to us. To get the current time the following code is required:

function main()
    time tTimeVal

    tTimeVal = time.new()
    tTimeVal.setnow()
end function tTimeVal

However this will not return a human readable time, returning something more like this: 25542008000 this is fine if we are not returning the time to a user and just using it internally but in most cases we will want to somewhere have a human readable time, to do this the following function is useful

function TimeFormat(time input)
  string output

  output = .tostr(input.hours(),10) + ":" + .tostr(input.minutes(),10) + ":" + .tostr(input.seconds(),10);
end function output

Putting this into our main function gives us a nice human readable time format as an output.

String to Time

The last thing we want to be able to do with time is convert a string we are given, or in this case make ourself into a SIMPOL time so we can for example sum times, or get time differences. To do this requires our sbltimelib library. This library concerns itself primarily with SBL compatibility but also contains the very useful string2time() function. A function making use of this as well as our TimeFormat function above to display the current time should look something like this:

constant sDEFTIMEFORMAT "0h:mm:ss";

function main()
    time tStringTime;
    string sTime;

    tStringTime = time.new()()
    tStringTime.setnow()

    sTime = "13:34:12";

    tStringTime = string2time(sTime,sDEFTIMEFORMAT)
end function TimeFormat(tStringTime)

Notice the constant declaration and use here. The string2time function requires a display format to convert SIMPOL time to,  the one we are using 0h:mm:ss is the most basic and also most useful of these although there others which will not be covered here.

A program showing the combination of those things we’ve covered today can be found as a .zip file below


.zip file

SBLTimeLib (Old)

Inbuilt SIMPOL time (old)


constant sDEFTIMEFORMAT "0h:mm:ss.s";

function main()
  time tTimeCurrent;
  time tStringTime;
  string sTime;
  string sOutput

  tTimeCurrent = time.new()
  tTimeCurrent.setnow()
  tStringTime = time.new()
  tStringTime.set(0,0,0,0,0);
  
  sTime = "13:34:12";

  tStringTime = string2time(sTime,sDEFTIMEFORMAT);

  sOutput = TimeFormat(tStringTime)
  sOutput = "Current: " + TimeFormat(tTimeCurrent) + ", Then: " + sOutput
end function sOutput

function TimeFormat(time input)
  string output

  output = .tostr(input.hours(),10) + ":" + .tostr(input.minutes(),10) + ":" + .tostr(input.seconds(),10);
end function output