1 2 3 4 Previous Next 53 Replies Latest reply on Feb 23, 2016 5:51 PM by user19752

    How to know a variable's type?

      Is there a FMP function that lets you know the variable's type?

       

      For example if I had something like:

       

      VarType(Timestamp("11-11-2015 11:11:11")

       

      It would return "Timestamp".

       

      In an object oriented environment, you might have something like if myvar.instanceOf("Timestamp") ....

       

      ----

       

      Something like being able to know what the type of a variable is probably exists already, but I can't find it. Knowing a variable's (or function return) type would be very useful so you could better know when certain FileMaker functions would apply.

       

      Thanks,

       

      - m

        • 1. Re: How to know a variable's type?
          siplus

          FieldType

           

          Given that you can force a type with GetAs.... I don't really see how you would use it.

          • 2. Re: How to know a variable's type?
            johan

            All variables are always text.

             

            But FileMaker is actually really good at guessing how to convert text into other data types when needed. For instance the calculation "2" + "2" equals 4.

            • 3. Re: How to know a variable's type?

              If a variable isn't a Timestamp or compatible with one (as with the case of Timestamps returned from SQL, for example), then GetAsTimestamp won't work. You just get the ever-nonhelpful: "?".

               

              Case in point, an ExecuteSQL returns this: 2016-02-20 13:50:17

               

              GetAsTimestamp("2016-02-20 13:50:17") returns "?"

               

              FileMaker returned this from a Timestamp field, but you can't use it as a Timestamp without modifying it. I found yesterday that FileMaker actually has three different formats for Timestamps depending on the context.

               

              ---

               

              So, it's not FieldType I need, but a "VarType" function.

               

              Thanks,

               

              - m

              • 4. Re: How to know a variable's type?

                See my reply to Siplus above.

                 

                In the case, for example, where you get a ExecuteSQL result back on a Timestamp column, you can't use it as a Timestamp since it's not the right format.

                 

                But if you had something like GetAsNumber(<expression>) wouldn't that be a "number"? Or, would that still be text?

                 

                Thanks,

                 

                - m

                • 5. Re: How to know a variable's type?
                  johan

                  And all custom functions also only return text.

                  • 6. Re: How to know a variable's type?
                    Mike_Mitchell

                    The problem is the Unix format. Try this:

                     

                    //  Function name: cfUnixTimestampToTextTimestamp

                    // Purpose: To convert a Unix-formatted timestamp text string (such as returned by ExecuteSQL()) to a text representation in US format

                    // Requires cfUnixDatetoTextDate as a helper function

                    // Author: Mike Mitchell, Net Caster Solutions, www.netcastersolutions.com

                    // Original publication date: 12/7/2015

                     

                    Let ( [

                    yearDigits = Case ( yearDigits = 2 or yearDigits = 4 ; yearDigits ; 4 ) ;

                    inputAsList = Substitute ( input ; " " ; "¶" ) ;

                    datePortion = GetValue ( inputAsList ; 1 ) ;

                    timePortion = GetValue ( inputAsList ; 2 ) ;

                    dateTransformed = cfUnixDateToTextDate ( datePortion ; yearDigits )

                    ] ;

                     

                    dateTransformed & " " & timePortion

                     

                    )

                     

                    //  Function name: cfUnixDateToTextDate

                    // Purpose: To convert a Unix-formatted date text string (such as returned by ExecuteSQL()) to a text representation in US format

                    // Author: Mike Mitchell, Net Caster Solutions, www.netcastersolutions.com

                    // Original publication date: 4/25/2013

                     

                    Let ( [

                    yearDigits = Case ( yearDigits = 2 or yearDigits = 4 ; yearDigits ; 4 ) ;

                    inputNumeric = Filter ( input ; "0123456789" ) ;

                    inputYear = Left ( inputNumeric ; 4 ) ;

                    inputYear = Right ( inputYear ; yearDigits ) ;

                    inputMonth = Middle ( inputNumeric ; 5 ; 2 ) ;

                    inputDay = Right ( inputNumeric ; 2 )

                    ] ;

                     

                    Case ( Length ( inputNumeric ) = 8 ;

                     

                      inputMonth & "/" & inputDay & "/" & inputYear ;

                     

                      ""

                     

                    )

                    )

                    • 7. Re: How to know a variable's type?

                      So, to get the the heart of the matter, if FMP returns: "2016-02-20 13:50:17", from an ExecuteSQL statement, how do you know you can use it since this value isn't usable in Timestamp calculations as it's not formatted correctly?

                       

                      I'm asking this question primarily since I realized FileMaker has (at least) three different Timestamp formats depending on context. I spent many hours trying to figure out why I couldn't do simple Timestamp math.

                       

                      Thanks to Nicolai here, I got the answer to that initial question, but this issue still begs the question as to how I can figure out what I actually have returned from FileMaker given that it could be three different formats?

                       

                      Sure, OK, you get text back, but that shouldn't be the end of the story.

                       

                      If you "evaluate" it in some FileMaker (yet unavailable way, apparently) you would know its particular type.

                       

                      So, using my fictitious example function, varType,...

                       

                      VarType(2016-02-20 13:50:17) might be "T" or "U" (Text or Unknown), but


                      VarType(02-20-2016 13:50:17) would return "TS" (Timestamp)


                      -----


                      Hope this clarifies what I was asking.

                       

                      Thanks,

                       

                      -m

                      • 8. Re: How to know a variable's type?
                        Mike_Mitchell

                        ExecuteSQL, as far as I know and have experienced, always returns Unix format. Should be relatively easy to predict in this use case.

                        • 9. Re: How to know a variable's type?

                          Thanks, and sure, I could write a CF, but ....this begs the question why there isn't already a built-in function to convert from one type to one that works?  IOW, why is it up to the developer to "know" that Unix returns a particular format? Why instead isn't it up to FileMaker to have a consistent Timestamp format?

                           

                          Being able to know, programmatically, what you have, especially in cases like this, seems like an obvious thing.

                           

                          Other than FileMaker (and I'm not trying to be negative, guys, OK?), every environment has that variable type function, right?

                           

                          Thanks,

                           

                          - m

                          • 10. Re: How to know a variable's type?
                            johan

                            But there is. The function you should use is Timestamp().

                             

                            In your case, you know you have a text in a particular form, that you need to convert into a proper timestamp. It was a good idea to try to force the text into a timestamp using the GetAsTimestamp() function, but it will only work if the text already is formatted as a valid timestamp in your region. It works for me, since we use the proper ISO date format in Sweden. :-)

                             

                            Try this:

                            Timestamp ( Date ( month ; day ; year ) ; Time ( hours ; minutes ; seconds ) )

                            where you parse the year, month, day... etc information from the text timestamp in your SQL result.

                            • 11. Re: How to know a variable's type?
                              Mike_Mitchell

                              morkus wrote:

                               

                              IOW, why is it up to the developer to "know" that Unix returns a particular format? Why instead isn't it up to FileMaker to have a consistent Timestamp format?

                               

                              Unix formats sort properly in Unicode lists. Hence, if you want to sort timestamps that exist as text, you need that format.

                               

                              Time and date formatting are not consistent across the world. Internally, FileMaker stores them as numbers anyway, so it really doesn't care what format they're in. That's a boon to people in parts of the world other than the US, where date formats are encoded differently, and it's helpful to people not to have to look at computerese, but use a familiar format.

                               

                              FileMaker doesn't support fixed variable typing. Like PHP (and as mentioned by Johan), it uses type juggling to determine the correct variable type. This is vastly easier on the programmer and works just fine in the large majority of cases. Since there is no fixed typing, having a function that fetches it would be pointless.

                               

                              And there are already a number of functions that force the type for you, as pointed out by siplus. If you need a type forced, use them.

                              • 12. Re: How to know a variable's type?

                                Cool, yes, that's interesting, but I've been using Timestamps for many years and never run into this problem. Until now.

                                 

                                Using MySQL, for example, the following works fine on the same machine (no muss, no fuss):

                                 

                                select (UNIX_TIMESTAMP(max(<FIELD_NAME)) - UNIX_TIMESTAMP(min(<FIELD_NAME)))/86400  from <TABLENAME>)

                                 

                                And, right, this gives back a time in seconds, which is perfect when you divide by 84,600.

                                 

                                And, yes, the Timestamps are in the same format on the same machine: 2016-02-19 13:05:56

                                 

                                The MySQL UNIX_TIMESTAMP function apparently abstracts any formatting differences in Timestamps, which is exactly what I'm talking about.

                                 

                                -----

                                 

                                But, OK, in any case, how would I change the Mac's default timestamp format so FileMaker likes it?

                                 

                                I don't see how to do that in System Preferences and I don't see any articles on it when I search (I tried this already yesterday).

                                 

                                Thanks,

                                 

                                - m

                                • 13. Re: How to know a variable's type?
                                  Mike_Mitchell

                                  I think you need to break yourself of the "but this is what I'm used to" mindset you seem to be stuck in. This is not MySQL; this is not .NET; this is not Java. This is FileMaker. If you want to succeed in this environment, you need to learn how FileMaker does things and simply adapt instead of constantly wanting it to work like "that thingy over there".

                                   

                                  I don't know what you mean by "the Mac's default timestamp". If you're using ExecuteSQL, I've given you the solution. If you're talking about something else, then you'll need to be more specific.

                                  • 14. Re: How to know a variable's type?

                                    You make a really good point and I don't disagree. In my postings, I am really only using other environments as examples, however.

                                     

                                    If I used your nice Timestamp-formatting CF, for example, would it break if someone using another Timestamp format used the FM app? Perhaps it's relatively generic (it's actually a nice function and similar to something I wrote as well as a test).

                                     

                                    I'm just saying there should be an abstracted way to work with Timestamps (to handle them internally) so it doesn't fall to the developer to create workarounds like your CF. And, I realize this is FIleMaker (which I like a lot) and that abstracted method does not (currently) exist.

                                     

                                    -----

                                     

                                    TSGal suggested I modify the Mac's ISO time settings to "accommodate" for FMP's Timestamp handling. I have no idea how to do that and not sure that's really a good solution, anyway.

                                     

                                    Thanks,

                                     

                                    - m

                                    1 2 3 4 Previous Next