Date Time Programming Tutorials?

Ok, I’ve put up a post on the Collab forums (despite that this isn’t a typical collaboration request). However, I’m also no stranger to programming myself.

I’m looking to create a… unique time calculator, and I don’t even know where to begin when working with the math involved in months, days, etc.

Does anyone know of some tutorials in this regard. I know how to get the date/time from the computer, which I will need. But I need to work with the math involved in this (basically converting it to a universal, decimal system).

Can anyone help me out with this?

Quick Edit: I will also be working with a broad range of times, ranging from calculating a few seconds to working with tens of millions of years. Now, I won’t need to know ridiculous things (like how many seconds are in a million years), but I will be working with large numbers. Obviously, basic integers will run into some issues with this. So any tips on that front would be appreciated as well.

edit: Sorry, didn’t see your last edit. Timespan is correct for most calculations, but it’s maximum value is10,675,199 days.

Tip 1) Don’t make silly assumptions.

There are ~31556900 seconds in a year. That’s 315.569 trillion per ten million years or 315.569 quadrillion if we’re talking about millisecond accuracy. Sounds like a lot… but it isn’t.

2^64 [ulong] has a range from 0 to 18 quintillion! That’s more than 580Mn years @ ms resolution!

For even huger number ranges, look into BigInt.


The real question is do you just want to muck around with an arbitrary abstract time? If so all you need is numbers. Time only really gets difficult if you try to account for the complexities of our current time mode [leap years, time zones, arbitrary historical events etc] or if you get silly and try to do something with relativity.

Nothing terribly fancy. And, while the time system I’ve come up with is accurate to about 30 seconds, I will never really need it that accurate (within a few centuries would be fine in fact).

The basic problem is that the system I’m using deals with multiple galaxies across large amounts of time. And, since I’m a stickler for realism, and this does tie in with real events on earth, I need to be able to say “This happened on this planet at this ‘Galactic Time/Date’; which is about X million years ago”.

So really, our “earth time” can’t be used, as its based off of the rotation of our planet and solar system. I simply need to tie it in so I can show relation to the events that happened on earth.

Does that make sense?

Division and Modulus.

Wrap that up in some nice structs and you are good to go.

Forgive me if I’m misunderstanding the problem, as I do not fully know what your intended application is, but it strikes me that the math is not so much the problem here as the structure. For dealing with very large spans of time (and distance in your case), where you need to be able to compare say what is happening on different planets at the same time. You will need a couple things to do this. First, you must understand you are creating a new calendar here, and that requires a few things of it’s own. Second you need to create a convenient way to display the time and perform math on it. I’m going to tackle these one at at time. Please bear with me me for the following as I’m about to completely geek out, I’ve spent way too much time thinking about time, and this looks fun to me.

First the calendar:

The first thing you need when creating a new calendar is a common point of reference. Every calendar must reference some past event as its start point; it’s epoch. For the modern Gregorian calendar used in the west, this is generally the birth of Christ, or the beginning of the “Common Era.” Since that is meaningless to someone living on another planet, you can throw that right out. The only reasonable reference point I see if we’re creating an intergalactic calendar would be the Big Bang. It’s the only universal reference point when you’re crossing galaxies. The Big Bang is estimated to have occurred about 13.7 billion years ago, so let’s start our new calendar there. In fact, let’s pretend it’s exactly 13.7 billion years, as of January 1 1900, just to make life easy. If you’re wondering why I chose that date, it’s just because that’s the date excel serializes dates from, so it’s one I’m used to working with. I didn’t say my reasons were great, I just said I had them.

Second, we need a standard unit of time with which to count and all it’s attendant subdivisions. Of course, in reality an earth day and year are totally meaningless to someone on another planet. However, since your game will be played by people on Earth (I presume), and it sounds like events in earth history will be frequently referenced, I think it would be forgivable to stick with a 24 hour day. However, since this is an intergalactic calendar, we have no cause to try to sync the calendar with the passing of seasons on any given planet so let’s not worry even trying to do so. For our purposes computational ease is far more important, especially considering you only need to be within a few centuries of a given date.

Given the above, our current arrangement of days and months with it’s odd and varying length of month etc does not lend itself to easy math. Or really even to easy numerical display. So we’ll just throw out the 12 month year entirely. Instead let’s use a 13 month year each month consisting of 28 days, and each day consisting of 24 hours. This gives us exactly 364 days per year, which does not line up with the earth year (365.242199 days per year). Were our calendar used on earth it would “wander” through the seasons. The months falling in spring this year, would eventually be in winter. However, this is the case with many real calendars here on earth (the islamic calendar and the Mayan calendar both suffer from the same defect) so I think it’s acceptable considering we’re not thinking only about earth here and those other calendars are!

