Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Minimize Date and Time Display Drudgery-3 : Page 3


advertisement
Notes on Code Safety
Visual Studio 2005 issues a spurious warning message when you use strftime(). You can safely ignore this warning (or better yet, disable it altogether). Remember, the key to code safety is awareness and through examination of strftime()'s behavior. My advice is to always test a function or class before using it in production code for the first time. In addition, consider the following issues:
  • Return Code: While most functions return zero to indicate success, strftime() returns a positive value on success, zero otherwise. This behavior is consistent with the rest of the printf() family of functions. If a strftime() call returns 0, you should also check the errno value to get more information about the cause of the failure:

    if (strftime(tmdescr, sizeof (tmdescr)-1, fmt, &now)==0) { cerr<<"strftime failed. Errno code: "<<errno<<endl; exit(EXIT_FAILURE); } else cout<<tmdescr<<endl;

  • Parameter Validation: If the format string contains an unrecognized escape sequence, the results are undefined. However, some implementations (Visual Studio 2005 for instance) validate every strftime() argument, including the escapes. Opt for an implementation that validates strftime() arguments carefully, paying attention to the format string in particular.


  • Thread-safety: In multithreaded apps, always copy the tm structure returned from localtime() and gmtime() to a private tm struct.
  • Buffer Overflows: The second parameter must be at least one byte smaller than the size of s because strftime() appends a terminating '\0' to s.
  • 64-bit Readiness: To ensure a smooth migration to 64-bits, stick to the time_t typedef. Implementations map time_t automatically to a 32-bit or a 64-bit representation (i.e., long long), depending on your compiler's settings and the target operating system. The code used in this 10-Minute Solution should run smoothly on both 32 and 64 systems, whereas __time64_t will only run on 64-bit Windows systems.

Efficient and Powerful
In spite of its old-fashioned look, strftime() enjoys immense popularity among developers. So much so that it has been ported to other programming languages such as PHP, Python, and Ruby. Believe it or not, even Java 1.5 has a Strftime class that uses a similar set of escape sequences. So don't frown at strftime(). When used with the proper security measures described above, strftime() can be an efficient and very powerful tool, particularly in internationalized projects. Furthermore, familiarity with strftime()'s interface will enable you to port C code to any of the aforementioned languages and environments.



Danny Kalev is a certified system analyst and software engineer specializing in C++. He was a member of the C++ standards committee between 1997 and 2000 and has since been involved informally in the C++0x standardization process. He is the author of "The ANSI/ISO Professional C++ Programmer's Handbook" and "The Informit C++ Reference Guide: Techniques, Insight, and Practical Advice on C++."
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap