RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

[2.x] Usage Syntax: DateTime, TimeSpan & TickCount

Vorspire

Knight
Working with dates and times to determine when things have, or should happen...

Just a simple reference thread for those having trouble with keeping time ( like I do, but not in the same way :p )

RunUO < 2.3
C#:
// if 'when' is in the past, time will always be > 0
// if 'when' is in the future, time will always be < 0

DateTime when; // = ?

TimeSpan time = DateTime.UtcNow - when;

RunUO 2.3+
C#:
// values actually represent the amount of time in 
// milliseconds since Core.TickCount was instantiated (server start).

long when; // = ?

long time = Core.TickCount - when;

Using 'time' as a factor in a logical conditional statement:

RunUO < 2.3
C#:
DateTime when; // = ?

TimeSpan time = DateTime.UtcNow - when;

if( time.TotalMilliseconds >= 1500 )
{
     // At least 1500 milliseconds (1.5 seconds) have passed since 'when'
}

RunUO 2.3+
C#:
long when; // = ?

long time = Core.TickCount - when;

if( time >= 1500 )
{
     // At least 1500 milliseconds (1.5 seconds) have passed since 'when'
}

That's the break-down.
The code used by the OP's script contains all of the above in a single conditional statement, the syntax is as such;

RunUO < 2.3
C#:
// 'now - when' is wrapped up with parentheses ( and ) here.
// 'now - when' resolves a TimeSpan value type, so we need to get TotalMilliseconds for the check.

if( (now - when).TotalMilliseconds >= 1500 )
{
     // At least 1500 milliseconds (1.5 seconds) have passed since 'when'
}

// can also be written as:
if( now - when >= TimeSpan.FromMilliseconds( 1500 ) )
{
     // At least 1500 milliseconds (1.5 seconds) have passed since 'when'
}

RunUO 2.3+
C#:
// note the missing parentheses ( and ) around 'now - when'
// the left-side and right-side of an operation are evaluated separately.
// the value of 'now - when' is determined before the 'above or equal' check.
// since 'now - value' is operating on 2 int64 types, no wrapping is needed.

if( now - when >= 1500 )
{
     // At least 1500 milliseconds (1.5 seconds) have passed since 'when'
}
 
Top