Announcement

Collapse
No announcement yet.

Unix-Like Time Functions: Wish List? Been done already?

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

  • Unix-Like Time Functions: Wish List? Been done already?

    I searched to see if this had been brought up before, can't seem
    to find it. It would be great to have Unix-like time functions
    available in PBCC. I'm sure I could spend lots of time writing the
    functions myself, but do they already exist? Built-in to v5 would be great.

    Unix has a timestamp: a 32-bit or 64-bit timer that counts the
    seconds since January 1, 1970. Dealing with our oddball crazy calendar
    then becomes much easier; ie, doesn't matter if a month has 30 days or
    31 days, leap years don't matter, etc. It can make life easy; ie, given
    the current timestamp, add 604800 to find the timestamp a week from now
    (604800 is 60 * 60 * 24 * 7). It won't matter if it falls in a different
    month, different year, or even different century.

    Unix languages then have some great tools to convert the timestamp back
    to useful strings. You can code, for exmaple,
    Code:
    PRINT UnixDate("l dS \of F Y h:i:s A",timestamp???)
    which produces something like: Monday 15th of August 2005 03:12:46 PM

    This came up because I'm writing code that needs to produce the calendar
    date of the date five days from now. Starting from scratch is a pain because
    suddenly you're dealing with irregular month length, leap years, etc.

    Somebody already have those routines written? Would love to see built-in....


    ------------------
    Christopher Becker
    Advanced Airborne Test Facility
    Island of Kauai, Hawaii

    [This message has been edited by Christopher Becker (edited April 03, 2006).]
    Christopher P. Becker
    signal engineer in the defense industry
    Abu Dhabi, United Arab Emirates

  • #2
    Search Poffs and these forums for API items, such as system time and GetDateFormat listed in the code below:

    Code:
    FUNCTION DateTimeStr (sysT AS SYSTEMTIME, DtFormat AS LONG,TmFormat AS LONG) AS STRING
      DIM zDate AS ASCIIZ * 32
      DIM zTime AS ASCIIZ * 32
      DIM DateFormat(13) AS LOCAL ASCIIZ * 32
      DIM TimeFormat(6) AS LOCAL ASCIIZ * 32
    
        DateFormat(1)  = "MM'/'dd'/'yy"           ' 02/18/01
        DateFormat(2)  = "MM'/'dd'/'yyyy"         ' 02/18/2001
        DateFormat(3)  = "MMM dd ''''yy"          ' Feb 18 '01
        DateFormat(4)  = "MMM d yyyy"             ' Feb 18 2001
        DateFormat(5)  = "MMMMM d',' yyyy"        ' February 18, 2001
        DateFormat(6)  = "ddd MM'/'dd'/'yy"       ' Sun 02/18/01
        DateFormat(7)  = "ddd MM'/'dd'/'yyyy"     ' Sun 02/18/2001
        DateFormat(8)  = "ddd MMM dd ''''yy"      ' Sun Feb 18 '01
        DateFormat(9)  = "ddd MMM d yyyy"         ' Sun Feb 18 2001
        DateFormat(10)  = "ddd MMM d yyyy"        ' Sun Feb 18 2001
        DateFormat(11) = "ddd',' dd MMM yyyy"     ' Sun, 18 Feb2001
        DateFormat(12) = "yyMMdd"                 ' 010218
        DateFormat(13) = "yyyyMMdd"               ' 20010218
    
        TimeFormat(1) = "HH':'mm"                 ' 15:18
        TimeFormat(2) = "HH':'mm':'ss"            ' 15:18:02
        TimeFormat(3) = "h':'mm tt"               ' 3:18 PM
        TimeFormat(4) = "h':'mm':'ss tt"          ' 3:18:02 PM
        TimeFormat(5) = "hh':'mm tt"              ' 03:18 PM
        TimeFormat(6) = "hh':'mm':'ss tt"         ' 03:18:02 PM
    
        IF DtFormat > 0 AND DtFormat < 14 THEN
            CALL GetDateFormat(%LOCALE_USER_DEFAULT, %NULL,  sysT, DateFormat(DtFormat), zDate, 32)
        END IF
    
        IF TmFormat > 0 AND TmFormat < 7 THEN
           CALL GetTimeFormat(%LOCALE_USER_DEFAULT, %NULL, sysT, TimeFormat(TmFormat), zTime, 32)
        END IF
    
        IF DtFormat > 0 AND TmFormat > 0 THEN
            FUNCTION = zDate + "  " + zTime
          ELSE
            IF DtFormat& > 0 THEN
               FUNCTION = zDate
            ELSE
                FUNCTION = zTime
            END IF
        END IF
    
    END FUNCTION
    ------------------

    Comment


    • #3
      Search for Julian Functions, day of week, etc it is all here.

      ------------------
      Roy Cline
      Roy Cline

      Comment


      • #4
        John, thanks for your fast reply and trying to help. Maybe this is a dumb
        question and the answer is right in front of me, but I'm not sure you answered
        my question. I was looking for a seamless timer, like the Unix timestamp.
        The SYSTEMTIME structure -- at least, it appears to me -- has elements such as
        Hour, Minute, Second, Year, Month, etc.

        You can't take a variable that's been set to SYSTEMTIME and, for example,
        add 86400 to it to see tomorrow's Month, Day, Year, etc. DateTimeStr() handles
        the output just fine, but I also need the underlying mechanism that measures time.

        If I've gotten this wrong, please post a code snippet. Given the current SYSTEMTIME,
        how to use DateTimeStr() to see MM-DD-YYYY of the date five days ago?

        You advised that I search Poffs. I don't know what that is. When I search for related
        date/time problems, I see everyone seems to have their own unique solution, some of
        which involve Julian Date calculations. I know Julian Dates make life easier just like
        the Unix timestamp. But I want to avoid extensive re-coding and computing just to find
        out five days ago was in March. Seems something like that should be packaged up
        and standardized.

        Again, if it's a dumb question, sorry.

        ------------------
        Christopher Becker
        Advanced Airborne Test Facility
        Island of Kauai, Hawaii

        [This message has been edited by Christopher Becker (edited April 03, 2006).]
        Christopher P. Becker
        signal engineer in the defense industry
        Abu Dhabi, United Arab Emirates

        Comment


        • #5
          Roy, you mean, in other words, re-code everything? I'm not trying to be
          a jerk. But, of course, I can compute the Julian date and manipulate it.
          But then what? I still need to code all of the output functions again.
          I'm sure a Julian-to-calendar function exists somewhere, but now you're
          talking all kinds of pieces that need to be packaged up and integrated.

          I'm just surprised no one's made a clean, easy-to-use function like the
          one I mentioned in my first post.

          Or am I just way off base here?

          ------------------
          Christopher Becker
          Advanced Airborne Test Facility
          Island of Kauai, Hawaii
          Christopher P. Becker
          signal engineer in the defense industry
          Abu Dhabi, United Arab Emirates

          Comment


          • #6
            I don't have any PowerBASIC code handy, but I can describe what you need to do and give you some C code to work with.

            Basically, the trick is to convert a SYSTEMTIME structure to a FILETIME structure (which is really a 64-bit integer), and then convert that to the standard UNIX 32-bit integer value.

            You would call the SystemTimeToFileTime function in the Windows API to convert your SYSTEMTIME structure to a FILETIME value. Then you could use the following function:

            Code:
            long FileTimeToUnixTime(FILETIME *lpft)
            {
                __int64 *qwft = (__int64 *)lpft;
                return (long)(((*qwft)/10000000)-11644473600);
            }
            To convert it back, you just reverse the process:

            Code:
            void UnixTimeToFileTime(long ut, FILETIME *lpft)
            {
                __int64 qwft = (__int64)((ut + 11644473600) * 10000000);
                *lpft = (FILETIME *)qwft;
            }
            Of course, you'd want to actually check the paramters being passed and so on. If someone else isn't enterprising enough to convert this to PowerBASIC code, I'll take a stab at it later when I have some free time.

            ------------------
            Mike Stefanik
            www.catalyst.com
            Catalyst Development Corporation

            [This message has been edited by Mike Stefanik (edited April 03, 2006).]
            Mike Stefanik
            sockettools.com

            Comment


            • #7
              Hoo-Rah!
              That's exactly what I was looking for.
              Still, would be nice to have built-in functions, but this will work.
              Thanks.

              ------------------
              Christopher Becker
              Advanced Airborne Test Facility
              Island of Kauai, Hawaii
              Christopher P. Becker
              signal engineer in the defense industry
              Abu Dhabi, United Arab Emirates

              Comment


              • #8
                FILETIME

                Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).


                typedef struct _FILETIME { DWORD dwLowDateTime; DWORD dwHighDateTime;
                } FILETIME, *PFILETIME;

                SYSTEMTIME

                Specifies a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.


                typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds;
                } SYSTEMTIME, *PSYSTEMTIME;
                Members
                wYear
                The year (1601 - 30827).
                wMonth
                The month.

                January = 1
                February = 2
                March = 3
                April = 4
                May = 5
                June = 6
                July = 7
                August = 8
                September = 9
                October = 10
                November = 11
                December = 12


                wDayOfWeek
                The day of the week.

                Sunday = 0
                Monday = 1
                Tuesday = 2
                Wednesday = 3
                Thursday = 4
                Friday = 5
                Saturday = 6


                wDay
                The day of the month (1-31).
                wHour
                The hour (0-23).
                wMinute
                The minute (0-59).
                wSecond
                The second (0-59).
                wMilliseconds
                The millisecond (0-999).
                Remarks
                It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should

                Convert the SYSTEMTIME structure to a FILETIME structure.
                Copy the resulting FILETIME structure to a ULARGE_INTEGER structure.
                Use normal 64-bit arithmetic on the ULARGE_INTEGER value.

                The system can periodically refresh the time by synchronizing with a time source. Because the system time can be adjusted either forward or backward, do not compare system time readings to determine elapsed time. Instead, use one of the methods described in Windows Time.

                Well get the system time and cast it to filetime format.
                Do your math,100-nanosecond intervals since January 1, 1601 (UTC).

                ------------------
                Roy Cline
                Roy Cline

                Comment


                • #9
                  here are some macros that clay clear posted to convert two dwords into a quad: http://www.powerbasic.com/support/pb...ad.php?t=23915

                  here is a function i built off of clay's code:

                  Code:
                  function makqud(dwlo as dword, dwhi as dword) as quad
                  
                      #register none
                  
                      local qret  as quad
                  
                      !pushad
                      !cld
                      !lea    edi, qret
                      !mov    eax, dwlo
                      !mov    eax, [eax]
                      !stosd
                      !mov    eax, dwhi
                      !mov    eax, [eax]
                      !stosd
                      !popad
                  
                      function=qret
                  
                  end function

                  ------------------
                  Adam Drake
                  PowerBASIC

                  Comment


                  • #10
                    Christopher,

                    Calculating the date five days from now is easy with the WinAPI, too.

                    Code:
                    local q&&, x as systemtime
                    
                    getsystemtimeasfiletime byval varptr(q)
                    
                    q = q + (5 * 24 * 3600 * 10000000)
                    
                    filetimetosystemtime byval varptr(q), x
                    
                    date$ = format$(x.wmonth, "00") + "-" + format$(x.wday, "00") + "-" + format$(x.wyear)
                    EDIT: if you want the returned date in "verbose" form (e.g., "Monday, January 1, 2007"), then
                    look in the SDK for the GetDateFormat() API. This API will accept
                    a systemtime structure, like I used above.


                    ------------------




                    [This message has been edited by Clay Clear (edited April 03, 2006).]

                    Comment


                    • #11
                      Code:
                      ' add days courtesy p lamejn via:
                      'http://b]byref qft[/b]  ' override parameter two
                        ...
                        filetimetosystemtime [b]byref[/b] qft,st  ' override parameter one
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Yes this has been discussed many times before. There are approximately an infinite number of ways to 'do' dates and times and there is no sense cluttering up the compiler with those functions for the rare times they are needed.

                        Comment


                        • #13
                          John,

                          This stuff may not be new or interesting to you, but it may be new
                          and interesting to someone else. Lighten-up man.



                          ------------------
                          Greg

                          Comment


                          • #14
                            Adam,

                            JFYI, the following should work, and would be a lot easier
                            than that old code I posted:

                            Code:
                            function makqud (byval dlow as dword, byval dhigh as dword) as quad
                                #register none
                                ! push dlow
                                ! pop dword ptr function[0]
                                ! push dhigh
                                ! pop dword ptr function[4]
                            end function

                            ------------------

                            Comment


                            • #15
                              Is it just me, or is this forum more hostile than others? I use other internet forums where people are eager to help and even hold the hands of newbies. But there are some users in this forum who seem too eager to criticize, offering little more than the equivalent of, “That’s a stupid question.”

                              John Hackl’s reply got me thinking. If my topic has been discussed many times before, maybe that should tell you something: it interests many people. And having a multitude of homemade solutions floating around - some in assembly - might be confusing to some. Seems to me the makers of the compiler might want to hear that. If you’re accustomed to PHP and Unix, these routines are hardly “clutter”.

                              And this isn’t the only thread where I’ve seen this. I’ve gotten lots of help in this forum, but I’ve also seen arrogance and condemnation for legitimate questions. Are the egos here really that big? They’ll answer questions with, “Look it up in win32api” or “Try this...” with a cryptic fragmented code snippet that the newbie can’t compile or understand. I recently tried to help a newbie only to have another member harshly criticize a typo in my code. This is the same guy who prides himself on finding the goofs in Star Wars movies. You’re not helping anyone.

                              Remind me of: “Answers are $10. Stupid looks are free.”

                              I guess I misunderstood the mission of this forum.


                              ------------------
                              Christopher Becker
                              Advanced Airborne Test Facility
                              Island of Kauai, Hawaii
                              Christopher P. Becker
                              signal engineer in the defense industry
                              Abu Dhabi, United Arab Emirates

                              Comment


                              • #16
                                Forgot to say thanks to the replies that contained code and comments which answered my question.

                                ------------------
                                Christopher Becker
                                Advanced Airborne Test Facility
                                Island of Kauai, Hawaii
                                Christopher P. Becker
                                signal engineer in the defense industry
                                Abu Dhabi, United Arab Emirates

                                Comment


                                • #17
                                  How's this ?

                                  I used to think that it would be good idea to have more date and time functions, but after a while realized how many there would need to be and how rarely the would be used, I changed my mind. I now believe that the compiler is better off WithOut extensive date and time functions. Take a look at how many date and time functions there are in VB or VBA.

                                  No hostile words at all. There weren't any hostile words in my original post also.



                                  [This message has been edited by John Hackl (edited April 05, 2006).]

                                  Comment

                                  Working...
                                  X