Skip to content

Commit 6eb532c

Browse files
Add ISO string conversion for time series timestamps
Introduced getTimestampAsIsoString to TimeSeries, allowing retrieval of timestamps as ISO strings with optional time component.
1 parent 4775af0 commit 6eb532c

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

example/example.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,9 +2761,12 @@ void showAllProperties(RESQML2_NS::AbstractRepresentation const * rep, bool* ena
27612761
if (prop->getTimeSeries() != nullptr || singleTs != -1) {
27622762
std::cout << "\tThis property is a time related one" << std::endl;
27632763
if (singleTs != -1) {
2764-
std::cout << "\tThis property is a related to single timestamp " << std::endl;
2764+
std::cout << "\tThis property is related to single timestamp " << std::endl;
27652765
if (prop->getTimeSeries() != nullptr) {
2766-
std::cout << "which is part of a time series at index " << prop->getTimeSeries()->getTimestampIndex(singleTs) << std::endl;
2766+
auto timeSeriesIndex = prop->getTimeSeries()->getTimestampIndex(singleTs);
2767+
std::cout << "which is part of a time series at index " << timeSeriesIndex
2768+
<< " ISO String (without time)" << prop->getTimeSeries()->getTimestampAsIsoString(timeSeriesIndex, false)
2769+
<< " ISO String (with time)" << prop->getTimeSeries()->getTimestampAsIsoString(timeSeriesIndex, true) << std::endl;
27672770
}
27682771
else {
27692772
std::cout << "which is not part of a time series." << std::endl;

src/eml2/TimeSeries.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,22 @@ using namespace EML2_NS;
2727

2828
void TimeSeries::pushBackTimestamp(time_t timestamp)
2929
{
30-
std::tm tmConversion = timeTools::to_calendar_time(timeTools::from_time_t(timestamp));
30+
const std::tm tmConversion = timeTools::to_calendar_time(timeTools::from_time_t(timestamp));
3131
pushBackTimestamp(tmConversion);
3232
}
3333

3434
time_t TimeSeries::getTimestamp(uint64_t index) const
3535
{
36-
tm temp = getTimestampAsTimeStructure(index);
36+
const tm temp = getTimestampAsTimeStructure(index);
3737
return timeTools::timegm(temp);
3838
}
3939

40+
std::string TimeSeries::getTimestampAsIsoString(uint64_t index, bool withTime) const
41+
{
42+
const time_t temp = getTimestamp(index);
43+
return timeTools::convertUnixTimestampToIso(temp, withTime);
44+
}
45+
4046
std::vector<RESQML2_NS::AbstractProperty *> TimeSeries::getPropertySet() const
4147
{
4248
return getRepository()->getSourceObjects<RESQML2_NS::AbstractProperty>(this);

src/eml2/TimeSeries.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ namespace EML2_NS
104104
*/
105105
DLL_IMPORT_OR_EXPORT time_t getTimestamp(uint64_t index) const;
106106

107+
/**
108+
* Gets a timestamp as an ISO string at a particular index of this time series.
109+
*
110+
* @exception std::logic_error If the underlying gSOAP instance is not a RESQML2.0 one.
111+
* @exception std::out_of_range If @p index is out of range.
112+
*
113+
* @param index Zero-based index of the timestamp we look for.
114+
* @param withTime Include or not the time in the output.
115+
*
116+
* @returns The timestamp as an ISO string at position @p index.
117+
*/
118+
DLL_IMPORT_OR_EXPORT std::string getTimestampAsIsoString(uint64_t index, bool withTime = true) const;
119+
107120
/**
108121
* Gets a timestamp as a time structure at a particular index of this time series. It allows to
109122
* read dates from 1900-01-01T00:00:00.

src/tools/TimeTools.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ std::string timeTools::convertMicrosecondUnixTimestampToIso(long long ts)
4545
return oss.str();
4646
}
4747
*/
48-
std::string timeTools::convertUnixTimestampToIso(time_t ts)
48+
std::string timeTools::convertUnixTimestampToIso(time_t ts, bool withTime)
4949
{
5050
auto tmp = timeTools::from_time_t(ts);
51-
return date::format("%FT%TZ", date::floor<std::chrono::seconds>(tmp));
51+
return date::format(withTime ? "%FT%TZ" : "%F", date::floor<std::chrono::seconds>(tmp));
5252
}
5353

5454
time_t timeTools::convertIsoToUnixTimestamp(const std::string & s)

src/tools/TimeTools.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ namespace timeTools
3232
/**
3333
* Convert an Unix timestamp which is given in seconds into a date in ISO format.
3434
*
35-
* @param ts The ts.
35+
* @param ts The UNIX timestamp.
36+
* @param withTime Include or not the time in the output.
3637
*
3738
* @returns The unix converted timestamp to ISO.
3839
*/
39-
std::string convertUnixTimestampToIso(time_t ts);
40+
std::string convertUnixTimestampToIso(time_t ts, bool withTime = true);
4041

4142
/**
4243
* Converts a UTC time (given in seconds) represented by a string to a UTC time represented by a

0 commit comments

Comments
 (0)