Time

  1. Declaring Time
    a. Time to String
    b. String to Time
  2. Downloads
  3. Source Code

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, as it is not a base types and will therefore require a formal declaration. This is as simple as including the following line tTimeVal = time.new() before assigning any value. To get the current time the following code is required:

function main()
    time tTimeVal

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

Time to String

This program will not return a value most people would be able to read, returning something more like: 25542008000. This is fine if we are not returning the time to a user and just using it, for example, to check if something has timed out. However, in most cases it will be useful to have the time in a human readable format, to do this the following function is very 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 might want to do with time is the reverse of the above: convert a string value we input, into a format that SIMPOL can use so we can for example sum two times, or get the difference betweent two times.

Doing this requires the sbltimelib library. This library concerns itself primarily with SBL compatibility but also contains the very useful string2time() function. The following function makes use of this as well as our own TimeFormat function, to display the current time:

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 a string,  the one we are using 0h:mm:ss is the most basic and also most useful. There are many others and in more advanced cases a time format is included in the appframework library

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


Downloads


Source Code

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