4

I am able to use strftime() to convert the current time to a string, but now I need to work the other way around:

  • Convert a string to {time}
  • Modify the time value
  • Convert back to string

Is there a way of doing this within Vim? That is, without the help of external commands, like date.

4
  • I answered about the availability of native methods for doing something like this (bad news). As far as trying to think of a workaround it would be helpful if you gave more details. For example, how exactly would you be using the functionality? Are there specific string formats you're interested in or do you need a wide range? Other pertinent details?
    – B Layer
    Commented Feb 20, 2019 at 8:25
  • Is python an option? If yes, you should update your question. Also: title and question don't match.
    – Ralf
    Commented Feb 20, 2019 at 8:50
  • I'm using date and it is too slow (I'm guessing because it is system call) and was looking for a built-in alternative. Unfortunately, the answer is that there is none.
    – Vitor
    Commented Feb 22, 2019 at 5:33
  • @Vitor If date is slow for you due to standard library calls or (less likely) actual system calls then I'm guessing a native vim solution probably wouldn't be much help as it likely would use the same facilities. (Similar to how vim's strftime() is built on strftime(3).) What system are you using anyways? Might want to dig into why date is so slow.
    – B Layer
    Commented Feb 23, 2019 at 10:18

1 Answer 1

5

There's no native function to parse a string into a time. The other direction is possible, though, because the strftime() function you mentioned takes an optional second parameter, {time}.

:echo strftime("%Y %b %d %X", localtime())
2019 Feb 20 12:12:12

The form of the time is seconds since epoch (1/1/1970). localtime() returns current time in that form which is why I used it in the example.

By the way, strftime() isn't portable. It's system dependent both in it's presence or lack there of as well as determining what are valid format strings if it is present.

Since portability is out the window might as well use external commands (e.g. :!date ... if you're on *nix). :)

3
  • To be honest it's strange that the API has no way of generating {time} from anything else other than localtime. But I guess there wasn't the immediate need when these commands were added. And strftime may be portable if the format is well defined, no? %Y shall always return the same thing.
    – Vitor
    Commented Feb 22, 2019 at 5:38
  • I admit I was a little surprised that vim doesn't provide a reverse counterpart to strftime(). I can imagine any number of rationales for this, though. Regarding portability of the specifiers, it's funny that you mention %Y since that is one for which you'll find problem reports. That's due to the C strftime spec not indicating how many digits it should resolve to. Some systems use 2 and others 4. Here's a (dated) example. Googling something like "C strftime portability" will probably reveal other examples.
    – B Layer
    Commented Feb 23, 2019 at 9:52
  • 2
    Completely unaware that %Y could problematic. Thanks for sharing!
    – Vitor
    Commented Feb 24, 2019 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.