Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:46:01+00:00 2026-06-15T10:46:01+00:00

POSIX defines a handy function, strptime , that can be used for parsing dates

  • 0

POSIX defines a handy function, strptime, that can be used for parsing dates and times. Thus, theoretically, if I have a date of the format “YYYY-MM-DD”, I should be able to use strptime to parse it like this:

char myDate[] = "2012-01-01";

struct tm result;
char *end = strptime(myDate, "%Y-%m-%d", &result);

… and get it back out in its canonical representation with:

if (end != NULL)
{
    char outDate[11];
    strftime(outDate, sizeof(outDate), "%Y-%m-%d", &result);
    printf("%s\n", outDate);
}
else
    printf("Invalid date\n");

On OS X and Linux, this prints out 2012-01-01. So far so good! However, let’s say my input date is in the wrong format: 01-01-2012.

If I run the above code again, on OS X, I get “Invalid date”, which is expected. However, on Linux, I get 1-01-20 — January 20th, 1 (yes, year one).

OS X follows the specifiers strictly, parsing a string as %Y only where a four-digit year exists. Linux takes a few liberties, though, and interprets two digits as a year — it doesn’t even appear that it assumes it’s 2001, it treats it as year 1!

This can be worked around by changing my if statement to something like

if (end != NULL && *end == '\0')

… but that seems hokey. Does anyone know if it’s possible to make strptime on Linux behave more strictly, and fail for %Y if the input string does not have a four-digit year?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:46:01+00:00Added an answer on June 15, 2026 at 10:46 am

    There is no option to change strptime()’s behavior or to specify a length within the format string. (I had a look into the sources in glibc-2.3, strptime discards format modifiers and field width, so something like “%04Y” wont change a thing).

    But as was already pointed out in the comments, there is nothing wrong with if (end != NULL && *end == '\0'). I’d recommend to use just that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a POSIX function that searches PATH for an executable according to the
Is there a POSIX function that works like the which command? That is, I
POSIX Expression is giving me a headache. Lets say we have a string: a
I am writing a POSIX shell script that may or may not receive input
I have two Slackware Linux systems on which the POSIX semaphore sem_open() call fails
I am trying a write a program that implements a bare bones POSIX cat
I recently became aware that the strdup() function I've enjoyed using so much on
in a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism
I have question regarding shared memory segmentation in c using POSIX system calls. Is
I have some questions regarding read-write locks in POSIX Pthreads on a *nix system,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.