No need to muck with anything lower than hours, it can all stay as it is in the regular time schema, nor anything larger than years. Centuries (100 years), millenia (1000 years), epoch (1 million years) and aeons (1 billion years) are all fine and quite easy to do math on .

Okay, so if that’s all established, now we need a convenient way to display our new calendar and do math on it. First displaying our new time system. What we’ve created above can be displayed in a type of dot notation, though of course not everything works on the same base here. Our format will be:

aeons.epoch.millenia.century.years.months.days

Therefore, if we wanted to display today’s date using our new calendar and display schema (September 9th, 2012) it would look like this:

13.700.0.1.12.13.27

Since our calendar starts at the big bang, that would be 13 billion, seven hundred million, one hundred, twelve years, thirteen months and 27 days. (remember, the big bang happened Exactly 13.7 billion years before January 1, 1900). We could call this the galactic date (catchy huh?).

Ok, now for the math. To find the length of time between any two dates, you just subtract each position of one date from each position of the other. So if we wanted to know how long between today and say, Febuary 3rd 1936, we would just convert that second date into Galactic Date Format (GDF) and subtract as follows:

2/3/36 in GDF: 13.700.0.0.36.2.6

Therefore:

13.700.0.1.12.13.27

  • 13.700.0.0.36. 2. 6

=

0.0.0.0.76.11.21

So the difference (in Galactic Standard Time, GST) is 76 years, 11 months 21 days. If as is the case with the years place in our above problem, the simple math would produce a negative number, you just borrow from the next higher place, much as you do in regular subtraction.

As an interesting aside, our new calendar now works in a way very similar to how the Mayan Long Count works.

So how do we make this work programmatically? Write a GalacticTime class, give it a unique namespace so it’s easy to use, and build your necessary functions and constructors there. Off the cuff I would suspect you would need a compare function, an add, subtract, and display function. It would perhaps also be useful to write a constructor that takes a date in the normal gregorian format and converts it into Galactic Time. You’ll also write your logic here for doing the math of course. I would suggest just having 7 int variables with the appropriate names (epoc, millenium etc), and having the constructor test to make sure each variable is within the acceptable range. This then makes all the math very simple.

From that point on whenever you need to do something with Galactic time, you just instantiate a new GalacticTime object and do whatever you need to do. The plumbing is all nicely kept out of your way in the GalacticTime class.

Let me know if this helps you out. If you want more percision with earth dates, I can help you with that, but the system will be somewhat more complex. If you’re not comfortable writing your own classes and constructors (it’s not that hard really), let me know and I might be able to find the time to write the script for you, it won’t really be very complicated. I won’t charge much for it, maybe we could even do a trade of services of some sort.

Cheers!

I guess you either have to give up being a sticker to realism or it will be interesting as it also involves the theory of relativity if done right, galaxies, solar systems, planets, all are moving with different speeds.

Really a good point actually. The idea of time passing at the same rate in different galaxies is in fact entirely fallacious. I’m not even sure if the differences in the rate of passage of time could be worked out in any meaningful way. We’ll have to leave that one to the cosmologists. The best you can do however, is about what khanstruct seems to be doing (or at least what I think he seems to be doing), which is using time centered on a certain place (earth), and using that as the central reference point. Time is such an interesting subject because it’s both completely arbitrary and at the same time completely influenced by outside factors. It makes my headasplode.

Wow, bluerededge, I think you and I would get along famously. And yes, being a huge science nerd (fascinated by quantum physics, theoretical physics, etc.) I’ve taken all of these various elements into consideration.

Yes, I do have the common basis at the Big Bang. However, the Big Bang is actually 5 million years before the “zero” point of the calendar; the zero point being the creation of the first galaxy (13.2 billion years ago), since before that, there wouldn’t be anything to measure or any creatures to measure it. The basis of time is then based on the rotational speed of a fictional galaxy (the Starfall Galaxy), which is about 100 million years per rotation.

NOTE: The Milky Way takes 250 million years for one rotation.
ALSO NOTE: The Starfall Galaxy was not the first galaxy. It was just home to the species that will have created this form of galactic measurement.

So, the largest increment on the calendar is 1 Galactic Cycle (100 million years).

Here’s how I have things worked out so far. (This is an image from my docs)

Obviously, the earth years, hours, etc., are all estimations, which gets dangerous across billions of years.

