diff --git a/src/xpdev/xpdatetime.c b/src/xpdev/xpdatetime.c index 694c9875c23e2eab0d0e8a39b659624fdbf494b6..e4251955ff3fee54593f5ce773ffb9e0746d57ed 100644 --- a/src/xpdev/xpdatetime.c +++ b/src/xpdev/xpdatetime.c @@ -267,19 +267,35 @@ char* xpDate_to_isoDateStr(xpDate_t date, const char* sep, char* str, size_t max return str; } +/* precision example output + * -2 "14" + * -1 "14:02" + * 0 "14:02:39" + * 1 "14.02:39.8" + * 2 "14.02:39.82" + * 3 "14.02:39.829" + */ char* xpTime_to_isoTimeStr(xpTime_t time, const char* sep, int precision ,char* str, size_t maxlen) { if(sep==NULL) sep=":"; - snprintf(str, maxlen, "%02lu%s%02lu%s%0*.*f" - ,time.hour ,sep - ,time.minute ,sep - ,precision ? (precision+3) : 2 - ,precision - ,time.second - ); + if(precision < -1) /* HH */ + snprintf(str, maxlen, "%02lu", time.hour); + else if(precision < 0) /* HH:MM */ + snprintf(str, maxlen, "%02lu%s%02lu" + ,time.hour ,sep + ,time.minute + ); + else /* HH:MM:SS[.fract] */ + snprintf(str, maxlen, "%02lu%s%02lu%s%0*.*f" + ,time.hour ,sep + ,time.minute ,sep + ,precision ? (precision+3) : 2 + ,precision + ,time.second + ); return str; } diff --git a/src/xpdev/xpdatetime.h b/src/xpdev/xpdatetime.h index 360423f781634181ef84f9ebfc9cdcc69409dfba..61ce9fdb3a94014f6b9ac07d0608162fdb457938 100644 --- a/src/xpdev/xpdatetime.h +++ b/src/xpdev/xpdatetime.h @@ -126,6 +126,17 @@ isoDate_t xpDateTime_to_isoDateTime(xpDateTime_t, isoTime_t*); /*****************************************************************/ /* Conversion from xpDate/Time/Zone to isoDate/Time/Zone Strings */ /*****************************************************************/ + +/* NULL sep (separator) values are automatically replaced with ISO-standard separators */ + +/* precision example output + * -2 "14" + * -1 "14:02" + * 0 "14:02:39" + * 1 "14.02:39.8" + * 2 "14.02:39.82" + * 3 "14.02:39.829" + */ char* xpDate_to_isoDateStr(xpDate_t ,const char* sep ,char* str, size_t maxlen);