Announcement

Collapse
No announcement yet.

Turning a date format into a time_t

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

    Turning a date format into a time_t

    So I have a function that turns a time_t into any date format, basically works the same as the PHP Date(). Now I'm trying to reverse the process. I'm getting a formatted date (publish date from RSS for example) and want to convert it back to a time_t.

    Has anyone built anything like this before? Do you think there is value in making a function that can decode anything, or one specific to the date being sent?

    I was thinking something like Decode_Date( l.date_string, l.time_t, 'Y-m-dTH:i:sO' ) but they just may be overkill. Especially when trying to convert timezone offsets.

    Any help? Ideas?

    Thanks,

    /Scott Mc

    #2
    Re: Turning a date format into a time_t

    How about the mktime() function? Does that work for you?

    Comment


      #3
      Re: Turning a date format into a time_t

      Originally posted by Jonathan-Driftwood View Post
      How about the mktime() function? Does that work for you?
      In the end, yes.

      But I can't feed 'Thu, 30 Aug 2012 05:00:00 GMT' and '2012-08-28T23:54:00.019-04:00' to mktime(). So I was seeing if anyone had tackled this intermediate step. Did they just make one function for each RFC/ISO date format, or an intelligent parser so that if fed how a format was laid out, it could figure out the parameters needed for mktime().

      That's all.

      Comment


        #4
        Re: Turning a date format into a time_t

        Yes you can. Just tokenize the input and then run mktime() against the result. Here are four small functions which might give you a starting point:

        Code:
        <MvFUNCTION NAME="FormatDate" PARAMETERS="time_t" STANDARDOUTPUTLEVEL="">
        <MvASSIGN NAME="l.date" VALUE="">
        <MvASSIGN NAME="l.date" VALUE="{ padl(time_t_month(l.time_t, timezone()), 2, '0') $ '/' $ padl(time_t_dayofmonth(l.time_t, timezone()), 2, '0') $ '/' $ time_t_year(l.time_t, timezone()) }">
        <MvFUNCTIONRETURN VALUE="{ l.date }">
        </MvFUNCTION>
        
        <MvFUNCTION NAME="FormatDateMe" PARAMETERS="time_t" STANDARDOUTPUTLEVEL="">
        <MvASSIGN NAME="l.date" VALUE="">
        <MvASSIGN NAME="l.date" VALUE="{ padl(time_t_year(l.time_t, timezone()) $ time_t_month(l.time_t, timezone()), 2, '0') $ padl(time_t_dayofmonth(l.time_t, timezone()), 2, '0') }">
        <MvFUNCTIONRETURN VALUE="{ l.date }">
        </MvFUNCTION>
        
        <MvFUNCTION NAME="UnFormatDateMe" PARAMETERS="time_u" STANDARDOUTPUTLEVEL="">
        <MvASSIGN NAME="l.year" VALUE="{ gettoken(time_u, asciichar(047), 0) }">
        <MvASSIGN NAME="l.month" VALUE="{ gettoken(time_u, asciichar(047), 1) }">
        <MvASSIGN NAME="l.day" VALUE="{ gettoken(time_u, asciichar(047), 2) }">
        <MvASSIGN NAME="l.date" VALUE="{ mktime_t(l.year, l.month, l.day, 5, 0, 0, time_zone) }">
        <MvFUNCTIONRETURN VALUE="{ l.date }">
        </MvFUNCTION>
        
        <MvFUNCTION NAME="ShowDateMe" PARAMETERS="time_u" STANDARDOUTPUTLEVEL="">
        	<MvASSIGN NAME="l.year" VALUE="{ gettoken(l.rawdate, '/', 3) }">
        	<MvASSIGN NAME="l.month" VALUE="{ gettoken(l.rawdate, asciichar(047), 1) }">
        	<MvASSIGN NAME="l.day" VALUE="{ gettoken(l.rawdate, asciichar(047), 2) }">
        	<MvASSIGN NAME="l.date" VALUE="{ mktime_t(l.year, l.month, l.day, 5, 0, 0, time_zone) }">
        	<MvFUNCTIONRETURN VALUE="{ l.date }">
        </MvFUNCTION>
        The "UnFormatDateMe" aims towards your goal. Hope that helps some Scott...

        Jonathan
        Last edited by Jonathan-Driftwood; 08-31-12, 12:31 PM.

        Comment


          #5
          Re: Turning a date format into a time_t

          This would be sooooo much easier if MivaScript actually had regex.

          Comment


            #6
            Re: Turning a date format into a time_t

            I dunno buddy, guess over the years I've just grown used to tokenizing the strings. Wouldn't be much to add to the code above and parse the different formats you quoted.

            And there's probably a way to do it more efficiently and quickly, I just stuck with what worked for me is all.

            Sorry it was not helpful for you.

            Comment


              #7
              Re: Turning a date format into a time_t

              No, no.. I appreciate the advice. I just go to the extreme. I've actually gotten pretty close. I just need to figure out how to properly parse timezones.

              So far:
              Given:
              Code:
              <p>
              <MvASSIGN NAME = "l.time_string" VALUE = "{ '2012-08-30T00:00:00-07:00' }">
              <MvASSIGN NAME = "l.format" VALUE = "{ '/Y-/m-/dT/g:/i:/s/O' }">
              <MvASSIGN NAME = "l.new_time_t" VALUE = "{ Decode_Date( l.time_string, l.format ) }">
              
              
              <br />Taking the string &quot;<MvEVAL EXPR = "{ encodeentities( l.time_string ) }">&quot; converts to the time_t of <MvEVAL EXPR = "{ l.new_time_t }">. Taking that value converts back to <MvEVAL EXPR = "{ Default_Date( l.new_time_t ) }">.
              <hr />
              
              
              <MvASSIGN NAME = "l.time_string" VALUE = "{ '2012-08-30T10:00:02.712-07:00' }">
              <MvASSIGN NAME = "l.format" VALUE = "{ '/Y-/m-/dT/g:/i:/s/O' }">
              <MvASSIGN NAME = "l.new_time_t" VALUE = "{ Decode_Date( l.time_string, l.format ) }">
              
              
              <br />Taking the string &quot;<MvEVAL EXPR = "{ encodeentities( l.time_string ) }">&quot; converts to the time_t of <MvEVAL EXPR = "{ l.new_time_t }">. Taking that value converts back to <MvEVAL EXPR = "{ Default_Date( l.new_time_t ) }">.
              <hr />
              
              
              <MvASSIGN NAME = "l.time_string" VALUE = "{ 'Sun, 14 Aug 2005 16:13:03 GMT' }">
              <MvASSIGN NAME = "l.format" VALUE = "{ '/D, /d /M /Y /g:/i:/s /T' }">
              <MvASSIGN NAME = "l.new_time_t" VALUE = "{ Decode_Date( l.time_string, l.format ) }">
              
              
              <br />Taking the string &quot;<MvEVAL EXPR = "{ encodeentities( l.time_string ) }">&quot; converts to the time_t of <MvEVAL EXPR = "{ l.new_time_t }">. Taking that value converts back to <MvEVAL EXPR = "{ Default_Date( l.new_time_t ) }">.
              <hr />
              <p>
              Becomes:
              Code:
              Taking the string "2012-08-30T00:00:00-07:00" converts to the time_t of 1346284800. Taking that value converts back to Wednesday, August 29th, 2012 5:00:00 PM Pacific Daylight Time.
              Taking the string "2012-08-30T10:00:02.712-07:00" converts to the time_t of 1346320802. Taking that value converts back to Thursday, August 30th, 2012 3:00:02 AM Pacific Daylight Time.
              Taking the string "Sun, 14 Aug 2005 16:13:03 GMT" converts to the time_t of 1124035983. Taking that value converts back to Sunday, August 14th, 2005 9:13:03 AM Pacific Daylight Time.
              Still need to add the exception for when seconds have a decimal place too. But otherwise, it's converting things like "Pacific Daylight Time" and "PDT" to -7.

              Comment


                #8
                Re: Turning a date format into a time_t

                Update: Okay, got it 93% working. There is just no feasible way for the system to understand time zones.
                1) The abbreviations are not unique. For instance there are five different meanings for CST, all with different shifts...
                CST Central Standard Time (North America) −06
                CST China Standard Time +08
                CST Central Standard Time (Australia) +09:30
                CST Central Summer Time (Australia) +10:30
                CST Cuba Standard Time -05

                2) Without taking liberties (assume that it will always be the last thing on the string), it would be prohibitively complex to parse out the length and meaning of the spelled out time zones.

                Other than that, all the commands that map out to something useful work correctly.

                Thanks for the help and encouragement

                Comment

                Working...
                X