Robert Newson wrote:

> Dilwyn Jones wrote:
> 
>>A help request:
>>
>>Is there a way of returning the current month as a number?
> 
> I had thought I had come up with a function for this, but it is getting the 
> ends of some months wrong, eg 1 May is coming out as 31 Apr! (Which seems 
> odd as I thought I had checked out all the month bounds when writing the 
> function...I'll have another look at it tomorrow.)

Had a fiddle with it this evening, and seem to have solved the problems 
(I've checked it against the QL's DATE$() and it's matched as far as today 
from 1 Jan 1961):

DEF PROC my_date(secs, dy, mn, yr)
    dy = INT(secs / 86400) + 306
    yr = INT((dy + .8) / 365.25)
    dy = dy - INT(yr * 365.25) + 31
    mn = INT(dy / 30.6)
    dy = dy - INT((mn + 3) * 30.6) + 92
    mn = mn + 2
    IF mn > 12 : mn = mn - 12 : yr = yr + 1
    yr = yr + 1960
END DEF

DEF my_mth_no(secs)
    LOC dy, yr, mn
    dy = INT(secs / 86400) + 306
    yr = INT((dy + .8) / 365.25)
    mn = INT((dy - INT(yr * 365.25) + 31) / 30.6) + 2
    IF mn > 12 : mn = mn - 12
    RET mn
END DEF


If you want an explaination of how they [supposedly] work, I'll be more than 
happy to [try and] explain the algorithm.


my_date has been made a PROCedure which modifies its calling parameters as 
more than 1 variable has to be set:

     my_date secs, day, month, year

secs  = number of secs since 01.01.1961 00:00:00 of date to extract
day   = stored with day of month (1-31)
month = stored with month number (1-12)
year  = stored with year (1961-2099)[1]

[1] Not checked beyond today.  2100 would be wrong (after 28.02.2100) as the 
proc/fn doesn't realise it will not be a leap year; however, for the 
required app, I don't think that would be too much of a problem.

I could have set a string and made it a function, but the whole point of the 
exercise was that you were trying to extract the day/month/year from the 
DATE$ (if I understood you properly) and it would have been a bit silly to 
package them up in a string to be unpackaged again afterwards.  An 
alternative would be to add the three elements together, eg:

     packed = (year - 1961) * 372 + (month - 1) * 31 + day - 1

Then:

     day   = packed MOD 31 + 1
     month = INT(packed / 31) MOD 12 + 1
     year  = 1961 + INT(packed / 372)

my_mth_no takes one param: secs since ~ and returns the month number of that 
date:

     month = my_mth_no(secs)


Any help?


_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm

Reply via email to