Skip to content

Instantly share code, notes, and snippets.

@artem78
Created July 12, 2020 09:56
Show Gist options
  • Save artem78/1c53cf96e40816b73f0b1958a448426f to your computer and use it in GitHub Desktop.
Save artem78/1c53cf96e40816b73f0b1958a448426f to your computer and use it in GitHub Desktop.
Symbian OS: microseconds time interval to readable string
// Include Files
#include "Interval2Des.h"
#include <e32base.h>
#include <e32std.h>
#include <e32cons.h> // Console
// Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");
// Global Variables
LOCAL_D CConsoleBase* console; // write all messages to this
// Local Functions
LOCAL_C void MainL()
{
PrintInterval(0); // 0us
PrintInterval(251); // 251us
PrintInterval(984124); // 984ms
PrintInterval(7245128); // 7.24s
PrintInterval(367 * 1000 * 1000); // 6.12m
PrintInterval(15601000100); // 4.33h
PrintInterval(-12345678); // -12.35s
PrintInterval(-98765432198); // -27.43s
PrintInterval(8123456, ETrue); // +8.12s
PrintInterval(0, ETrue); // +0us
}
LOCAL_C void DoStartL()
{
// Create active scheduler (to run active objects)
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
MainL();
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
}
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
// Create output console
TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(
KConsFullScreen, KConsFullScreen)));
if (createError)
{
delete cleanup;
return createError;
}
// Run application code inside TRAP harness, wait keypress when terminated
TRAPD(mainError, DoStartL());
if (mainError)
console->Printf(KTextFailed, mainError);
console->Printf(KTextPressAnyKey);
console->Getch();
delete console;
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}
void PrintInterval(TTimeIntervalMicroSeconds aInterval, TBool aPrintPlusSign)
{
TBuf<64> buff1;
buff1.Num(aInterval.Int64());
TBuf<64> buff2;
TimeIntervalToDes(aInterval, buff2, aPrintPlusSign);
console->Printf(_L("%S\t= %S\n"), &buff1, &buff2);
}
void TimeIntervalToDes(TTimeIntervalMicroSeconds aInterval, TDes &aDes, TBool aAddPlusSign)
{
const TInt64 KMilliSecond = 1000;
const TInt64 KSecond = KMilliSecond * 1000;
const TInt64 KMinute = KSecond * 60;
const TInt64 KHour = KMinute * 60;
const TChar KPlusSign = '+';
aDes.Zero();
if (aInterval >= 0 && aAddPlusSign)
aDes.Append(KPlusSign);
/*TUint64*/ TInt64 absInterval = Abs(aInterval.Int64());
if (absInterval < KMilliSecond)
{ // As is (microseconds)
aDes.AppendFormat(_L("%Ldus"), aInterval.Int64());
}
else if (absInterval < KSecond)
{ // Milliseconds
aDes.AppendFormat(_L("%.3fms"), aInterval.Int64() / TReal(KMilliSecond));
}
else if (absInterval < KMinute)
{ // Seconds
aDes.AppendFormat(_L("%.3fs"), aInterval.Int64() / TReal(KSecond));
}
else if (absInterval < KHour)
{ // Minutes
aDes.AppendFormat(_L("%.2fm"), aInterval.Int64() / TReal(KMinute));
}
else
{ // Hours
aDes.AppendFormat(_L("%.2fh"), aInterval.Int64() / TReal(KHour));
}
};
#ifndef __INTERVAL2DES_H__
#define __INTERVAL2DES_H__
// Include Files
#include <e32base.h>
// Function Prototypes
GLDEF_C TInt E32Main();
void PrintInterval(TTimeIntervalMicroSeconds aInterval, TBool aPrintPlusSign = EFalse);
void TimeIntervalToDes(TTimeIntervalMicroSeconds aInterval, TDes &aDes, TBool aPlusSign = EFalse);
#endif // __INTERVAL2DES_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment