• Login
Community
  • Login

TextFX - customizing the date/time format

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 4 Posters 2.7k 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.
  • M
    mkupper
    last edited by May 31, 2019, 10:51 PM

    This is a continuation of the https://notepad-plus-plus.org/community/topic/17730/textfx-insert-questions thread which has topic-drifted into that TextFX itself is no longer supported.

    The goal is to learn how to set up substitutes. using Pythonscript, for TextFX’s Insert date & time functions and to set up keyboard shortcuts. For example, at present I have F5 mapped to TextFX’s Insert date & time - short format which generates “4:43 PM 5/30/2019.” My desire is for that F5 generate the date & time formatted as 05/30/2019 16:43. Once that’s available I’ll set up another keyboard shortcut to generate a yyyy-mm-dd hh:mm format (and probably add the :ss seconds too as I can backspace those out if I don’t need them)

    A 1 Reply Last reply Jun 1, 2019, 2:11 AM Reply Quote 0
    • A
      Alan Kilborn @mkupper
      last edited by Jun 1, 2019, 2:11 AM

      @mkupper

      It might best best to have a dialog box come up and allow you to select which format you want from a list? The list would be in the format of examples of the current time, each formatted differently. If you like that idea, we can proceed with that. An upside of this is that there need only be one script. Selecting a format and choosing OK would insert the time in the chosen format into the current document. The downside of this is that it is hard to have a “long” list of formats in the dialog box that Pythonscript provides. I do have a workaround for that, though.

      Otherwise, with Pythonscripts, it is a one script per keycombination kind of thing. Thus, if you want 3 different hotkeys to insert 3 different timestamp formats, you have to have 3 script files that are 90% the same repeated code. It’s okay…but not ideal. I think Luascript is better in this regard, but I’m not sure as I don’t know much about Lua or the Luascript plugin.

      So we will take some ideas from these links to build your final scripts:

      https://notepad-plus-plus.org/community/topic/16147/date-and-time
      https://notepad-plus-plus.org/community/topic/13792/date-time-again

      1 Reply Last reply Reply Quote 3
      • A
        Alan Kilborn
        last edited by Jun 3, 2019, 1:22 AM

        So even though I didn’t get feedback from the OP yet, I came up with a little Pythonscript for this; stole the input mechanism from something I already had. Basically, you define the formats you want to use in the source code (I’ve predefined some) and then when the script is run, it shows you different examples of the current time formatted in different ways.

        Here’s what it looks like at runtime:

        Imgur

        If there are more than 5 choices defined (I defined 6), then if what you are looking for doesn’t appear on-screen you simply enter > and click OK and the next group of choices is displayed. When you see the format you want to insert, just enter its letter, e.g. c (overwriting ALL of the preselected blue text) and click OK. At that point the desired timestamp text will be inserted into the currently active Notepad++ file.

        Here’s the Pythonscript that does this:

        import time
        
        time_formats_list = [
            "%I:%M %p %m/%d/%Y",     #  08:56 PM 06/02/2019
            "%I:%M:%S %p %m/%d/%Y",  #  08:56:15 PM 06/02/2019
            "%b %d %Y %H:%M:%S",     #  Jun 02 2019 20:56:15
            "%m/%d/%Y %H:%M:%S",     #  06/02/2019 20:56:15
            "%m/%d/%Y %H:%M",        #  06/02/2019 20:56
            "%A %b %d %Y %H:%M:%S",  #  Sunday Jun 02 2019 20:56:15
        ]
        
        # for format codes, see https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
        '''
        %a - Weekday as locale's abbreviated name. Sun, Mon, ..., Sat (en_US); So, Mo, ..., Sa (de_DE)
        %A - Weekday as locale's full name. Sunday, Monday, ..., Saturday (en_US); Sonntag, Montag, ..., Samstag (de_DE)
        %w - Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, ..., 6
        %d - Day of the month as a zero-padded decimal number. 01, 02, ..., 31
        %b - Month as locale's abbreviated name. Jan, Feb, ..., Dec (en_US); Jan, Feb, ..., Dez (de_DE)
        %B - Month as locale's full name. January, February, ..., December (en_US); Januar, Februar, ..., Dezember (de_DE)
        %m - Month as a zero-padded decimal number. 01, 02, ..., 12
        %y - Year without century as a zero-padded decimal number. 00, 01, ..., 99
        %Y - Year with century as a decimal number. 1970, 1988, 2001, 2013
        %H - Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
        %I - Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
        %p - Locale's equivalent of either AM or PM. AM, PM (en_US); am, pm (de_DE)
        %M - Minute as a zero-padded decimal number. 00, 01, ..., 59
        %S - Second as a zero-padded decimal number. 00, 01, ..., 59
        %f - Microsecond as a decimal number, zero-padded on the left. 000000, 000001, ..., 999999
        %z - UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). (empty), +0000, -0400, +1030
        %Z - Time zone name (empty string if the object is naive). (empty), UTC, EST, CST
        %j - Day of the year as a zero-padded decimal number. 001, 002, ..., 366
        %U - Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, ..., 53
        %W - Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, ..., 53
        %c - Locale's appropriate date and time representation. Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE)
        %x - Locale's appropriate date representation. 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE)
        %X - Locale's appropriate time representation. 21:30:00 (en_US); 21:30:00 (de_DE)
        %% - A literal '%' character. %
        '''
        
        instructions0 = "\r\nEnter letter of your choice"
        instructions1 = instructions0 + " or > for next group"
        instructions2 = instructions0 + " or >(next group) or <(previous group)"
        instructions3 = instructions0 + " or < for previous group"
        
        max_choices_fit_1_screen = 5
        
        prompt_number_start = 0
        
        while True:
        
            prompt_number_end = prompt_number_start + max_choices_fit_1_screen
            if prompt_number_end > prompt_number_start + len(time_formats_list):
                prompt_number_end = prompt_number_start + len(time_formats_list)
        
            prompt_body = ''
            for (j, v) in enumerate(time_formats_list[prompt_number_start : prompt_number_end + 1]):
                letter_choice = chr(ord('a') + prompt_number_start + j)
                example = time.strftime(v, time.localtime())
                prompt_body += ' {})     {}'.format(letter_choice, example)       # leading space is important!
                if prompt_number_start + j != len(time_formats_list) - 1: prompt_body += "\r\n"
        
            if prompt_number_start == 0:
                if len(time_formats_list) > max_choices_fit_1_screen:
                    instructions = instructions1
                else:
                    instructions = instructions0
            else:
                if prompt_number_end == len(time_formats_list):
                    instructions = instructions3
                else:
                    instructions = instructions2
        
            prompt_result = notepad.prompt(instructions, "Timestamp Insert (at caret)", prompt_body)
        
            if prompt_result == None: break  # user cancelled the dialog box
        
            if prompt_result == '<' or prompt_result == ',':
                # show previous group
                prompt_number_start -= max_choices_fit_1_screen
                if prompt_number_start < 0: prompt_number_start = 0
                continue
        
            if prompt_result == '>' or prompt_result == '.':
                # show next group
                prompt_number_start += max_choices_fit_1_screen
                if prompt_number_start + max_choices_fit_1_screen > len(time_formats_list):
                    prompt_number_start = len(time_formats_list) - max_choices_fit_1_screen
                    if prompt_number_start < 0: prompt_number_start = 0
                continue
        
            if len(prompt_result) > 0:
                user_choice_letter = prompt_result[0].lower()
                temp = ord(user_choice_letter) - ord('a')
                if temp >= 0 and temp < len(time_formats_list):
                    text = time.strftime(time_formats_list[temp], time.localtime())
                    curr_pos = editor.getCurrentPos()
                    editor.insertText(curr_pos, text)
                    editor.setEmptySelection(curr_pos + len(text))
                    break
        
        1 Reply Last reply Reply Quote 4
        • P
          PeterJones
          last edited by Jun 3, 2019, 1:14 PM

          @Alan-Kilborn said:

          So even though I didn’t get feedback from the OP yet, I came up with a little Pythonscript for this

          And you accuse me of addiction to the forum? :-)

          A A 2 Replies Last reply Jun 3, 2019, 1:17 PM Reply Quote 4
          • A
            andrecool-68 @PeterJones
            last edited by Jun 3, 2019, 1:17 PM

            @PeterJones Do not quarrel boys)))

            1 Reply Last reply Reply Quote 1
            • A
              Alan Kilborn @PeterJones
              last edited by Jun 3, 2019, 1:27 PM

              @PeterJones said:

              And you accuse me of addiction to the forum? :-)

              Yea, I TOTALLY KNEW that was coming!

              So oftentimes a forum posting pushes one into doing something that was already on the TODO list, or something on the even-more-populated started-but-never-finished list.

              :)

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