aqnwb 0.3.0
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <chrono>
6#include <cmath>
7#include <cstdint>
8#include <ctime>
9#include <iomanip>
10#include <random>
11#include <regex>
12#include <sstream>
13#include <string>
14
15#include "io/BaseIO.hpp"
16#include "io/hdf5/HDF5IO.hpp"
17
18namespace AQNWB
19{
20namespace detail
21{
27inline std::tm to_local_time(std::time_t time_value)
28{
29 std::tm local_tm {};
30#if defined(_WIN32)
31 localtime_s(&local_tm, &time_value);
32#elif defined(__unix__) || defined(__APPLE__)
33 localtime_r(&time_value, &local_tm);
34#else
35 const std::tm* local_tm_ptr = std::localtime(&time_value);
36 if (local_tm_ptr) {
37 local_tm = *local_tm_ptr;
38 }
39#endif
40 return local_tm;
41}
42
48inline std::tm to_utc_time(std::time_t time_value)
49{
50 std::tm utc_tm {};
51#if defined(_WIN32)
52 gmtime_s(&utc_tm, &time_value);
53#elif defined(__unix__) || defined(__APPLE__)
54 gmtime_r(&time_value, &utc_tm);
55#else
56 const std::tm* utc_tm_ptr = std::gmtime(&time_value);
57 if (utc_tm_ptr) {
58 utc_tm = *utc_tm_ptr;
59 }
60#endif
61 return utc_tm;
62}
63
69inline long get_utc_offset_seconds(std::time_t time_value)
70{
71#if defined(__unix__) || defined(__APPLE__)
72 std::tm local_tm = to_local_time(time_value);
73 return local_tm.tm_gmtoff;
74#elif defined(_WIN32)
75 long tz_seconds = 0;
76 _get_timezone(&tz_seconds);
77 std::tm local_tm = to_local_time(time_value);
78 if (local_tm.tm_isdst > 0) {
79 long dstbias = 0;
80 _get_dstbias(&dstbias);
81 tz_seconds += dstbias;
82 }
83 return -tz_seconds;
84#else
85 // Fallback: force utc_tm.tm_isdst to match local so mktime treats both
86 // consistently, avoiding the DST double-count.
87 std::tm local_tm = to_local_time(time_value);
88 std::tm utc_tm = to_utc_time(time_value);
89 utc_tm.tm_isdst = local_tm.tm_isdst;
90 std::time_t local_time = std::mktime(&local_tm);
91 std::time_t utc_time = std::mktime(&utc_tm);
92 if (local_time == static_cast<std::time_t>(-1)
93 || utc_time == static_cast<std::time_t>(-1))
94 {
95 return 0;
96 }
97 return static_cast<long>(std::difftime(local_time, utc_time));
98#endif
99}
100
106inline std::string format_utc_offset(long offset_seconds)
107{
108 const char sign = (offset_seconds < 0) ? '-' : '+';
109 long abs_offset = (offset_seconds < 0) ? -offset_seconds : offset_seconds;
110 long hours = abs_offset / 3600;
111 long minutes = (abs_offset % 3600) / 60;
112
113 std::ostringstream oss;
114 oss << sign << std::setw(2) << std::setfill('0') << hours << ':'
115 << std::setw(2) << std::setfill('0') << minutes;
116 return oss.str();
117}
118
124inline uint16_t to_little_endian_u16(uint16_t value)
125{
126#if defined(_WIN32) \
127 || (defined(__BYTE_ORDER__) \
128 && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
129 return value;
130#else
131 return static_cast<uint16_t>((value >> 8) | (value << 8));
132#endif
133}
134} // namespace detail
135
140static inline std::string generateUuid()
141{
142 std::array<uint8_t, 16> bytes {};
143 std::random_device rd;
144 std::mt19937 gen(rd());
145 std::uniform_int_distribution<uint32_t> dist(0, 0xFFFFFFFF);
146
147 for (size_t i = 0; i < bytes.size(); i += 4) {
148 uint32_t random_value = dist(gen);
149 bytes[i] = static_cast<uint8_t>(random_value & 0xFF);
150 bytes[i + 1] = static_cast<uint8_t>((random_value >> 8) & 0xFF);
151 bytes[i + 2] = static_cast<uint8_t>((random_value >> 16) & 0xFF);
152 bytes[i + 3] = static_cast<uint8_t>((random_value >> 24) & 0xFF);
153 }
154
155 // RFC 4122 version 4 UUID.
156 bytes[6] = static_cast<uint8_t>((bytes[6] & 0x0F) | 0x40);
157 bytes[8] = static_cast<uint8_t>((bytes[8] & 0x3F) | 0x80);
158
159 std::ostringstream oss;
160 oss << std::hex << std::nouppercase << std::setfill('0');
161 for (size_t i = 0; i < bytes.size(); ++i) {
162 if (i == 4 || i == 6 || i == 8 || i == 10) {
163 oss << '-';
164 }
165 oss << std::setw(2) << static_cast<int>(bytes[i]);
166 }
167 return oss.str();
168}
169
174static inline std::string getCurrentTime()
175{
176 auto now = std::chrono::system_clock::now();
177 auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
178 auto micros =
179 std::chrono::duration_cast<std::chrono::microseconds>(now - seconds)
180 .count();
181 std::time_t time_value = std::chrono::system_clock::to_time_t(seconds);
182 std::tm local_tm = detail::to_local_time(time_value);
183 long offset_seconds = detail::get_utc_offset_seconds(time_value);
184
185 std::ostringstream oss;
186 oss << std::put_time(&local_tm, "%Y-%m-%dT%H:%M:%S");
187 oss << '.' << std::setw(6) << std::setfill('0') << micros;
188 oss << detail::format_utc_offset(offset_seconds);
189 return oss.str();
190}
191
200static inline bool isISO8601Date(const std::string& dateStr)
201{
202 // Define the regex pattern for ISO 8601 extended format with timezone offset
203 // Allow one or more fractional seconds digits
204 const std::string iso8601Pattern =
205 R"(^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+[+-]\d{2}:\d{2}$)";
206 std::regex pattern(iso8601Pattern);
207
208 // Check if the date string matches the regex pattern
209 return std::regex_match(dateStr, pattern);
210}
211
219static inline std::shared_ptr<IO::BaseIO> createIO(const std::string& type,
220 const std::string& filename)
221{
222 if (type == "HDF5") {
223 return std::make_shared<AQNWB::IO::HDF5::HDF5IO>(filename);
224 } else {
225 throw std::invalid_argument("Invalid IO type");
226 }
227}
228
242static inline std::string mergePaths(const std::string& path1,
243 const std::string& path2)
244{
245 std::string result = path1;
246 // Remove trailing "/" from path1
247 while (!result.empty() && result.back() == '/' && result != "/") {
248 result.pop_back();
249 }
250 // Remove leading "/" from path2
251 size_t start = 0;
252 while (start < path2.size() && path2[start] == '/') {
253 start++;
254 }
255 // Get path2 without trailing slashes
256 std::string path2Clean = path2.substr(start);
257 while (!path2Clean.empty() && path2Clean.back() == '/' && path2Clean != "/") {
258 path2Clean.pop_back();
259 }
260 // Append path2 to path1 with a "/" in between
261 if (!result.empty() && !path2Clean.empty()) {
262 result += '/';
263 }
264 result += path2Clean;
265
266 // Remove any potential occurrences of "//" and replace with "/"
267 size_t pos = result.find("//");
268 while (pos != std::string::npos) {
269 result.replace(pos, 2, "/");
270 pos = result.find("//", pos);
271 }
272
273 // Remove trailing "/" from final result if not root path
274 while (!result.empty() && result.back() == '/' && result != "/") {
275 result.pop_back();
276 }
277
278 return result;
279}
280
289static inline void convertFloatToInt16LE(const float* source,
290 void* dest,
291 SizeType numSamples)
292{
293 // TODO - several steps in this function may be unnecessary for our use
294 // case. Consider simplifying the intermediate cast to char and the
295 // final cast to uint16_t.
296 auto maxVal = static_cast<double>(0x7fff);
297 auto intData = static_cast<char*>(dest);
298
299 for (SizeType i = 0; i < numSamples; ++i) {
300 auto clampedValue =
301 std::clamp(maxVal * static_cast<double>(source[i]), -maxVal, maxVal);
302 auto intValue =
303 static_cast<uint16_t>(static_cast<int16_t>(std::round(clampedValue)));
304 intValue = detail::to_little_endian_u16(intValue);
305 *reinterpret_cast<uint16_t*>(intData) = intValue;
306 intData += 2; // destBytesPerSample is always 2
307 }
308}
309
316static inline std::unique_ptr<int16_t[]> transformToInt16(
317 SizeType numSamples, float conversion_factor, const float* data)
318{
319 std::unique_ptr<float[]> scaledData = std::make_unique<float[]>(numSamples);
320 std::unique_ptr<int16_t[]> intData = std::make_unique<int16_t[]>(numSamples);
321
322 // copy data and multiply by scaling factor
323 float multFactor = 1.0f / (32767.0f * conversion_factor);
324 std::transform(data,
325 data + numSamples,
326 scaledData.get(),
327 [multFactor](float value) { return value * multFactor; });
328
329 // convert float to int16
330 convertFloatToInt16LE(scaledData.get(), intData.get(), numSamples);
331
332 return intData;
333}
334
340static inline bool isValidIndex(SizeType index)
341{
342 return (index != AQNWB::Types::SizeTypeNotSet);
343}
344
351static inline Status intToStatus(int status)
352{
353 return (status < 0) ? Status::Failure : Status::Success;
354}
355
361static inline void checkStatus(Status status, const std::string& operation)
362{
363 if (status != Status::Success) {
364 std::cerr << operation << " failed" << std::endl;
365 }
366}
367
368} // namespace AQNWB
AQNWB::Types::Status Status
Definition BaseIO.hpp:21
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
static constexpr SizeType SizeTypeNotSet
Value to use to indicate that a SizeType index is not set.
Definition Types.hpp:105
Definition Utils.hpp:21
std::tm to_utc_time(std::time_t time_value)
Convert a std::time_t value to a UTC std::tm structure.
Definition Utils.hpp:48
std::tm to_local_time(std::time_t time_value)
Convert a std::time_t value to a local std::tm structure.
Definition Utils.hpp:27
std::string format_utc_offset(long offset_seconds)
Format a UTC offset in seconds as a string (+HH:MM or -HH:MM).
Definition Utils.hpp:106
uint16_t to_little_endian_u16(uint16_t value)
Convert a 16-bit unsigned integer to little-endian byte order.
Definition Utils.hpp:124
long get_utc_offset_seconds(std::time_t time_value)
Get the UTC offset in seconds for a given time_t value.
Definition Utils.hpp:69
The main namespace for AqNWB.
Definition Channel.hpp:11
static Status intToStatus(int status)
Convert an integer status code to a Types::Status enum value. Shorthand for return (status < 0) ?...
Definition Utils.hpp:351
static std::unique_ptr< int16_t[]> transformToInt16(SizeType numSamples, float conversion_factor, const float *data)
Method to scale float values and convert to int16 values.
Definition Utils.hpp:316
static void convertFloatToInt16LE(const float *source, void *dest, SizeType numSamples)
Method to convert float values to uint16 values. This method was adapted from JUCE AudioDataConverter...
Definition Utils.hpp:289
static std::string mergePaths(const std::string &path1, const std::string &path2)
Merge two paths into a single path, handling extra trailing and starting "/".
Definition Utils.hpp:242
static bool isISO8601Date(const std::string &dateStr)
Check that a string is formatted in ISO8601 format.
Definition Utils.hpp:200
static std::string generateUuid()
Generates a UUID (Universally Unique Identifier) as a string.
Definition Utils.hpp:140
static void checkStatus(Status status, const std::string &operation)
Check status and print to standard error.
Definition Utils.hpp:361
static std::shared_ptr< IO::BaseIO > createIO(const std::string &type, const std::string &filename)
Factory method to create an IO object of the specified type.
Definition Utils.hpp:219
static std::string getCurrentTime()
Get the current time in ISO 8601 format with the UTC offset.
Definition Utils.hpp:174
static bool isValidIndex(SizeType index)
Check if a SizeType index is valid (i.e., not equal to SizeTypeNotSet).
Definition Utils.hpp:340