BBCsdl datelib

Discussions related to the code libraries supplied with BB4W & BBCSDL
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

BBCsdl datelib

Post by svein »

The calender library for BBCsdl do not currently have the FN_date$() function.
I came across the need for it, so i wrote the function, english version only.
To my surprise, it is faster than the original one.
Testing code included.

Code: Select all

      REM test......................................
      INSTALL @lib$+"datelib"
      D%=FN_today
      PRINT FN_date$(D%,"d")
      PRINT FN_date$(D%,"dd")
      PRINT FN_date$(D%,"ddd")
      PRINT FN_date$(D%,"dddd")
      PRINT FN_date$(D%,"M")
      PRINT FN_date$(D%,"MM")
      PRINT FN_date$(D%,"MMM")
      PRINT FN_date$(D%,"MMMM")
      PRINT FN_date$(D%,"y")
      PRINT FN_date$(D%,"yy")
      PRINT FN_date$(D%,"yyyy")
      PRINT FN_date$(D%,"ddd dd MMM yyyy")
      PRINT FN_date$(D%,"dddd dd MMMM yyyy")
      PRINT FN_date$(D%,"yyyy ddd dd MMM")
      PRINT FN_date$(D%,"yyyy dddd dd MMMM")
      PRINT FN_date$(D%,"dddddMMMyyyy")
      PRINT FN_date$(D%,"ddddddMMMMyyyy")
      PRINT FN_date$(D%,"yyyyMMdd")
      PRINT FN_date$(D%,"yyyyddMM")
      PRINT FN_date$(D%,"ddyyyyMM")
      PRINT FN_date$(D%,"dyyM")
      PRINT FN_date$(D%,"dMyy")
      PRINT TIME$
      END
      REM test......................................

      REM Return a formatted date string
      DEF FN_date$(M%,f$)
      LOCAL r$,day$(),month$(),b$,N%
      DIM day$(6),month$(12)
      day$()="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
      month$()=" ","January","February","Mars","April","May","June","July","August","September","October","November","December"
      REPEAT
        CASE LEFT$(f$,4) OF
          WHEN "dddd" : r$+=day$(FN_dow(M%)) : N%=5
          WHEN "MMMM" : r$+=month$(FN_month(M%)) : N%=5
          WHEN "yyyy" : r$+=STR$(FN_year(M%)) : N%=5
          OTHERWISE
            CASE LEFT$(f$,3) OF
              WHEN "ddd"  : r$+=LEFT$(day$(FN_dow(M%)),3) : N%=4
              WHEN "MMM"  : r$+=LEFT$(month$(FN_month(M%)),3) : N%=4
              OTHERWISE
                CASE LEFT$(f$,2) OF
                  WHEN "dd"   : r$+=RIGHT$("0"+STR$(FN_day(M%)),2) : N%=3
                  WHEN "MM"   : r$+=RIGHT$("0"+STR$(FN_month(M%)),2) : N%=3
                  WHEN "yy"   : r$+=RIGHT$(STR$(FN_year(M%)),2) : N%=3
                  OTHERWISE
                    CASE LEFT$(f$,1) OF
                      WHEN " "    : r$+=" " : N%=2
                      WHEN "d"    : r$+=STR$(FN_day(M%)) : N%=2
                      WHEN "M"    : r$+=STR$(FN_month(M%)) : N%=2
                      WHEN "y"    : b$=RIGHT$(STR$(FN_year(M%)),2) : N%=2
                        IF ASC(b$)=ASC("0") THEN r$+=RIGHT$(b$) ELSE r$=r$+b$
                    ENDCASE
                ENDCASE
            ENDCASE
        ENDCASE
        f$=MID$(f$,N%)
      UNTIL f$=""
      =r$
Svein
guest

Re: BBCsdl datelib

Post by guest »

svein wrote: Sat 03 Nov 2018, 22:51 The calender library for BBCsdl do not currently have the FN_date$() function.
I came across the need for it, so i wrote the function, english version only.
There seems to be a flaw in this function; try this program:

Code: Select all

      INSTALL @lib$+"datelib"
      D%=FN_today
      PRINT FN_date$(D%,"dd/MM/yy")
      PRINT FN_date$(D%,"dd-MMM-yyyy")
 
The expected output (e.g. from BB4W) is:

Code: Select all

26/11/18
26-Nov-2018
but your routine is outputting this:

Code: Select all

261118
26111818
Sadly there seem to be multiple problems: the separator characters are not appearing in the output, and in the second example both the month and the year are incorrectly formatted. If the routine can be corrected it would be a valuable addition to the library.
guest

Re: BBCsdl datelib

Post by guest »

guest wrote: Mon 26 Nov 2018, 18:38If the routine can be corrected it would be a valuable addition to the library.
This set me thinking, and I came up with this (the strange spelling of the days is deliberate!). Can anybody break it?

