This for sure already appeared here, but I find it semi-useful for things like debug messages or some custom randomization libraries that take seed depending on time and whatnot
I know it could use CTL, but meh its just one timer
JASS:
library GameTime
/*
made by edo494
version 1.1
This library provides basic API for getting time in both functional and
vJass(struct based) style.
This allows you to get current time in raw format in either real(with 3 decimal
places) or in integer(multiplied) format and set the time to custom value if you so desire.
This method should never revert the returned integer format, because it has period of
59 hours, 3 minutes, 54 seconds and 836.47 milliseconds, which is more than any game will ever go on for.
Configurables:
private constant real PERIOD = 0.003250000
API:
struct Time extends array
static method operator integer takes nothing returns integer
- returns integer representation of the time passed since the beggining of the game
static method operator real takes nothing returns real
- returns floating point representation of the time passed since the beginning of the game
static method operator hrs takes nothing returns integer
- returns hours passed since the start of the game
- this returns the current number in ASCII representation
static method operator min takes nothing returns integer
- returns minutes passed since the start of the game
- this returns the current number in ASCII representation
so 75 minutes == 15 minutes 1 hour
static method operator sec takes nothing returns integer
- returns seconds passed since the start of the game
- this returns the current number in ASCII representation
so 75 seconds == 15 seconds 1 minute
static method operator ms takes nothing returns integer
- returns milliseconds passed since the start of the game
- this returns the current number in ASCII representation
so 1785 milliseconds == 785 milliseconds 1 second
static method setTimeInt takes integer newTime returns nothing
- sets new time depending on the newTime(this is the same format as
the value returned from operator integer)
static method setTime takes integer newH, integer newM, integer newS, real newMs returns nothing
- sets new time to specified number of hours, minuts, seconds and milliseconds
endstruct
functions:
function GetTimeInt takes nothing returns integer
- returns operator integer
function GetTimeReal takes nothing returns real
- returns operator real
function SetTime takes integer hours, integer minutes, integer seconds, real ms returns nothing
- calls Time.setTime
function SetTimeInt takes integer newTime returns nothing
- calls Time.setTimeInt
function GetHours takes nothing returns integer
- returns operator hrs
function GetMinutes takes nothing returns integer
- returns operator min
function GetSeconds takes nothing returns integer
- returns operator sec
function GetMilliSeconds takes nothing returns real
- returns operator ms
function GetDifferenceHours takes integer rawTime returns integer
- Takes integer rawTime in the same format as returned value from
GetTimeInt and compares it with the current time
- returned value is in format of GetHours
function GetDifferenceMinutes takes integer rawTime returns integer
- Takes integer rawTime in the same format as returned value from
GetTimeInt and compares it with the current time
- returned value is in format of GetMinutes
function GetDifferenceSeconds takes integer rawTime returns integer
- Takes integer rawTime in the same format as returned value from
GetTimeInt and compares it with the current time
- returned value is in format of GetSeconds
function GetDifferenceMilliseconds takes integer rawTime returns real
- Takes integer rawTime in the same format as returned value from
GetTimeInt and compares it with the current time
- returned value is in format of GetMilliSeconds
*/
//===== Configurables =====
globals
private constant real PERIOD = 0.003250000
globals
//===== End Configurables =====
private module m
private static method onUpdate takes nothing returns nothing
set milliseconds = milliseconds + PERIOD * 1000
if milliseconds >= 1000. then
set seconds = seconds + R2I(milliseconds/1000)
set milliseconds = ModuloInteger(R2I(milliseconds), 1000)
if seconds >= 60. then
set minutes = minutes + seconds/60
set seconds = ModuloInteger(seconds, 60)
if minutes >= 60. then
set hours = hours + minutes/60
set minutes = ModuloInteger(minutes, 60)
endif
endif
endif
endmethod
private static method onLoad takes nothing returns nothing
call TimerStart(GetExpiredTimer(), PERIOD, true, function thistype.onUpdate)
endmethod
private static method onInit takes nothing returns nothing
call TimerStart(CreateTimer(), 0.00, false, function thistype.onLoad)
endmethod
endmodule
struct Time extends array
readonly static integer hours = 0
readonly static integer minutes = 0
readonly static integer seconds = 0
readonly static real milliseconds = 0
/*
Reverts in 59 hours, 3 minutes, 54 seconds and 836.47 ms
*/
static method operator integer takes nothing returns integer
return R2I(milliseconds * 100) + seconds * 100000 +/*
*/minutes * 6000000 + hours * 36000000
endmethod
static method operator real takes nothing returns real
return milliseconds + I2R(seconds * 1000) +/*
*/I2R(minutes * 60000) + I2R(hours * 360000)
endmethod
static method operator hrs takes nothing returns integer
return hours
endmethod
static method operator min takes nothing returns integer
return minutes
endmethod
static method operator sec takes nothing returns integer
return seconds
endmethod
static method operator ms takes nothing returns real
return milliseconds
endmethod
static method setTimeInt takes integer newTime returns nothing
local real reminder = 0
set hours = newTime / 36000000
set minutes = (newTime - hours * 36000000) / 6000000
set seconds = (((newTime - hours * 36000000) / 6000000) - minutes * 6000000) / 100000
set milliseconds = ((((newTime - hours * 36000000) / 6000000) - minutes * 6000000) / 100000) - seconds * 100000
endmethod
static method setTime takes integer newH, integer newM, integer newS, real newMs returns nothing
set hours = newH
set minutes = newM
set seconds = newS
set milliseconds = newMs
endmethod
implement m
endstruct
function GetTimeInt takes nothing returns integer
return Time.integer
endfunction
function GetTimeReal takes nothing returns real
return Time.real
endfunction
function SetTime takes integer hours, integer minutes, integer seconds, real ms returns nothing
call Time.setTime(hours, minutes, seconds, ms)
endfunction
function SetTimeInt takes integer newTime returns nothing
call Time.setTimeInt(newTime)
endfunction
function GetHours takes nothing returns integer
return Time.hours
endfunction
function GetMinutes takes nothing returns integer
return Time.minutes
endfunction
function GetSeconds takes nothing returns integer
return Time.seconds
endfunction
function GetMilliSeconds takes nothing returns real
return Time.milliseconds
endfunction
function GetDifferenceHours takes integer rawTime returns integer
local integer i = Time.integer
local integer q = Time.hrs
call Time.setTimeInt(rawTime)
set q = Time.hrs - q
call Time.setTimeInt(i)
return q
endfunction
function GetDifferenceMinutes takes integer rawTime returns integer
local integer i = Time.integer
local integer q = Time.min
call Time.setTimeInt(rawTime)
set q = Time.min - q
call Time.setTimeInt(i)
return q
endfunction
function GetDifferenceSeconds takes integer rawTime returns integer
local integer i = Time.integer
local integer q = Time.sec
call Time.setTimeInt(rawTime)
set q = Time.sec - q
call Time.setTimeInt(i)
return q
endfunction
function GetDifferenceMilliseconds takes integer rawTime returns real
local integer i = Time.integer
local real q = Time.ms
call Time.setTimeInt(rawTime)
set q = Time.ms - q
call Time.setTimeInt(i)
return q
endfunction
endlibrary
I know it could use CTL, but meh its just one timer
Last edited: