TimeSpan

v4.0

The TimeSpan class contains float values for in-game seconds, minutes, hours, days, weeks, months, seasons, and years. This is used in a variety of the GameTime methods, and can also be saved to isolate specific chunks of time that are important to your project.

Many methods which utilize this concept will allow both a TimeSpan object, or a list of floats designating the specific TimeSpan to utilize.

public float Seconds
public float Minutes
public float Hours
public float Days
public float Months
public float Years

TimeSpan does not track time itself, and does not decrease as time passes.

TimeSpan does not include Weeks or Seasons, but Gametime methods will include those when handling TimeSpan objects and doing date and time calculations.

Create a TimeSpan

// Create a new Timespan object for 2 weeks -- Each value is optional, defaults to 0
TimeSpan myTimeSpan = new TimeSpan
{
    Weeks = 2
};

// Create an empty Timespan, then populate it
TimeSpan respawnTime = new TimeSpan();
respawnTime.Years = 1;
respawnTime.Months = 6;

Methods

Add another TimeSpan

public TimeSpan Add(TimeSpan other)

// Example
timeSpan = timeSpan.Add(BonusTime);

Multiply by a multiplier float value

public TimeSpan Multiply(float multiplier)

// Example
timeSpan = timeSpan.Multiply(0.5f); // Cut the time in half

Remember that all values in a TimeSpan are based on in-game time.

Timespan Methods on Gametime.cs

These general methods will return a TimeSpan object, which may be saved and used later.

// Provides the difference between start and end. May be negative if 
// start is before end.
public TimeSpan TimeSpanBetween(float startGameTime, float endGameTime)

// Provides the TimeSpan since one gameTime and another. Will use Now() if gameTime
// is not provided. May return negative if otherGameTime is > gameTime
public TimeSpan TimeSpanSince(float otherGameTime, float gameTime = -1)

// Provides the TimeSpan until one gameTime and another. Will use Now() if gameTime
// is not provided. May return negative if otherGameTime is < gameTime
public TimeSpan TimeSpanUntil(float otherGameTime, float gameTime = -1)

// Example
TimeSpan timeSpanUntilNextWinterSolstice = TimeSpanUntil(NextWinterSolsticeGameTime());

Convert from TimeSpan

See all the conversion methods on the Gametime Properties & Methods page

public float TimespanToGameTime(TimeSpan timeSpan)
public float TimeSpanToRealSeconds(TimeSpan timeSpan)
public float TimeSpanToDatePart(DatePart datePart, TimeSpan timeSpan)

Last updated