Announcement

Collapse
No announcement yet.

Validate date function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    you have to know the date format ( eg dd/mm/yyyy or mm/dd/yyyy) which has to have the year including the century.
    It's probably better to say "four-digit-year" than "year including the century" if you work with Oracle DB functions.

    Oracle lets you request "century" as a formatting character... let's say we wanted 12/06/2017 (today).

    While you might think the format string would be "MM/DD/CCYY" formatting SYSDATE() [ meaning "now") that way would return "12/06/2117" ... after all, it is the twenty-first century!

    You format "MM/DD/YYYY" if you want 12/06/2017

    Summary:
    Date (today) including century = 12/06/2117
    Date (today) including four-digit year = 12/06/2017

    Reference: https://docs.oracle.com/cd/B19306_01...nctions180.htm

    MCM
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mmattias@talsystems.com
    http://www.talsystems.com

    Comment


    • #22
      And of course there is the other problem that you have to know the date format ( eg dd/mm/yyyy or mm/dd/yyyy) which has to have the year including the century.
      Those people that like to use dates should, IMO, adjust the date to YYYY/MM/DD, the longest time period first, second longest second, and shortest last. We don't seem to have any trouble with HH:MM:SS, particularly if one uses the 24 hour clock.
      One can get rid of the '/' and ':' if one wishes and 20171206151500 is the approximate time of this post.
      Rod
      "To every unsung hero in the universe
      To those who roam the skies and those who roam the earth
      To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

      Comment


      • #23
        Originally posted by Kerry Farmer View Post
        And of course there is the other problem that you have to know the date format ( eg dd/mm/yyyy or mm/dd/yyyy) which has to have the year including the century.
        Bear in mind that there are many more legitimate date formats than just your two examples. Which is why the OP's question about a "built in Validate date function" is naive in the extreme.

        SQL Server, for instance, has some 30 different built in date/time formats that it can return. Good luck with building a validation function to handle just those.

        FWIW, the most common date formats I use for display/output are "d mmm yyyy" and "yyyymmdd", for user input I generally go with "d/m/[yy]yy" (epoch dependent on application). Just validating those few has a number of complexities.



        --
        [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
        PNG Domain Hosting[/URL]

        Comment


        • #24
          Those people that like to use dates should, IMO, adjust the date to YYYY/MM/DD...
          In the apps where I have to carry or store dates, I am rather partial to FILETIME and I just format what I need when I need it. Works fine with/without time and there are functions already available to compare (CompareFileTime()) and format (GetDateFormat()/GetTimeFormat() if needed.

          MCM
          Michael Mattias
          Tal Systems Inc.
          Racine WI USA
          mmattias@talsystems.com
          http://www.talsystems.com

          Comment


          • #25
            Originally posted by Rodney Hicks View Post
            Those people that like to use dates should, IMO, adjust the date to YYYY/MM/DD, the longest time period first, second longest second, and shortest last. We don't seem to have any trouble with HH:MM:SS, particularly if one uses the 24 hour clock.
            One can get rid of the '/' and ':' if one wishes and 20171206151500 is the approximate time of this post.
            Yep, it's pity that BASIC predates ISO 8601 and was created in the US
            Life would have been so much simpler if DATE$ returned YYYYMMDD

            --
            [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
            PNG Domain Hosting[/URL]

            Comment


            • #26
              SQL Server, for instance, has some 30 different built in date/time formats that it can return. Good luck with building a validation function to handle just those.
              On the "validate a string to be a valid date" thing....

              Mercator (later bought and renamed (Ascential) DataStage TX and bought again and is now (IBM) Websphere Transformation Extender ("WTX")) has a function like that, except you pass the string value and the expected format which is the format against which "string" will be validated. .

              It's kind of a complement to "GetDateFormat" in which you pass a systemtime and a string format such as 'YYMMDD' and get back the string, except when using the FROMDATETIME() function you pass the string value and the expected format... eg you might pass "030217" and "MMDDYY" in the USA but "020317" "DDMMYY" in Europe to validate March 2 2017.

              MCM
              Michael Mattias
              Tal Systems Inc.
              Racine WI USA
              mmattias@talsystems.com
              http://www.talsystems.com

              Comment


              • #27
                Originally posted by Michael Mattias View Post

                In the apps where I have to carry or store dates, I am rather partial to FILETIME and I just format what I need when I need it. Works fine with/without time and there are functions already available to compare (CompareFileTime()) and format (GetDateFormat()/GetTimeFormat() if needed.

                MCM
                Me too. I've got a DateTimeFunctions.inc with a load of tried and tested functions for manipulating FILETIMEs and SQLTools TIMESTAMP structures..
                --
                [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                PNG Domain Hosting[/URL]

                Comment


                • #28
                  Me too [uses FILETIME structure to store date-time values]
                  FOR THE RECORD AND THE NEWER USERS:

                  We used the Windows FILETIME structure and Windows' API date-time functions in our programs because there was then no such thing as PowerTime in the PB compilers. PowerTime does not offer any kind of permanent storage but can provide many date-time services within a program.

                  Michael Mattias
                  Tal Systems Inc.
                  Racine WI USA
                  mmattias@talsystems.com
                  http://www.talsystems.com

                  Comment


                  • #29
                    psssst - in post #8 she converts the date she created in string (code not shown) to three LONG variables for month, day and year). The date string format does not matter for the valid date check. Anne wrote both the not shown code, and the try at validating in post 8, so I'm going with the string format matching in the two places.

                    new thought -
                    (hinted at by someone else on first page I think)

                    Why not generate a valid random date to start with?

                    LOCAL MM, YY, DD, MaxDD AS LONG '<== psuedo code starts
                    'somekind of RANDOMIZE here
                    MM = RND(1, 12)
                    YY = RND(1972, 2099) 'or whatever range you want
                    ''''''put your day code (like corrected post 8 code) here ''''''
                    '''''and put value into MaxDD
                    DD = RND(1, MaxDD) 'now DD is a random day number which is already valid for the random year and month picked
                    put the longs into string now if you want.

                    Cheers,
                    Dale

                    Comment


                    • #30
                      Ok, so the concept of using PowerTime for this chore (much of the required wheel has been invented there), along with having the ability to check various possible date formats intrigued me. So I did this in PB/CC, so you'll have to wrap your own PB/WIN main function around it. It should accept inputs like 01-01-2017, 2017-01-01, Jan 01 2017, jan 1st 2017, etc. I didn't give a painfully thorough test, but I think it's fairly close:

                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      
                      %True = -1
                      %False = NOT(%True)
                      
                      FUNCTION PBMAIN () AS LONG
                      
                          LOCAL tDate AS STRING
                      
                          DO
                            CON.LINE.INPUT "Enter a date, leave blank to end> ", tDate
                            IF LEN(tDate) = 0 THEN EXIT LOOP
                            CON.PRINT USING$("& & a valid date",tDate,IIF$(isValidDate(tDate),"IS","IS NOT"))
                            CON.PRINT ""
                          LOOP
                          CON.PRINT "All done, press a key"
                          WAITKEY$
                      
                      END FUNCTION
                      
                      
                      FUNCTION isValidDate(BYVAL tDate AS STRING) AS LONG
                      
                          LOCAL p1, p2  AS LONG
                          LOCAL tString1, tString2     AS STRING
                          LOCAL lMonth, lDay, lYear   AS LONG
                      
                          LOCAL MyTime AS IPOWERTIME
                      
                          LET Mytime = CLASS "PowerTime"
                      
                          tDate = SHRINK$(tDate,"- /,")
                      
                          tString1 = LEFT$(UCASE$(PARSE$(tDate,"-",1)),3)
                          p1 = INSTR("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",tString1)
                          tString2 = LEFT$(UCASE$(PARSE$(tDate,"-",2)),3)
                          p2 = INSTR("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC",tString2)
                      
                          ' apr 01 2017
                          IF p1 > 0 THEN
                            lMonth = (p1 \ 3) + 1
                            lDay = VAL(PARSE$(tDate,"-",2))
                            lYear = VAL(PARSE$(tDate,"-",3))
                          ' 01 apr 2017
                          ELSEIF p2 > 0 THEN
                            lMonth = (p2 \ 3) + 1
                            lDay = VAL(PARSE$(tDate,"-",1))
                            lYear = VAL(PARSE$(tDate,"-",3))
                          ' 2017-04-01
                          ELSEIF INSTR(tDate, "-") > 3 THEN
                            lYear = VAL(PARSE$(tDate,"-",1))
                            lMonth = VAL(PARSE$(tDate,"-",2))
                            lDay = VAL(PARSE$(tDate,"-",3))
                          ' 04-01-2017
                          ELSE
                            lMonth = VAL(PARSE$(tDate,"-",1))
                            lDay = VAL(PARSE$(tDate,"-",2))
                            lYear = VAL(PARSE$(tDate,"-",3))
                          END IF
                      
                          'optional 4 digit century adaptation
                          SELECT CASE lyear
                            CASE 0 : lYear = 2000
                            CASE 71 TO 99: lYear += 1900
                            CASE 1 TO 70: lYear += 2000
                          END SELECT
                      
                          IF (lMonth = 0) OR (lDay = 0) THEN
                            FUNCTION = %False
                          ELSE
                            ' Can PowerTime digest it?
                            MyTime.NewDate lYear, lMonth, lDay
                            FUNCTION = (OBJRESULT = %S_OK)
                          END IF
                      
                          LET MyTime = NOTHING
                      
                      END FUNCTION
                      Real programmers use a magnetized needle and a steady hand

                      Comment


                      • #31
                        Julian day is the continuous count of days since the beginning of the Julian Period. Using Julian dates is a great way to generate date test data, because each number represents a valid date.

                        See also the fine work of Egbert Zijlema here: http://zijlema.basicguru.eu/ and in these forums.

                        And look up Zeller's congruence!

                        Comment


                        • #32
                          Originally posted by Anne Wilson View Post
                          Is there a built in Validate date function in PowerBasic ?

                          For instance, the date Nov 31 2017 is an invalid date.
                          or Feb 30 2017 is also an invalid date.

                          I need a function to tell me that a random date computed by a program is invalid and therefore would need
                          another random date.
                          Why not generate a random number and convert that number to a date?
                          The world is strange and wonderful.*
                          I reserve the right to be horrifically wrong.
                          Please maintain a safe following distance.
                          *wonderful sold separately.

                          Comment


                          • #33
                            Side note: Sadly it appears we've lost Egbert https://www.mensenlinq.nl/overlijden...ijlema-4970227
                            <b>George W. Bleck</b>
                            <img src='http://www.blecktech.com/myemail.gif'>

                            Comment


                            • #34
                              Originally posted by Kurt Kuzba View Post

                              Why not generate a random number and convert that number to a date?
                              Already suggested and rejected by the OP.
                              --
                              [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                              PNG Domain Hosting[/URL]

                              Comment


                              • #35
                                > Egbert

                                Oh that is sad. Anybody set up to OCR and translate the text in that image? I'd like to be able to read that.
                                "Not my circus, not my monkeys."

                                Comment


                                • #36
                                  Eric Pearson I don't want to derail this thread further so I posted a translation here: https://forum.powerbasic.com/forum/c...egbert-zijlema
                                  <b>George W. Bleck</b>
                                  <img src='http://www.blecktech.com/myemail.gif'>

                                  Comment


                                  • #37
                                    Originally posted by Stuart McLachlan View Post

                                    Already suggested and rejected by the OP.
                                    I reread the thread and cannot find where that is suggested, actually.
                                    What I had in mind is to take your target range, say from July 17, 2001 to August 21,2005,
                                    and use Powertime to arrive at two quad integers representing those dates.
                                    Then take their difference, and derive a random from 0 to the maximum.
                                    Add that value to the lower date quad and derive your date from the resulting number.
                                    The world is strange and wonderful.*
                                    I reserve the right to be horrifically wrong.
                                    Please maintain a safe following distance.
                                    *wonderful sold separately.

                                    Comment


                                    • #38
                                      Originally posted by Kurt Kuzba View Post

                                      I reread the thread and cannot find where that is suggested, actually.
                                      What I had in mind is to take your target range, say from July 17, 2001 to August 21,2005,
                                      and use Powertime to arrive at two quad integers representing those dates.
                                      Then take their difference, and derive a random from 0 to the maximum.
                                      Add that value to the lower date quad and derive your date from the resulting number.
                                      See posts 13 to 16.

                                      Personally, I would do it with Julian Day Numbers rather than Powertime Quads given the precision of RND (Single or Long).
                                      i.e. Convert target date boundaries to JDNs, derive a RND in that range and then convert the JDNs back to whatever date format is desired.
                                      --
                                      [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
                                      PNG Domain Hosting[/URL]

                                      Comment


                                      • #39
                                        Here is a link to doing everything with PowerTime and also check out Jose Roca's method.
                                        https://forum.powerbasic.com/forum/u...g-days-to-date

                                        Comment

                                        Working...
                                        X