Code: Select all

      DEF FN_date$(J%,d$)
      LOCAL I%,day$(),month$()
      DIM day$(6),month$(12)
      day$()="Sunxay","Monxay","Tuesxay","Wexnesxay","Thursxay","Frixay","Saturxay"
      month$()="","January","February","March","April","May","June","July","August","September","October","November","December"
      I%=INSTR(d$,"yyyy") : IF I% MID$(d$,I%,4)=STR$FN_year(J%)
      I%=INSTR(d$,"yy")   : IF I% MID$(d$,I%,2)=RIGHT$(STR$FN_year(J%),2)
      I%=INSTR(d$,"y")    : IF I% d$=LEFT$(d$,I%-1)+STR$(FN_year(J%)MOD100)+MID$(d$,I%+1)
      I%=INSTR(d$,"MMMM") : IF I% d$=LEFT$(d$,I%-1)+month$(FN_month(J%))+MID$(d$,I%+4)
      I%=INSTR(d$,"MMM")  : IF I% MID$(d$,I%,3)=month$(FN_month(J%))
      I%=INSTR(d$,"MM")   : IF I% MID$(d$,I%,2)=RIGHT$("0"+STR$FN_month(J%),2)
      I%=INSTR(d$,"M")    : IF I% d$=LEFT$(d$,I%-1)+STR$FN_month(J%)+MID$(d$,I%+1)
      I%=INSTR(d$,"dddd") : IF I% d$=LEFT$(d$,I%-1)+day$(FN_dow(J%))+MID$(d$,I%+4)
      I%=INSTR(d$,"ddd")  : IF I% MID$(d$,I%,3)=day$(FN_dow(J%))
      I%=INSTR(d$,"dd")   : IF I% MID$(d$,I%,2)=RIGHT$("0"+STR$FN_day(J%),2)
      I%=INSTR(d$,"d")    : IF I% d$=LEFT$(d$,I%-1)+STR$FN_day(J%)+MID$(d$,I%+1)
      REPEAT I%=INSTR(d$,"x"):MID$(d$,I%,1)="d":UNTIL I%=0
      = d$
guest

Re: BBCsdl datelib

Post by guest »

guest wrote: Mon 26 Nov 2018, 22:17This set me thinking, and I came up with this (the strange spelling of the days is deliberate!). Can anybody break it?
I'm surprised to have received no feedback, especially from Svein. Nevertheless I've gone ahead and included my function with the new release of BBCSDL (v0.27a), on the basis that even if it has bugs it's no worse than the previous library which had no FN_date$() function at all! I'd still like people to test it as thoroughly as they can and report back any anomalies.
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: BBCsdl datelib

Post by svein »

Sorry, forgot to check for other separators than space.
Easily corrected though.
Needed longer time to figure out the x in the days.
Listing the corrected routine, but i like your version better.
Sorry for late answer, i'm not checking the forum regularly.

Svein

Code: Select all

      REM Return a formatted date string
      DEF FN_date$(M%,f$)
      LOCAL r$,day$(),month$(),b$,N%
      DIM day$(6),month$(12)
      day$()="Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
      month$()=" ","January","February","Mars","April","May","June","July","August","September","October","November","December"
      REPEAT
        CASE LEFT$(f$,4) OF
          WHEN "dddd" : r$+=day$(FN_dow(M%)) : N%=5
          WHEN "MMMM" : r$+=month$(FN_month(M%)) : N%=5
          WHEN "yyyy" : r$+=STR$(FN_year(M%)) : N%=5
          OTHERWISE
            CASE LEFT$(f$,3) OF
              WHEN "ddd"  : r$+=LEFT$(day$(FN_dow(M%)),3) : N%=4
              WHEN "MMM"  : r$+=LEFT$(month$(FN_month(M%)),3) : N%=4
              OTHERWISE
                CASE LEFT$(f$,2) OF
                  WHEN "dd"   : r$+=RIGHT$("0"+STR$(FN_day(M%)),2) : N%=3
                  WHEN "MM"   : r$+=RIGHT$("0"+STR$(FN_month(M%)),2) : N%=3
                  WHEN "yy"   : r$+=RIGHT$(STR$(FN_year(M%)),2) : N%=3
                  OTHERWISE
                    CASE LEFT$(f$,1) OF
                      WHEN "d"    : r$+=STR$(FN_day(M%)) : N%=2
                      WHEN "M"    : r$+=STR$(FN_month(M%)) : N%=2
                      WHEN "y"    : b$=RIGHT$(STR$(FN_year(M%)),2) : N%=2
                        IF ASC(b$)=ASC("0") THEN r$+=RIGHT$(b$) ELSE r$=r$+b$
                      OTHERWISE
                        r$+=LEFT$(f$,1) : N%=2
                    ENDCASE
                ENDCASE
            ENDCASE
        ENDCASE
        f$=MID$(f$,N%)
      UNTIL f$=""
      =r$
guest

Re: BBCsdl datelib

Post by guest »

svein wrote: Sat 01 Dec 2018, 22:34 Sorry for late answer, i'm not checking the forum regularly.
Perhaps I can take this opportunity to remind you that you can set up email notifications, either per thread (click on 'Subscribe topic' below) or per sub-forum or for the entire board. That way you don't need to "check" the forum at all, you will be automatically alerted to any new posts.
svein
Posts: 58
Joined: Tue 03 Apr 2018, 19:34

Re: BBCsdl datelib

Post by svein »

Thank you for the reminder.
Svein