Module:TimeSpan/doc

From Melvor Idle

This is the documentation page for Module:TimeSpan

TimeSpan Module Documentation

The 'TimeSpan' module provides functionality for working with time spans in Lua.

Constructor

TimeSpan.new(days, hours, minutes, seconds, milliseconds)

Creates a new 'TimeSpan' object representing the specified time components.

  • Parameters:
 * `'days'`: Number of days (default: `0`).
 * `'hours'`: Number of hours (default: `0`).
 * `'minutes'`: Number of minutes (default: `0`).
 * `'seconds'`: Number of seconds (default: `0`).
 * `'milliseconds'`: Number of milliseconds (default: `0`).
  • Returns: 'TimeSpan' object.

TimeSpan.fromSeconds(seconds)

Creates a new 'TimeSpan' object from a total number of seconds.

  • Parameters:
 * `'seconds'`: Total number of seconds.
  • Returns: 'TimeSpan' object representing the specified duration in seconds.

TimeSpan.fromMilliseconds(milliseconds)

Creates a new 'TimeSpan' object from a total number of milliseconds.

  • Parameters:
 * `'milliseconds'`: Total number of milliseconds.
  • Returns: 'TimeSpan' object representing the specified duration in milliseconds.

Methods

TimeSpan:getDays()

Gets the number of days in the 'TimeSpan'.

  • Returns: Number of days as an integer.

TimeSpan:getHours()

Gets the number of hours in the 'TimeSpan'.

  • Returns: Number of hours as an integer.

TimeSpan:getMinutes()

Gets the number of minutes in the 'TimeSpan'.

  • Returns: Number of minutes as an integer.

TimeSpan:getSeconds()

Gets the number of seconds in the 'TimeSpan'.

  • Returns: Number of seconds as an integer.

TimeSpan:getMilliseconds()

Gets the number of milliseconds in the 'TimeSpan'.

  • Returns: Number of milliseconds as an integer.

TimeSpan:totalDays()

Gets the total number of days represented by the 'TimeSpan' (including fractional days).

  • Returns: Total number of days as a floating-point number.

TimeSpan:totalHours()

Gets the total number of hours represented by the 'TimeSpan' (including fractional hours).

  • Returns: Total number of hours as a floating-point number.

TimeSpan:totalMinutes()

Gets the total number of minutes represented by the 'TimeSpan' (including fractional minutes).

  • Returns: Total number of minutes as a floating-point number.

TimeSpan:totalSeconds()

Gets the total number of seconds represented by the 'TimeSpan' (including fractional seconds).

  • Returns: Total number of seconds as a floating-point number.

TimeSpan:totalMilliseconds()

Gets the total number of milliseconds represented by the 'TimeSpan' (including fractional milliseconds).

  • Returns: Total number of milliseconds as a floating-point number.

TimeSpan:toString()

Converts the 'TimeSpan' object to a string representation.

  • Returns: String representation of the 'TimeSpan' in the format `'d.hh:mm:ss.fff'`, where `'d'` is days, `'hh'` is hours, `'mm'` is minutes, `'ss'` is seconds, and `'fff'` is milliseconds.

Example Usage

To use the 'TimeSpan' module in your Lua scripts, follow these examples:

local TimeSpan = require('TimeSpan')

-- Create a new TimeSpan representing 1 day, 2 hours, 30 minutes, 15 seconds, and 500 milliseconds
local ts = TimeSpan.new(1, 2, 30, 15, 500)

-- Get the string representation of the TimeSpan
local tsString = ts:toString()

-- Output the TimeSpan string representation
print(tsString)  -- Output: "1.02:30:15.500"

-- Get specific components of the TimeSpan
print("Days:", ts:getDays())  -- Output: 1
print("Hours:", ts:getHours())  -- Output: 2
print("Minutes:", ts:getMinutes())  -- Output: 30
print("Seconds:", ts:getSeconds())  -- Output: 15
print("Milliseconds:", ts:getMilliseconds())  -- Output: 500

-- Get the total number of hours represented by the TimeSpan
local totalHours = ts:totalHours()
print("Total Hours:", totalHours)  -- Output: 26.507638888889

-- Get the total number of minutes represented by the TimeSpan
local totalMinutes = ts:totalMinutes()
print("Total Minutes:", totalMinutes)  -- Output: 1590.4583333333