To give an example, the first dinosaurs appeared on earth about 245 million years ago. This would be: GC 108.690.000.000.000.000 (the zeros are just assumed, since I don’t really care about the birthday of the first t-rex); so it could actually be read, Galactic Cycle 108.69.

When dealing with smaller increments, I typically use KC (Kilocycles), as that’s accurate up to a millennium.

In the end, yes, the actual date on earth is entirely irrelevant. What matters is the measure of time that has passed since the beginning of the universe.

Ideally, I would love to have this as a widget on part of our website, so users could see Galactic Time passing in real time, giving them a better sense of immersion in the games. I’ve been getting some great interest in PMs as well. If anyone else would like to be involved in this, I would love to hear from you.

Thanks again!

@blurededge
Well, it’s all just a matter of how far you want to take it. But i like the idea of having a game with relativistic math behind. As these things are relative and considering that time on earth also isn’t passing in a constant way as we aren’t moving with a constant speed, you might want to take a non moving system as a reference system, from where on you add information about the movement of each component (for example, expansion of the universe, translation of a galaxy, rotation of a solar systems, the paths of planets and so on).

Kind of reminds me how long one second is and how arbitrary (or more an hour as a second is a angle unit), best measured with a pulsar or an optic atomic clock.

I also do appreciate the value of relativity. However, time being inconsistent never truly matters, since you would never, personally notice the difference (unless the flow of time was important while manually traversing the universe, which, in this case, its not).

Or, you would have to use something outside of the fourth dimension in order to “allegedly” tell time accurately (without bias). While, in this storyline, there are things beyond the fourth dimension; they aren’t exactly the kind of folk you’d ask for the time.

My goal is to make a story that can easily entertain the masses, while providing interesting, supporting evidence to root it in reality for those who want to go looking. I’m afraid relative physics is just a bit outside the “fun factor” for the general population.

Hey, you were the one asking for a time system which works across galaxies. And if people would use such a system they would consider these things as they matter, they already matter on earth when dealing with satellites. More as you seem to observe times spans of millions of years. Obviously they don’t matter if you just want to tell a story, if it’s not about time.

If you’re referring to gravitational time dilation, it barely applies in this case (and the only way it affects satellites is that their internal clocks are sometimes a little off). At best, it would simply define why time flows differently in galaxies with higher mass. And even then, since I’m primarily concerned with “eras” (not specific dates), the fluctuation in time due to gravity is not significant enough to make a difference (even over millions of years).

Now, if I were looking to launch a warp-capable starship, I may consider things like that, but since I’m simply looking to explain events that occurred in order, its not really necessary.

But yes, the story is very much about time, as well as dimensions. Thanks for the thoughts though; I do appreciate all the feedback.

Both, velocity and gravity influence time. I don’t know if you’re familiar with how fast galaxies travell and rotate but it’s significantly more than just a satellite orbiting around earth. A satellite would have a relative difference of about three-fourths of an hour relative to earth in a million years. Not that someone would care if he’s been waiting this long. With galaxies those minutes turn already into days/weeks.

Now if you take gravity into account as well, our satellite might be off more than four and a half hour. Considering that there exist some quite insane gravity fields in space, this can change the days/weeks into a lot, a lot more.

As I had mentioned, I would love to have this built into an upcoming page on my website. Given a little more research and a bit of fancy design, I’ve come up with a design I’m pretty happy with.

Behold! (I love saying behold), the Maha Kalpa (cosmic clock)

So who wants to join me in designing the greatest story ever told? :slight_smile:

NOTE: The actual sanskrit word for time is kala, not kalpa, but “maha kalpa” is an arbitrary, sanskrit measurement of time which covers the creation and destruction of a species. In this case, every species.

And none of you felt the need to point out the error in my original image for the decimal time!?

I mistakenly wrote 10,000 twice! All of my dates have been off by a power of 10! FML

Well, one of the major hurdles has been covered in this project.

I huge thanks goes out to ShaderOp for his work on the clock! He’s an excellent programmer, and also offered some very useful design tips which helped me out.

While the end functionality of the project isn’t there, if anyone is interested in seeing what the time is in Galactic Cycles, check out the Maha Kalpa here.

Once I get some of the interactivity built into this, I’ll open a WIP thread to show off the progress.

Thanks, man. I’m glad to help, and it was a lot of fun.

And if anyone is interested, I’m available for contract work and/or collaboration opportunities. I can’t claim to be an expert in all things Unity, but I can probably do a good enough job for most tasks. Shoot me a PM and we can talk.