Author Topic: TimeStamp  (Read 694 times)

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
TimeStamp
« Reply #15 on: July 17, 2011, 11:27:27 pm »


               Or, if you're running NWNX, you can use MySQL and leave the ints in string form in nwscript, doing the calcs in MySQL - it's 64 bit, rather than 32. Frankly though, you're WAY overthinking this.

Funky
               
               

               


                     Modifié par FunkySwerve, 17 juillet 2011 - 10:29 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
TimeStamp
« Reply #16 on: July 18, 2011, 01:29:03 am »


               traditionally when you are talking about a time stamp, you are talking about an amount of time from a given date.  For example here are few of the systems currently in use.   

 UNIX Timestamp:  seconds since Jan 1 1970    ex.   Jan 1 1970   1:00am =  3600  or  60*60 (Min *Sec)
 Mac Timestamp:  seconds since Jan 1 1904     es    Jan 1 1904   2.00am  = 7200 or  120*60 ( (min*Sec)
both of  thies are stright forward  where the time stamp is an unsinged intenger of the number of seconds from the given date. .



here is the Mircrosoft time stamp.
Microsoft Timestamp:  days since Dec 31 1899

note:  mircrosoft does not use the number of seconde from the given date.  Instead it it the number of days.   inorder to get an acurate time they use a double pricesion float, where the mumber of days is represented by whole numbers and the time during the day is representsd the fraction of the number.   so:

January 1, 1900  12:00Pm  would have a time stamp of    0.5  or   half after december 31 1989.
January 2, 1900  6:00Pm  would have a time stamp of    1.75   or 1 and three quraters of a day  after december 31 1989.


The time stamp you are devloping is doing basicly the same thing, you just have not added a way for a starting date that is higher then  jan 1 year 0.  but I have not really answered you question yet.  

to simplify it a little lets just look at a stamp for nwn that would be the number of days  and we will still use year 0 for simplisity.   So we would have. 

//multiplyers:
int nmlDay     =    1;   //there is 1 day per day.
int nmlMonth = 28;  //There are 28 days per month.
int nmlyear    =436  // there are 336 days per year.  

// now lets create a time stamp with this without using the -1's that I included.  lets take: 
year 0.
month 12 
day 28

Our time stamp is :  12 * 28  + 28  =  364 

now lets try and get our number of years, months and days back out of the stamp.  

lets see if we devide our number by the number of years we should get our  year.   

364 / 336    =  1 with a remainde of  28.     so we have 1 year. 

 now if we take the remainder and devide it by the number of days per month we should get the month. with the remainder being the number of days.    

28/28 =   1 remainder  0 

so pulling our date back out of the stamp we get 
year 1 
month 1 
day 0 

Hmm it does really what we where looking for.    The problem is that we are using a day of 1 to 28 in our equasions   where 28 is also 1 month.  and using  1-12 months  where 12 months is also 1 year.    So that is how they are returned to us.  not to mention that our numbering system  is somehow missing the fact that I can be one be  28 day away from my start date.   

Again for simplicity lets look a a different date to find the problem.  lets say: 

year 0 
month 1 
day 1 
hour 12:00pm  // yea I know I gave  it a time.   

with the date given above what shoud our time stamp be if it is the number of days form year 0.  
well it is still in the first day.   If we had decimal places  it would be 0.5,  half a day.   The fact is that on day 1 month 1  a day has not yet passed and a month surly has not yet passed.   To simplify getting the same date out of a time stamp that we put into the time stamp we change the way the months and days are numbered from  days: 1-28  to days 0 -27.  or just day-1  
and for months from   1- 12  to 0-11  
notice that  1-12 and 0 -11 still has the same nunber of  members in the set.  { 1,2,3,4,5,6,7,8,9,10,11,12] and [0,1,2,3,4,5,6,7,8,9,10,11]  both sets have 12 members.   the 0-11 is the preferred usage because it is 0 based  making it so the upper limit does not (11)  does not get confused with a full year like the upper limit of the other set (12) would.   


so lets look back at our orginal try that returned the wrong date when we tryed to convert it back from the stamp.  
year 0.
month 12 
day 28 

if we are in month 12 , 12 months have not yet passed .  11 months have passed putting us in the 12 month.  
on day 28 only 27 days have fully passed.  

 so we now have 
(12-1) *28 + (28-1)  = 335 days since year 0 

ok let convert it nack to a year/ month/day 

stamp /  the number days in a year should give us the number of years with a remainder of days into the year.  

335/ 336 = 0 remainder of 336.   
ok so far so goor we get year 0.  

now when we divide the 'days into the year'  by the number of days in a month we should get the number of  month that have passed.  with a remander of days that have passed into the month.   

335/28 =  11  with a remainder of 27  

now that is not  quite the number we started with.   Just keep in mind what the 11 and 27 are.  they are the munber months and days that have passed.   if 11 months have already passed  we are not in month 11, we are in month 12.    So the month we are in will always be  "the months that have passed +1"   same thing with the days.  

so we now have 
(335/28) +1  = our month.  
 our day is the remainder (27 +1) =  day 28.  


now let me try and work in the modulo '%" operator.  

the only thing the modulo does is return the remainder of the division.  

so in our last division there where we had the nimber of days into the year.   335 of them 

335 / 28 = 11  
now if we need to find the remainder for our number of days  wy can either  muntiply 28 by 11 and then subtract it from our original 335  
335 -  (28*11)  = 27 

or we can just use the modulo to give us the remainder. 

335 % 28  = 27


I hope that explains everything. 
L8   


               
               

               
            

Legacy_Epitaffio

  • Jr. Member
  • **
  • Posts: 70
  • Karma: +0/-0
TimeStamp
« Reply #17 on: July 18, 2011, 11:15:00 am »


               Woah, long and perfect, thanks for the time L8!
               
               

               
            

Legacy_Epitaffio

  • Jr. Member
  • **
  • Posts: 70
  • Karma: +0/-0
TimeStamp
« Reply #18 on: July 18, 2011, 12:41:58 pm »


               I think i must share the last edited function for future reference


    int iMinPerHour = FloatToInt(HoursToSeconds(1)/60);
    int iMinPerDay = 24*iMinPerHour;
    int iMinPerMonth = 28*iMinPerDay;
    int iMinPerYear = 12*iMinPerMonth;

    int iMinute  = GetTimeMinute();
    int iHour    = GetTimeHour() * iMinPerHour;
    int iDay     = (GetCalendarDay()-1) * iMinPerDay;
    int iMonth   = (GetCalendarMonth()-1) * iMinPerMonth;
    int iYear    = GetCalendarYear() * iMinPerYear;

    int iTimestamp = iMinute + iHour + iDay + iMonth + iYear;



Here the decoding:

    int iTestYear = iTimestamp/iMinPerYear;
    iTimestamp = iTimestamp%iMinPerYear;

    int iTestMonth = iTimestamp/iMinPerMonth +1;
    iTimestamp = iTimestamp%iMinPerMonth;

    int iTestDay = iTimestamp/iMinPerDay +1;
    iTimestamp = iTimestamp%iMinPerDay;

    int iTestOre = iTimestamp/iMinPerHour;
    iTimestamp = iTimestamp%iMinPerHour;

    int iTestMinuti = iTimestamp;
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
TimeStamp
« Reply #19 on: July 18, 2011, 09:22:28 pm »


               looks good.
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
TimeStamp
« Reply #20 on: March 06, 2012, 12:38:58 am »


                Since this was linked to recently I thought it would be worth while to share my time functions:
http://pastebin.com/xnJMcHFR

I saw the comment above that game hours are not necessarily equalt to 60 game minutes. This is true. I create a constant in my modules that represents a conversion from game minutes to real minutes. So if a game hour is only 2 game minutes, the value of my constant is 30 (meaning 30 real minutes per game minute).