Skip to content

Commit

Permalink
feat: add temporal literal parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Jul 17, 2024
1 parent 360d444 commit 4f8eab3
Show file tree
Hide file tree
Showing 24 changed files with 1,957 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitattribute
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.ll text
*.yy text
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ option(ENABLE_COVERAGE "enable coverage on debug build" OFF)
include(GNUInstallDirs)

find_package(mpdecpp REQUIRED)
find_package(FLEX REQUIRED)
find_package(BISON 3.6 REQUIRED)

find_package(ICU 60
COMPONENTS
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ git submodule update --init --recursive
```dockerfile
FROM ubuntu:22.04

RUN apt update -y && apt install -y git build-essential cmake ninja-build libboost-container-dev libboost-stacktrace-dev libicu-dev
RUN apt update -y && apt install -y git build-essential cmake ninja-build libboost-container-dev libboost-stacktrace-dev libicu-dev flex bison
```

optional packages:
Expand All @@ -40,6 +40,25 @@ make -j4
make install # or sudo make install
```

#### GNU Bison `>= 3.6`

This project requires GNU Bison `>= 3.6`.
Please run `bison --version` and check the printed version.

```sh
# install packages to build bison
sudo apt update -y
sudo apt install -y curl m4

curl http://ftp.jaist.ac.jp/pub/GNU/bison/bison-3.6.4.tar.gz | tar zxv
cd bison-3.6.4
./configure --prefix=/path/to/install
make -j4
make install # or sudo make install
```

If you install the above to a non-standard path, please specify `-DCMAKE_PREFIX_PATH=</path/to/install>` to cmake.

## How to build

```sh
Expand Down
55 changes: 55 additions & 0 deletions include/takatori/datetime/conversion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <optional>
#include <string>

#include <takatori/util/either.h>

#include "conversion_info.h"

namespace takatori::datetime {

/**
* @brief represents the result of conversion, or error message.
* @tparam T the type of conversion result
*/
template<class T>
using conversion_result = util::either<std::string, T>;

/**
* @brief parses the given contents as a date.
* @details The date format must be `YYYY-MM-DD`.
* @param contents the source contents
* @return the parsed date information
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<date_info> parse_date(std::string const& contents);

/**
* @brief parses the given contents as a time of day.
* @details The date format must be `hh:mm:ss.SSSSSSSSS`.
* @param contents the source contents
* @return the parsed time of day information
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<time_info> parse_time(std::string const& contents);

/**
* @brief parses the given contents as a datetime, with or without zone offset.
* @details The date format must be `YY-MM-DD hh:mm:ss.SSSSSSSSS+HH:MM`.
* @param contents the source contents
* @return the parsed datetime information with or without zone offset
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<datetime_info> parse_datetime(std::string const& contents);

/**
* @brief parses the given contents as a zone offset.
* @details The date format must be `+HH:MM` or just `Z`.
* @param contents the source contents
* @return the parsed zone offset
* @return an error message if the conversion was failed
*/
[[nodiscard]] conversion_result<zone_offset_info> parse_zone_offset(std::string const& contents);

} // namespace takatori::datetime
76 changes: 76 additions & 0 deletions include/takatori/datetime/conversion_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <chrono>
#include <optional>

#include <cstdint>

namespace takatori::datetime {

/**
* @brief represents date information.
*/
struct date_info {

/// @brief the year field of date.
std::uint32_t year {};

/// @brief the month field of date.
std::uint32_t month {};

/// @brief the day of month field of date.
std::uint32_t day {};
};

/**
* @brief represents time of day information.
*/
struct time_info {

/// @brief the sub-second field type.
using subsecond_type = std::chrono::duration<std::uint32_t, std::nano>;

/// @brief the hour field of time of day.
std::uint32_t hour {};

/// @brief the minute field of time of day.
std::uint32_t minute {};

/// @brief the second field of time of day.
std::uint32_t second {};

/// @brief the sub-second field of time of day.
subsecond_type subsecond {};
};

/**
* @brief represents timezone offset information.
*/
struct zone_offset_info {

/// @brief the sign of timezone offset.
bool plus { true };

/// @brief the hour field of timezone offset.
std::uint32_t hour {};

/// @brief the minute field of timezone offset.
std::uint32_t minute {};
};

/**
* @brief represents datetime information.
*/
struct datetime_info {

/// @brief the date information.
date_info date;

/// @brief the time of day information.
time_info time;

/// @brief the optional timezone offset information.
std::optional<zone_offset_info> offset;
};

} // namespace takatori::datetime
Loading

0 comments on commit 4f8eab3

Please sign in to comment.