Преглед на файлове

Added public toJulianDay format

gbeurier преди 7 години
родител
ревизия
89ec0e2ff3
променени са 2 файла, в които са добавени 40 реда и са изтрити 21 реда
  1. 22 21
      src/artis/utils/DateTime.cpp
  2. 18 0
      src/artis/utils/DateTime.hpp

+ 22 - 21
src/artis/utils/DateTime.cpp

@@ -86,12 +86,7 @@ struct intern_date
     long mminutes;
     long mseconds;
 
-    enum STR_FORMAT
-    {
-	extended, //"%Y-%m-%d %H:%M:%S"
-	ymd,      //"%Y-%m-%d"
-	hms       //"%H:%M:%S"
-    };
+
 
     intern_date() noexcept
 	: myear(1400)
@@ -133,11 +128,11 @@ struct intern_date
 	mseconds = std::floor(f);
     }
 
-    std::string toString(STR_FORMAT fmt) noexcept
+    std::string toString(DateFormat fmt) noexcept
     {
 	std::stringstream ss;
 
-	if (fmt != hms) {
+    if (fmt != DATE_FORMAT_HMS) {
 	    ss << myear << "-";
 	    if (mmonth < 10) {
 		ss << "0";
@@ -148,10 +143,10 @@ struct intern_date
 	    }
 	    ss << mday ;
 	}
-	if (fmt == extended) {
+    if (fmt == DATE_FORMAT_EXTENDED) {
 	    ss << " ";
 	}
-	if (fmt != ymd) {
+    if (fmt != DATE_FORMAT_YMD) {
 	    if (mhours < 10) {
 		ss << "0";
 	    }
@@ -173,11 +168,11 @@ struct intern_date
     //format : not extended = "%Y-%m-%d"
     //         extended     = "%Y-%m-%d %H:%M:%S"
     //return true if no error
-    bool fromString(const std::string& date, STR_FORMAT toparse) noexcept
+    bool fromString(const std::string& date, DateFormat toparse) noexcept
     {
 	bool error = false;
 
-	if (toparse == extended) {
+    if (toparse == DATE_FORMAT_EXTENDED) {
 	    //parse "%Y-%m-%d %H:%M:%S"
 	    try {
 		std::regex regex("([^\\s]+)\\s([^\\s]+)");
@@ -189,8 +184,8 @@ struct intern_date
 		    std::smatch match = *next;
 
 		    if (match.size() == 3) {
-			fromString(match[1].str(), ymd);
-			fromString(match[2].str(), hms);
+            fromString(match[1].str(), DATE_FORMAT_YMD);
+            fromString(match[2].str(), DATE_FORMAT_HMS);
 		    } else {
 			error = true;
 		    }
@@ -212,19 +207,19 @@ struct intern_date
 		    std::smatch match = *next;
 		    nbmatches++;
 		    if (nbmatches == 1) {
-			if (toparse == ymd) {
+            if (toparse == DATE_FORMAT_YMD) {
 			    myear = std::stol(match.str());
 			} else {
 			    mhours = std::stol(match.str());
 			}
 		    } else if (nbmatches == 3) {
-			if (toparse == ymd) {
+            if (toparse == DATE_FORMAT_YMD) {
 			    mmonth = std::stol(match.str());
 			} else {
 			    mminutes = std::stol(match.str());
 			}
 		    } else if (nbmatches == 5) {
-			if (toparse == ymd) {
+            if (toparse == DATE_FORMAT_YMD) {
 			    mday = std::stol(match.str());
 			} else {
 			    mseconds = std::stol(match.str());
@@ -596,7 +591,7 @@ std::string DateTime::toJulianDayNumber(unsigned long date)
 {
     intern_date d;
     d.fromJulianDay(static_cast<double>(date));
-    return d.toString(intern_date::ymd);
+    return d.toString(DATE_FORMAT_YMD);
 }
 
 
@@ -604,7 +599,7 @@ std::string DateTime::toJulianDayNumber(unsigned long date)
 long DateTime::toJulianDayNumber(const std::string& date)
 {
     intern_date d;
-    d.fromString(date, intern_date::ymd);
+    d.fromString(date, DATE_FORMAT_YMD);
     return d.julianDayNumber();
 }
 
@@ -613,7 +608,13 @@ std::string DateTime::toJulianDay(double date)
 {
     intern_date d;
     d.fromJulianDay(date);
-    return d.toString(intern_date::extended);
+    return d.toString(DATE_FORMAT_EXTENDED);
+}
+
+std::string DateTime::toJulianDayFmt(double date, DateFormat format){
+    intern_date d;
+    d.fromJulianDay(date);
+    return d.toString(format);
 }
 
 
@@ -622,7 +623,7 @@ double DateTime::toJulianDay(const std::string& date)
 {
 
     intern_date d;
-    d.fromString(date, intern_date::extended);
+    d.fromString(date, DATE_FORMAT_EXTENDED);
     return d.julianDay();
 }
 

+ 18 - 0
src/artis/utils/DateTime.hpp

@@ -46,6 +46,13 @@ enum DateTimeUnitOptions
     DATE_TIME_UNIT_YEAR
 };
 
+enum DateFormat
+{
+    DATE_FORMAT_EXTENDED, //"%Y-%m-%d %H:%M:%S"
+    DATE_FORMAT_YMD,      //"%Y-%m-%d"
+    DATE_FORMAT_HMS       //"%H:%M:%S"
+};
+
 class DateTime
 {
 public:
@@ -263,6 +270,17 @@ public:
      */
     static double toJulianDay(const std::string& date);
 
+    /**
+     * @brief Convert a julian date into a string in a given format.
+     * @code
+     * artis::utils::DateTime::toJulianDay(2454115.05486, DATE_FORMAT_YMD)) = "2001-10-9";
+     * @endcode
+     * @param date The date to convert.
+     * @param format The string format from enum artis::utils::DateFormat.
+     * @return A string representation of the julian day.
+     */
+    static std::string toJulianDayFmt(double date, artis::utils::DateFormat format);
+
                                   /* * * * */
 
     /**