Andrea Pappacoda ac0c58d5da Stop using PATH_MAX
PATH_MAX isn't guaranteed to be defined in Posix environments; it is
only on systems that have a path length limit, and even in environments
where it is defined its usage can lead to issues.

To avoid using PATH_MAX, I've made two main changes:

- Where realpath() was used, I've changed the code to use its
  [POSIX.1-2008]'s new behaviour, where passing a null pointer as the
  resolved_name buffer results in realpath() to automatically allocate
  a buffer large enough to handle the given path, that is returned to
  the caller. This has been supported for a long time as a GNU libc
  extension before being standardized.
- Where readlink() was used, the size of the buffer was already
  determined when calling lstat(); the returned struct stat contains a
  st_size field, containing the number of bytes needed to store the
  symbolic link contents. This meant that to avoid using the tricky
  define I only needed to use a dynamically allocated buffer instead of
  a static one, of size stat.st_size (+1 when a null terminator is
  needed).

To make sure that memory is always freed, I've wrapped the new dynamic
allocations in an std::unique_ptr. The pointer returned by realpath()
must be freed with free(), so a unique_ptr with a custom deleter that
calls free() on destruction was used.

To read more about why PATH_MAX leads to buggy code I'd suggest reading
something like this: <https://eklitzke.org/path-max-is-tricky>.

[POSIX.1-2008]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
2024-09-28 16:56:16 -04:00
2024-09-28 16:56:16 -04:00
2024-09-28 16:41:52 -04:00
2017-01-26 19:42:42 -05:00
2021-08-31 12:40:28 -04:00

Date

Build Status Join the chat at https://gitter.im/HowardHinnant/date


Try it out on wandbox!

Summary

This is actually several separate C++11/C++14/C++17 libraries:

  1. "date.h" is a header-only library which builds upon <chrono>. It adds some new duration types, and new time_point types. It also adds "field" types such as year_month_day which is a struct {year, month, day}. And it provides convenient means to convert between the "field" types and the time_point types.

  2. "tz.h" / "tz.cpp" are a timezone library built on top of the "date.h" library. This timezone library is a complete parser of the IANA timezone database. It provides for an easy way to access all of the data in this database, using the types from "date.h" and <chrono>. The IANA database also includes data on leap seconds, and this library provides utilities to compute with that information as well.

  3. "iso_week.h" is a header-only library built on top of the "date.h" library which implements the ISO week date calendar.

  4. "julian.h" is a header-only library built on top of the "date.h" library which implements a proleptic Julian calendar which is fully interoperable with everything above.

  5. "islamic.h" is a header-only library built on top of the "date.h" library which implements a proleptic Islamic calendar which is fully interoperable with everything above.

Standardization

Slightly modified versions of "date.h" and "tz.h" were voted into the C++20 working draft at the Jacksonville FL meeting on 2018-03-17:

Build & Test

The recommended way to use any of these libraries besides "tz.h" is to just include it. These are header-only libraries (except "tz.h").

To use "tz.h", there is a single source file (src/tz.cpp) that needs to be compiled. Here are the recommended directions: https://howardhinnant.github.io/date/tz.html#Installation.

One can run tests by cd'ing into the test subdirectory and running testit. There are known failures on all platforms except for macOS. And even on macOS if C++11 is used. If any of these failures present problems for you, there exist workarounds.

Additionally there is unsupported support for vcpkg and CMake. I don't personally use or maintain these systems as for me they cause more problems than they solve (for this small project). If you would like to contribute to these build systems please feel free to file a PR.

You can download and install Date using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install date

The Date port in vcpkg is updated by Microsoft team members and community contributors. If the version falls behind, please create an issue or pull request on the vcpkg repository.

You can optionally build using CMake. Here is a guide of how to build and test using the CMake Makefile generator.

mkdir build
cd build
cmake -DENABLE_DATE_TESTING=ON -DBUILD_TZ_LIB=ON ../
cmake --build . --target testit # Consider '-- -j4' for multithreading

Projects using this library

If you would like your project (or product) on this list, just let me know.

Description
A date and time library based on the C++11/14/17 header
Readme
Languages
C++ 97.5%
CMake 1%
Objective-C++ 0.9%
Shell 0.5%
C 0.1%