57

There is a strptime function in many language libraries (C, Python, Ruby, PHP, PERL, etc.).

It seems to be based on the Open Group's specification for time.h.

I understand 'str' stands for string, and 'time' obviously stands for time, but what does the 'p' stand for? Parse? Pointer? Print?

Every time I reach for the strptime() function, I have a mental blank, and have to look up the name in a manual. I figure if I finally worked out what it stood for, perhaps I would have a chance of remembering it.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
  • 10
    stands for parse... string parse time – jeremy Sep 04 '12 at 02:07
  • 1
    @Nile - So, how do you know that? – Oddthinking Sep 04 '12 at 02:48
  • 1
    I mean... does it really matter? Whatever you want to think of it as, parse, pointer, print... it could even stand for `POSIX`. Who knows what those guys were thinking. – jeremy Sep 04 '12 at 02:51
  • 4
    I think I explained my justification in the question. Further, if there *is* a good reason, it may help my understanding of other code, or even persuade me to adopt the standard... – Oddthinking Sep 04 '12 at 03:07
  • 1
    I mean, obviously you want a way to remember it. But if you just think of it as "string posix time" or "string pointer time" or "string parse time", it'll work.... – jeremy Sep 04 '12 at 03:14

9 Answers9

26

I guess it stands for "parse" because its reverse function is called strftime in Python's time module wherein the "f" I can reasonably guess stands for "format".

an0
  • 17,191
  • 12
  • 86
  • 136
  • 2
    I would like to say **f** stands for **from**. So that I always remember _strftime_ gives me a string **from** a datetime object. But I like your "parse" idea for strptime :) – Sacha Apr 17 '19 at 08:52
15

p = pointer. It returns a pointer to a char.

BTW According to my K&R there is a

char *strpbrk(cs,ct);

This 'p' also refers to the returned pointer.

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
  • 4
    if the p in `strptime` would refer to _pointer_ then I guess the f in `strftime` refers to _from pointer_? That seems unlikely. – miraculixx Feb 01 '16 at 12:30
  • 2
    `strptime` = str points to time = str -> time. `strftime` = str from time = str <- time. – Ken T Jul 14 '19 at 17:23
  • In Python is clear. `d1 = datetime.strptime('2019-12-25', '%Y:%m:%d') # str, parse, time. This creates a datetime object from a string`. And `d1.strftime('%d/%m/%Y')` formats the datetime as a string. – axell-brendow Dec 17 '19 at 09:25
9

Most of the places, I found

strftime() -> string format time &
strptime() -> string parsed time

8

I have same problem and I'm going with put:

strftime -> 'string from time'
strptime -> 'string, put time'
kztd
  • 3,121
  • 1
  • 20
  • 18
5

It helps me to remember:

  • p for produce, str p time -> string produce time
  • f for format, str f time -> string format time
user11681129
  • 51
  • 1
  • 1
1

I think of strftime() as string from time. And the function strptime() is, probably, derived from string parsed time.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

Here's a pneumonic I found helpful:

strftime -> string (forward) time : this function converts from a time object

strptime -> string (previous) time : this function converts from a string object

0

This is yet another way to remember this:

f for from, str f time (strftime)-> string from time

p for produce, str p time (strptime)-> string produces time

hermit
  • 1
  • 1
0

strftime: format a datetime into a string
strptime: parse a string into a datetime

inflrc
  • 1
  • 1