3

I have an application where I need to generate date and time based on the locale using the strftime function provided by C's time.h:

#include <stdio.h>
#include <time.h>
#include <locale.h>
#include<string.h>
int main() 
{
    //This is where date & time will be stored
    char buffer[10];

    // Set Japanese locale for LC_TIME
    //Just an example, japanese takes 33 bytes. But the buffer I provided is of 10 bytes
    // Installing japanese locale is another effort. So, instead of japanese you can try it by commenting the below setlocale. As even english date & time needs more space than 10.
    setlocale(LC_TIME, "ja_JP.UTF-8");

    // Initialize struct tm with some value, in this case September 26, 2025, at 12:27:05 PM
    struct tm tm1 = {
        .tm_sec = 5,
        .tm_min = 27,
        .tm_hour = 12,
        .tm_mday = 26,
        .tm_mon = 8,      
        .tm_year = 125,   
        .tm_wday = 5,     
        .tm_yday = 268,   
        .tm_isdst = 1     
    };

    //In this case, len will be 0.
    size_t len = strftime(buffer, sizeof(buffer), "%x %X", &tm1);

    //From here, I want to send some error to my application which expects and throws an error to terminal, based on errno.h.
    //ERANGE or EOVERFLOW or any other
}

The return value of strftime will be stored in rtnValue. If everything goes well, storeIn will contain the formatted date and time.

Different locales require different buffer sizes. However, there are cases where the date and time for a given locale can exceed the size specified by len. This can be mitigated by providing a larger buffer, but proper error handling is also necessary.

strftime does not set any specific error code like ERANGE or EOVERFLOW. Instead, it returns 0 when the provided buffer is too small, meaning no bytes were written. Due to requirements, I need to set an error code from errno.h. I am confused between ERANGE and EOVERFLOW.

EOVERFLOW sounds appropriate, and I have seen some implementations using it when a buffer overflow occurs. However, its description clearly states: “Value too large for defined data”, which suggests that the “too large” part refers to exceeding a data type’s limit rather than a manually set buffer size. Is my understanding correct? If so, this would not apply to my case.
ERANGE throws "Numerical result out of range" as the error, even though it is mostly used for math-related functions, certain websites state it as the input being outside the range which does sound right. But the error statement doesn't match with the issue.

Question:
What would be the most suitable error code for a situation like this?

Thank you in advance.

10
  • len - 1 is odd. Commented Nov 26 at 12:06
  • 2
    ERANGE would be the most portable since it's required to exist by the C standard while EOVERFLOW is not. ERANGE is not only mentioned in relation to math operations in the standard. It's used by conversion functions like strtoimax too. Commented Nov 26 at 12:19
  • 1
    Voting to reopen as this is not opinion-based, as @TedLyngmo comment demonstrates Commented Nov 26 at 12:27
  • 1
    @Ted Lyngmo, Re-opened, if you want to make that an answer. Commented Nov 26 at 14:46
  • 1
    @Micro Soft, "returns 0 when the provided buffer is too small, meaning no bytes were written." is not consistent with the C spec: "Otherwise, zero is returned and the members of the array have an indeterminate representation." implying bytes may have been written - they are indeterminate. Commented Nov 26 at 15:46

2 Answers 2

5

ERANGE would be the most portable since it's required to exist by the C standard while EOVERFLOW is not.

ERANGE is not only mentioned in relation to math operations in the standard. It's used by conversion functions like strtoimax too.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't understand the statement "since it's required to exist by the C standard".
@MicroSoft It means that the ISO specification of the C language includes ERANGE (but it does not include EOVERFLOW). You can see where ERANGE is mentioned in C23 first post-publication draft.
5

Standard C library only specs a few errno macros: EDOM EILSEQ ERANGE and 0 as no error.

Roughly:

  • EDOM trouble with input, the domain x of a function y = foo(x).

  • EILSEQ trouble with the sequence of input data.

  • ERANGE trouble with forming output, the range y of a function y = foo(x).

Since OP's issue relates to forming the output, ERANGE makes more sense.


Instead of using errno for passing the error info, I would consider other approaches.

1 Comment

For now there are restrictions on the way error info is passed. The problem with ERANGE is the comment being thrown is "Numerical result out of range" which doesnt sound right for my issue. Wherein EOVERFLOW throws "Value too large for defined data" which matches with my issue.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.