Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly calculate IRL time based on game ticks #24

Merged
merged 1 commit into from
May 26, 2024
Merged

Conversation

zefir-git
Copy link
Member

@zefir-git zefir-git commented May 26, 2024

Explanation of game time to IRL time conversion

getFullTime() returns the absolute in-game time in ticks. One tick is 50ms (20TPS = 1/20s) and Minecraft time passes 72 times faster than IRL time (IRL_day_ms / (MC_day_ticks * 50) = 86400000 / (24000 * 50)). And 3600 = 72*50, thus converting in-game time to IRL time: game_ticks * 3600

However, the first day of Minecraft (i.e. absolute time 0 ticks) is not midnight, but 06:00 AM. Therefore, 21600000 (6 IRL hours in ms) is added.

Therefore, the first day of the server was Thursday, January 1st 1970, at 06:00 AM (UNIX time is used, which starts in the year 1970).

IRL time to game time

Unfortunately, the in-game absolute time is not linear as players can sleep to skip nights and thunderstorms, effectively adding ticks to the absolute time. As the ticks added by players sleeping are not recorded, it is not possible to reverse the conversion.

The following is a JavaScript/TypeScript function to estimate IRL time to game time by ignoring the sleeping factor.

// d is a Date object for IRL time
const estimateIrlToGameDate = d => {
        // IRL timestamp for when the server was opened (± 2s IRL)
        // should be equal to 0 in-game ticks absolute time
	const serverStart = 1714417200000;
        // converts IRL time to in-game ticks
	const irlToTicks = t => (t - serverStart)/1000 * 20;
        // game absolute time to IRL equivalent
	return new Date(irlToTicks(d.getTime()) * 3600 + 21600000).toGMTString();
}

@zefir-git zefir-git added the bug An unexpected problem or behaviour label May 26, 2024
@zefir-git zefir-git requested a review from a team May 26, 2024 00:22
@zefir-git zefir-git self-assigned this May 26, 2024
@zefir-git zefir-git enabled auto-merge May 26, 2024 00:24
@zefir-git zefir-git merged commit 9a47442 into main May 26, 2024
3 checks passed
@zefir-git zefir-git deleted the fix-time branch May 26, 2024 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or behaviour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants