3#ifndef _TIME_SHIELD_ISO_WEEK_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_ISO_WEEK_CONVERSIONS_HPP_INCLUDED
44 template<
class Y = year_t,
class M = Month,
class D =
int>
54 template<
class Y = year_t,
class M = Month,
class D =
int>
61 const year_t iso_year = thursday_date.year;
65 const uday_t first_thursday = jan4_day +
static_cast<uday_t>(4 - jan4_iso_weekday);
67 const int32_t week =
static_cast<int32_t
>((thursday_day - first_thursday) /
DAYS_PER_WEEK + 1);
82 template<
class T = ts_t>
93 return static_cast<int>(info.
week);
102 if (iso_year <
MIN_YEAR)
return false;
103 if (iso_year >
MAX_YEAR)
return false;
105 if (week < 1)
return false;
107 return week <= max_week;
116 throw std::invalid_argument(
"Invalid ISO week date");
121 const uday_t first_thursday = jan4_day +
static_cast<uday_t>(4 - jan4_iso_weekday);
136 throw std::invalid_argument(
"Invalid ISO week date");
139 if (!include_weekday) {
140 const char* fmt = extended ?
"%" PRId64
"-W%.2d" :
"%" PRId64
"W%.2d";
141 char buffer[32] = {0};
142 std::snprintf(buffer,
sizeof(buffer), fmt, iso_date.
year, iso_date.
week);
143 return std::string(buffer);
146 const char* fmt = extended ?
"%" PRId64
"-W%.2d-%d" :
"%" PRId64
"W%.2d%d";
147 char buffer[32] = {0};
148 std::snprintf(buffer,
sizeof(buffer), fmt, iso_date.
year, iso_date.
week, iso_date.
weekday);
149 return std::string(buffer);
158 if (input ==
nullptr) {
164 const char* p = input;
165 const char*
const end = input + length;
167 bool negative =
false;
168 if (p < end && (*p ==
'+' || *p ==
'-')) {
169 negative = (*p ==
'-');
173 const char* start_digits = p;
175 while (p < end && std::isdigit(
static_cast<unsigned char>(*p)) != 0) {
176 value = value * 10 +
static_cast<int64_t
>(*p -
'0');
180 if (p == start_digits)
return false;
181 iso_date.year = negative ? -value : value;
183 if (p >= end)
return false;
185 const bool has_dash_after_year = (*p ==
'-');
186 if (has_dash_after_year) {
188 if (p >= end)
return false;
191 if (*p !=
'W' && *p !=
'w')
return false;
195 for (
int i = 0; i < 2; ++i) {
196 if (p >= end || std::isdigit(
static_cast<unsigned char>(*p)) == 0)
return false;
197 week = week * 10 + (*p -
'0');
201 if (week == 0)
return false;
203 bool has_weekday =
false;
205 if ((*p ==
'-' && has_dash_after_year) || (!has_dash_after_year && std::isdigit(
static_cast<unsigned char>(*p)) == 0)) {
207 if (p >= end)
return false;
208 if (std::isdigit(
static_cast<unsigned char>(*p)) == 0)
return false;
209 iso_date.weekday = *p -
'0';
212 }
else if (std::isdigit(
static_cast<unsigned char>(*p)) != 0) {
213 iso_date.weekday = *p -
'0';
220 iso_date.weekday = 1;
223 iso_date.week = week;
225 if (p != end)
return false;
Configuration macros for the library.
Header file with time-related constants.
Header for date structure and related functions.
Header for date and time structure and related functions.
constexpr int64_t DAYS_PER_WEEK
Days per week.
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t MIN_YEAR
Minimum representable year.
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
std::string format_iso_week_date(const IsoWeekDateStruct &iso_date, bool extended=true, bool include_weekday=true)
Format ISO week date to string.
TIME_SHIELD_CONSTEXPR uday_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
bool is_valid_iso_week_date(year_t iso_year, int week, int weekday)
Validate ISO week date components.
TIME_SHIELD_CONSTEXPR T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
int iso_weeks_in_year(year_t iso_year)
Calculate number of ISO weeks in a year.
IsoWeekDateStruct to_iso_week_date(Y year, M month, D day)
Convert calendar date to ISO week date.
DateStruct iso_week_date_to_date(const IsoWeekDateStruct &iso_date)
Convert ISO week date to calendar date.
TIME_SHIELD_CONSTEXPR int iso_weekday_of_date(Y year, M month, D day)
Get ISO weekday for a calendar date.
TIME_SHIELD_CONSTEXPR int iso_weekday_from_weekday(Weekday weekday) noexcept
Convert Weekday enum to ISO weekday (Mon=1 .. Sun=7).
bool parse_iso_week_date(const char *input, std::size_t length, IsoWeekDateStruct &iso_date) noexcept
Parse ISO week date string buffer.
bool try_parse_iso_week_date(const char *input, std::size_t length, IsoWeekDateStruct &iso_date) noexcept
Alias for parse_iso_week_date.
constexpr T1 weekday(year_t year, int month, int day)
Alias for day_of_week_date.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
constexpr T unix_day_to_ts(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
const DateStruct create_date_struct(int64_t year, int32_t mon=1, int32_t day=1)
Creates a DateStruct instance.
const IsoWeekDateStruct create_iso_week_date_struct(int64_t year, int32_t week=1, int32_t weekday=1)
Creates an IsoWeekDateStruct instance.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
int64_t uday_t
Unix day count since 1970‑01‑01 (days since epoch).
int64_t year_t
Year as an integer (e.g., 2024).
Header for ISO week date structure.
Main namespace for the Time Shield library.
Structure to represent a date.
int32_t mon
Month component of the date (1-12).
int32_t day
Day component of the date (1-31).
int64_t year
Year component of the date.
Structure to represent date and time.
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int mon
Month component of the date (1-12).
Structure to represent an ISO week date.
int64_t year
ISO week-numbering year component.
int32_t week
ISO week number component (1-52/53).
int32_t weekday
ISO weekday component (1=Monday .. 7=Sunday).
Umbrella header for time conversion functions.
Conversions related to UNIX-based time units and epochs.