Commit 3821dfeb authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding stringfunctions function utc_to_time to convert UTC and Zulu times to struct tm.

parent 0cdcca43
Pipeline #11140 failed with stage
in 3 minutes and 22 seconds
......@@ -121,4 +121,24 @@ std::string remove_extra_whitespace(const std::string &inputStr)
str.erase(new_end, str.end());
return trim_string(str);
}
struct tm utc_to_time(const std::string &time, int &zone, int &daylight)
{
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
int count = sscanf(time.c_str(), "%d-%d-%dT%d:%d:%d-%d:%d", &year, &month,
&day, &hour, &minute, &second, &zone, &daylight);
radix_line("utc_to_time elements: " << count);
struct tm res;
res.tm_year = year;
res.tm_mon = month;
res.tm_mday = day;
res.tm_hour = hour;
res.tm_min = minute;
res.tm_sec = second;
radix_line("Parsed time:" << year << "-" << month << "-" << day << "T" << hour
<< ":" << minute << ":" << second << "-" << zone
<< ":" << daylight);
return res;
}
} // namespace radix
......@@ -8,15 +8,25 @@
*/
#include <cstdio>
#include <ctime>
#include <fstream>
#include <string>
#include <type_traits>
#include <vector>
#include "radixdl/visibility.hh"
namespace radix
{
/**
* @brief utc_to_time UTC time to
* @param time UTC formatted date and time
* @param zone time zone
* @param daylight daylight savings
* @return struct tm
*/
struct tm RADIX_PUBLIC utc_to_time(const std::string &time, int &zone,
int &daylight);
/**
* Convert a string to lowercase
* @param the string to be converted
......
#include <iomanip>
#include <limits>
#include <sstream>
#include <type_traits>
#include "radixbug/bug.hh"
namespace radix
{
......
#include "gtest/gtest.h"
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
......@@ -11,6 +12,35 @@ using namespace radix;
typedef std::vector<std::string> Vec_Str;
TEST(StringFunctions, utc_to_time)
{
{
std::string utcDateTime = "1951-11-19T17:00:00-08:00";
int zone = 0, daylight = 0;
struct tm time = utc_to_time(utcDateTime, zone, daylight);
EXPECT_EQ(time.tm_year, 1951);
EXPECT_EQ(time.tm_mon, 11);
EXPECT_EQ(time.tm_mday, 19);
EXPECT_EQ(time.tm_hour, 17);
EXPECT_EQ(time.tm_min, 0);
EXPECT_EQ(time.tm_sec, 0);
EXPECT_EQ(zone, -8);
EXPECT_EQ(daylight, 0);
}
{
std::string utcDateTime = "1951-11-19T17:00:00Z";
int zone = 0, daylight = 0;
struct tm time = utc_to_time(utcDateTime, zone, daylight);
EXPECT_EQ(time.tm_year, 1951);
EXPECT_EQ(time.tm_mon, 11);
EXPECT_EQ(time.tm_mday, 19);
EXPECT_EQ(time.tm_hour, 17);
EXPECT_EQ(time.tm_min, 0);
EXPECT_EQ(time.tm_sec, 0);
EXPECT_EQ(zone, 0);
EXPECT_EQ(daylight, 0);
}
}
TEST(FindAndReplace, Basic)
{
EXPECT_EQ("x001", find_and_replace("x$f", "$f", "001"));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment