Announcement

Collapse
No announcement yet.

convert dates and times in 12hour format into time_t value

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

    convert dates and times in 12hour format into time_t value

    Hi All,

    I have data that has date and times info like so:

    1/21/2012 2:56:58AM
    1/21/2012 3:09:32PM

    I can use mktime_t to convert this to a time_t value I think (after chopping the data into the correct chunks). The only thing is the AM and PM formatting. mktime_t would want the input in military format I believe. Does anyone have any handy code snippets for converting 12hour time formats to 24hour?

    Thanks,
    Last edited by Southlander; 01-22-12, 08:50 AM.
    Sean Harrell
    Southland Trade Corp.

    #2
    Re: convert dates and times in 12hour format into time_t value

    Hi there Sean, haven't heard from you since '04. Glad to see your still scripting!

    What about something like "if PM then + 12" on the hour?

    Jonathan

    Comment


      #3
      Re: convert dates and times in 12hour format into time_t value

      Hey there Jonathan. Good to hear from you. Yes, that would work fine. Thanks for the tip.
      Sean Harrell
      Southland Trade Corp.

      Comment


        #4
        Re: convert dates and times in 12hour format into time_t value

        These functions return date and time strings.

        With some small effort Format_Time() could be adopted by storing the following in a variable and adding a few conditionals.

        time_t_hour(l.time_val, l.time_zone)


        Code:
        <MvFUNCTION NAME="Format_Date" PARAMETERS="time_val" STANDARDOUTPUTLEVEL="">    <MvASSIGN NAME="l.time_zone" VALUE="{ timezone() }">
            <MvFUNCRETURN VALUE="{
                    padl(time_t_month(l.time_val, l.time_zone),2,'0') $ '/' $
                    padl(time_t_dayofmonth(l.time_val, l.time_zone),2,'0') $ '/'
                    $ time_t_year(l.time_val, l.time_zone) }">
        </MvFUNCTION>
        
        <MvFUNCTION NAME="Format_Time" PARAMETERS="time_val" STANDARDOUTPUTLEVEL="">
                <MvASSIGN NAME="l.time_zone" VALUE="{ timezone() }">
            <MvFUNCRETURN VALUE="{
                padl(time_t_hour(l.time_val, l.time_zone),2,'0') $ ':' $
                padl(time_t_min(l.time_val, l.time_zone),2,'0') $ ':' $
                padl(time_t_sec(l.time_val, l.time_zone),2,'0') }">
        </MvFUNCTION>
        FYI If you are doing this in the context of a Merchant page template, Toolbelt has exhaustive date time formatting and conversion functions.
        Last edited by RayYates; 01-22-12, 08:46 PM.
        Ray Yates
        "If I have seen further, it is by standing on the shoulders of giants."
        --- Sir Isaac Newton

        Comment


          #5
          Re: convert dates and times in 12hour format into time_t value

          Originally posted by RayYates View Post
          FYI If you are doing this in the context of a Merchant page template, Toolbelt has exhaustive date time formatting and conversion functions.
          Thanks Ray. No this is not Merchant related. It is a standalone script app I am writing to import some point of sale data and do some custom analysis. Our POS exports flatfile data and formats the date/time of the transactions in the above mentioned manner. So I am wanting to do the following: (appreciate any advice on whether my approach is bad or not)

          Covert the date/time in this format (1/21/2012 2:56:58AM) to a time_t value. Then load that into an array that is indexed on that time_t value so that I can build an ordered list of transactions from earliest to latest. Apply the analysis logic, which depends on the transactions being sorted in order, and then covert from time_t back to a mm/dd/yyyy xx:xx:xx am/pm format to output report data in a human readable format. Sound ok?

          Thanks,
          Sean
          Sean Harrell
          Southland Trade Corp.

          Comment


            #6
            Re: convert dates and times in 12hour format into time_t value

            When you convert to time_t, why do you need to lose the original value of human readable time? Couldn't you store both?

            Also, unless you really need to, you don't have to convert to time_t. You're already parsing the date/time, why not just reconstruct the string as yyyy $ mm $ dd $ (hh * ((l.meridian EQ 'PM')*12)) $ mm $ ss? Make sure to '0'-pad everything to keep it all consistent.

            Typing out loud...

            Comment


              #7
              Re: convert dates and times in 12hour format into time_t value

              I am intending to use time_t because I am not storing any of this imported data into a database (where a DATE field type can be create and indexed upon). I will just suck it all into an array and sort it numerically based on time_t. I will have the original human readable date and time loaded into variables obviously to do the conversion. So then when I say convert back, I really just mean eval that original variable set to get the report printout while using the equivalent time_t value just for sorting. This seems more efficient to me than somehow sorting an array based on xx/xx/xxxx tt:tt:tt am/pm. In fact I am not even sure how one would do that without sorting date, then sub-sorting the time after converting from 12hr to 24hr. Just seems more complex than time_t.
              Sean Harrell
              Southland Trade Corp.

              Comment


                #8
                From looking at the links that refer to time on mivascript.com,
                http://www.mivascript.com/topic/system-variables.html
                http://www.mivascript.com/item/mivas...me_t_hour.html
                http://www.mivascript.com/item/mivas...ions/padl.html

                it looks like I can build a current military time using something like this...
                Code:
                <mvt:assign name="g.curr_hour" value="padl(s.dyn_tm_hour,2,0)" />
                <mvt:assign name="g.curr_min" value="padl(s.dyn_tm_min,2,0)" />
                &mvt:global:curr_hour;&mvt:global:curr_min;

                Comment

                Working...
                X