Community
    • Login

    12-hour to 24-hour clock conversion

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    21 Posts 5 Posters 3.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan KilbornA
      Alan Kilborn @Alan Kilborn
      last edited by Alan Kilborn

      I got it to work with named groups. The problem was, and it makes it less elegant, is that mixing numbered and named capture groups seems to mess things up. So I had to artificially name group # 1 as “one” instead of just using its number. I’ll let the reader decide if this version is more readable than the previous:

      Find: ^(?:(?<one>01)|(?<two>02)|(?<three>03)|(?<four>04)|(?<five>05)|(?<six>06)|(?<seven>07)|(?<eight>08)|(?<nine>09)|(?<ten>10)|(?<eleven>11)|(?<twelve>12))\.(?<mins>\d\d)\.(?<secs>\d\d)\.(?<ms>\d{3}) (?:(?<am>A)|(?<pm>P))M

      Repl: (?{twelve}?{am}00:12)(?{one}?{pm}13:01)(?{two}?{pm}14:02)(?{three}?{pm}15:03)(?{four}?{pm}16:04)(?{five}?{pm}17:05)(?{six}?{pm}18:06)(?{seven}?{pm}19:07)(?{eight}?{pm}20:08)(?{nine}?{pm}21:09)(?{ten}?{pm}22:10)(?{eleven}?{pm}23:11):$+{mins}:$+{secs}:$+{ms}

      Search mode: Regular expression

      (And I fully expect a “holy cow batman!” from @Meta-Chuh on that one!)

      (And yes, doing 12 differently from the others in the replace was intentional, as it is a little “out of place” in the conversion)

      EkopalypseE Meta ChuhM 2 Replies Last reply Reply Quote 5
      • EkopalypseE
        Ekopalypse @Alan Kilborn
        last edited by

        @Alan-Kilborn

        LOL - if I could I would upvote it 12 times. :-D

        Alan KilbornA 1 Reply Last reply Reply Quote 2
        • Alan KilbornA
          Alan Kilborn @Ekopalypse
          last edited by Alan Kilborn

          @Ekopalypse

          Sometimes things get upvoted too quickly for people to have tried it out to verify/duplicate results. This is somewhat bad as if something doesn’t work (and the thread dies right there) it looks to future readers like a good solution…

          BTW I think the “replace” is very readable with the named groups, but the “find” suffers…slightly…

          EkopalypseE 1 Reply Last reply Reply Quote 1
          • EkopalypseE
            Ekopalypse @Alan Kilborn
            last edited by

            @Alan-Kilborn

            :-D for me this looks like what we call - bohemian villages.
            But I can participate on that thread now - I’ve tested it, and it looks good to me :-)
            Let’s see what the OP thinks about it.

            1 Reply Last reply Reply Quote 2
            • Sophia CruzS
              Sophia Cruz @Alan Kilborn
              last edited by

              @Alan-Kilborn it works perfectly. Thank you so much!

              1 Reply Last reply Reply Quote 3
              • Sophia CruzS
                Sophia Cruz
                last edited by

                2nd solution is less confusing but both still produces the desired output. :D

                1 Reply Last reply Reply Quote 1
                • Meta ChuhM
                  Meta Chuh moderator @Alan Kilborn
                  last edited by

                  @Alan-Kilborn

                  holy cow batman! 😄👍

                  Sometimes things get upvoted too quickly for people to have tried it out to verify/duplicate results.

                  i guess sometimes your writing style is so worth an upvote, that people give you one, even if they have tested it’s content to fail.

                  greetings,
                  robin

                  Alan KilbornA 1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Meta Chuh
                    last edited by Alan Kilborn

                    @Meta-Chuh said:

                    your writing style…worth an upvote…

                    My writing style? Nah. I don’t have the flowery prose of a @PeterJones or the diplomatic panache of a @Meta-Chuh . [Basically I don’t wanna put that kind of effort in–to the typing part–obviously with the above regexes staring us in the face I’ll put a little time/effort into THAT aspect. Trying to provide some hopefully accurate help, maybe without the greatest english composition accompanying.]

                    1 Reply Last reply Reply Quote 2
                    • guy038G
                      guy038
                      last edited by guy038

                      Hi, @sophia-cruz, @alan-kilborn, @eko-palypse, @meta-chuh and All,

                      Here is a variant of the Alan’s solution, which is less restrictive :

                      • It just needs that the hour should be located after a non-word, instead of the beginning of line, due to the \b syntax

                      • It does not care of the synbol between the different parts of the hour ( just changing the \. syntax by . ! )

                      • It allows the milliseconds part to be absent ( .MMM ), in the hour, with the (?:.(\d{3}))? syntax

                      • It allows the AM or PM part to come next to the digits, with \x20?

                      • It does not care of the case , allowing the four syntaxes am, pm, AM and PM with the regex (?i:(AM)|PM)

                      Note :
                      In the remplacement regex, when group 17 exists ( AM ), I just rewrite the hour part ( \1 ), except when hour = 12, where the logic is reversed !


                      So, assuming this sample text :

                      12.00.47 AM
                      	12.58.18.387 am
                      01 07 28 421 AM
                      03:17:31:934am
                      05.43.26.582 AM
                              07:23:03.732 am
                      09.31.08AM
                      11.57.59,003 am
                      ~~~~~~~~~~~~~~~~~~~~
                      12.00.45 PM
                      	12.56.49.203 pm
                      01 04 51 387 PM
                      03:15:33:632pm
                      05.22.00.814 PM
                              07:33:55.548 pm
                      09.45.26PM
                      11.59.11,247 pm
                      

                      With the regex S/R below :

                      SEARCH :    \b(0(?:(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))|(10)|(11)|(12)).(\d\d).(\d\d)(?:.(\d{3}))?\x20?(?i:(AM)|PM)
                                    │    │   │   │   │   │   │   │   │   │    │    │    │     │      │         │                │
                                    1    2   3   4    5   6   7   8   9   10   11   12    13    14     15        16               17   <-- Groups
                      
                      REPLACE :   (?2(?17\1:13))(?3(?17\1:14))(?4(?17\1:15))(?5(?17\1:16))(?6(?17\1:17))(?7(?17\1:18))(?8(?17\1:19))(?9(?17\1:20))(?10(?17\1:21))(?11(?17\1:22))(?12(?17\1:23))(?13(?{17}00:\1)):$14:$15(?16\:$16)
                      

                      We would obtain :

                      00:00:47
                      	00:58:18:387
                      01:07:28:421
                      03:17:31:934
                      05:43:26:582
                              07:23:03:732
                      09:31:08
                      11:57:59:003
                      ~~~~~~~~~~~~~~~~~~~~
                      12:00:45
                      	12:56:49:203
                      13:04:51:387
                      15:15:33:632
                      17:22:00:814
                              19:33:55:548
                      21:45:26
                      23:59:11:247
                      

                      Best Regards,

                      guy038

                      1 Reply Last reply Reply Quote 3
                      • guy038G
                        guy038
                        last edited by guy038

                        Hi, All,

                        When I woke up this morning, I immediately saw a simplification ( in length ! ) of the replacement regex ;-))

                        So, my last version is :

                        SEARCH \b(0(?:(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))|(10)|(11)|(12)).(\d\d).(\d\d)(?:.(\d{3}))?\x20?(?i:(AM)|PM)

                        REPLACE (?13(?{17}00:\1):(?17\1:(?{2}13)(?{3}14)(?{4}15)(?{5}16)(?{6}17)(?{7}18)(?{8}19)(?{9}20)(?{10}21)(?{11}22)(?{12}23))):$14:$15(?16\:$16)

                        BR

                        guy038

                        1 Reply Last reply Reply Quote 3
                        • guy038G
                          guy038
                          last edited by

                          Hi, all,

                          As the (?...:....) regex structure, of the Boost regex library is not so common, you may feel a bit lost about the replacement regex syntax !

                          So, here is, below, the algorithmic translation of the replacement regex, of my previous post :

                          If group 13 exists    #  Case hour = '12'
                          
                              then
                          
                                  If group 17 exists    #  String 'AM' or 'am', after the digits
                            
                                      then
                          
                                          Write the string '00'
                          
                                      else
                          
                                          Rewrite the group 1 contents    #  Actually, the hour '12'
                          
                              else
                          
                                  If group 17 exists    #  String 'AM' or 'am', after the digits
                          
                                      then
                          
                                          Rewrite the group 1 contents    #  The 'hour' digits, so the values  '01'  or  '02'  or  ...... '11'
                          
                                      else    #  The 'hour' digits, when followed with the string 'PM' or 'pm'
                          
                                          If group  2 exists then write the string '13'    #  Case hour = '01'
                          
                                          If group  3 exists then write the string '14'    #  Case hour = '02'
                          
                                          If group  4 exists then write the string '15'    #  Case hour = '03'
                          
                                          If group  5 exists then write the string '16'    #  Case hour = '04'
                          
                                          If group  6 exists then write the string '17'    #  Case hour = '05'
                          
                                          If group  7 exists then write the string '18'    #  Case hour = '06'
                          
                                          If group  8 exists then write the string '19'    #  Case hour = '07'
                          
                                          If group  9 exists then write the string '20'    #  Case hour = '08'
                          
                                          If group 10 exists then write the string '21'    #  Case hour = '09'
                          
                                          If group 11 exists then write the string '22'    #  Case hour = '10'
                          
                                          If group 12 exists then write the string '23'    #  Case hour = '11'
                          
                                 endif
                          
                          endif
                          
                          Write the string ':'
                          
                          Rewrite the group 14    #  Minutes digits
                          
                          Write the string ':'
                          
                          Rewrite the group 15    #  Seconds digits
                          
                          if group 16 exits       #  Milleseconds digits
                          
                              then
                          
                                  write the string ':'
                          
                                  write the group 16 contents    # Milleseconds digits
                          endif
                          

                          BR

                          guy038

                          1 Reply Last reply Reply Quote 3
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors