1
$\begingroup$

In programming, we use a start date and count the milliseconds since that date, to calculate the 'time since epoch' (which is 1970 Jan 1st 00:00:00 I believe). A class called DateTime does all the math for us. Searching for HOW DateTime does this math usually yields helpful answers like "Just use DateTime". This unfortunately doesn't help me if I want to use the math to calculate date and time of a different timeframe in which time runs faster.

What I'd like to know is what's the math that goes in to figuring date time? Date Time being the Day, Month, Year. Hours and minutes in a day, optionally the seconds. The day of the week may also be figured out from the milliseconds since epoch (somehow).

The problem I'd like to solve is this: There is 3.6 of our minutes in a game hour. (I picked this with the assumption that I can easily figure out the angle of the minutehand on a clock with 3.6min to an hour). Lets call this accelerated timeframe "game time". While only 3.6 minutes passes our time, in game time 60 minutes would pass.

This should means an hour in game time is 216000 of our milliseconds long. A minute should be 3600ms long. A second should be 60ms long. A Day should be 5184000ms long. That's assuming I did the math right. If I'm off feel free to correct me.

Let us assume the 'epoch' of this gametime begins at 1970 Jan 1st 00:00:00.

If I have a number of milliseconds since Epoch, how would I find the year, date, and time? Assume the world of the game doesn't have leap years or leap seconds. The planet is in perfect orbit that takes exactly 365 days to go around the sun, and the planet spins 360 degrees every 24 hours exactly.

My assumption is that modulus and division is needed to figure this out, but I get lost along the way and I don't get reasonable answers.

My assumption was that I could take the number of milliseconds passed, say 1485902668429ms (which was picked based on the time I queried for milliseconds since epoch) and modulo that by (the number of ms for an hour times 24, times 365), and that would get me the number of years since 1970. But the number I get is ridiculously big, which I suppose means that it's time in milliseconds still?

  • 0
    What I'd like to do, once I understand the math, is code a library that lets the user select their time frame, epoch start date, and plug in the number of milliseconds passed to figure out what time and date it is in their own games. This would be very handy for game programmers who want to have an in game calendar for example.2017-01-31
  • 0
    So, is this a universal time that happens whether you are playing or not or do you always start at some point?2017-02-01
  • 0
    "My assumption was that I could take the number of milliseconds passed, say 1485902668429ms (which was picked based on the time I queried for milliseconds since epoch) and modulo that by (the number of ms for an hour times 24, times 365), and that would get me the number of years since 1970." No, that would give you the number of milliseconds since new years minus then numbers of leap days since 1970. You need to divide it and round to the nearest year.2017-02-01
  • 0
    miliseconds mod milliseconds in year = milliseconds since new years. milseconds/milliseconds in year = years since 19702017-02-01
  • 0
    @fleablood It would depend on how it's implemented. I would have it so it counts the number of milliseconds that have passed while the game is running. The count would halt when the game is paused or closed, and resume when it starts again. Also, we can assume there are no leap anythings in the game time, time flows at exactly 1000ms in a second, 60s in a minute, 60min in an hour, 24hr in a day, 365days in a year. In game time of course.2017-02-01
  • 0
    I have a general understanding that from milliseconds I can convert to seconds by dividing by 1000, and convert from a second to milliseconds by multiplying by 1000. But it's the wrinkle of resetting the clock back to 00:00:00 every day that gets me. Or that we start at January once 365 days have passed. How would I determine if 123242343242ms is a tuesday or a friday?2017-02-01
  • 0
    Ok so if I understand you right, msSinceEpoch / msInYear = years since epoch? And msSinceEpoch % msInYear = msSinceStartOfYear? So should I do (msSinceEpoch % msInYear) / msInMonth to get the current month in the year? ((msSinceEpoch % msInYear) % msInMonth) / msInDay to get the current day of the current month? I get numbers that look reasonable, but if I change the number of ms for a given time period (hour, month, year) to the approximate length in ms in real time, the values I get back are way off. Except for years since epoch....2017-02-02
  • 0
    Doesn't get date let you enter timestamps and dates. Can't you just scale the miliseconds and feed it into getdate?2017-02-02
  • 0
    You can't convert dates before 1970 using DateTime, for one. The setting I'm writing is in the 50's. Also DateTime doesn't work with irregular calendars. If I wanted a year to last only 120 days, or have only 4 months one for each season, DateTime couldn't give me what I want. As to why I would want 120 days a year, it'd be because I want the player to see multiple years while playing, at 3.6m an hour that would make it about ~127hours for one year versus 300+ hours for a year at 365 days per year.2017-02-02
  • 0
    I've made up a spreadsheet that calculates from a date to time since epoch, and back to a date to test if my math is right. [link](https://docs.google.com/spreadsheets/d/18ZxyBW6_JLQ7m2TylqXPnA08rhIjkUVEOQ41T4q4xtQ/edit?usp=sharing)2017-02-02

0 Answers 0