--- /dev/null
+//Compile with:
+//gcc -Wall -o eina_strbuf_02 eina_strbuf_02c `pkg-config --cflags --libs eina`
+
+#include <stdio.h>
+#include <Eina.h>
+
+int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
+{
+ Eina_Strbuf *buf;
+ time_t curr_time;
+ struct tm *info;
+
+ eina_init();
+
+ curr_time = time(NULL);
+ info = localtime(&curr_time);
+
+ buf = eina_strbuf_new();
+ eina_strbuf_append_strftime(buf, "%I:%M%p", info);
+ printf("current time: %s\n", eina_strbuf_string_get(buf));
+ eina_strbuf_reset(buf);
+
+ buf = eina_strbuf_new();
+ eina_strbuf_append(buf, "Hours: Minutes");
+ //insert hour at ^Hours: Minutes where ^ is the position
+ eina_strbuf_prepend_strftime(buf, "%I ", info);
+ //insert hour at hhhHours: ^Minutes where ^ is the position
+ eina_strbuf_insert_strftime(buf, "%M ", info, 10);
+ printf("%s\n", eina_strbuf_string_get(buf));
+
+ eina_strbuf_free(buf);
+ eina_shutdown();
+
+ return 0;
+}
return eina_strbuf_manage_new(str);
}
+EAPI Eina_Bool
+eina_strbuf_append_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm)
+{
+ char *outputbuf;
+
+ outputbuf = eina_strftime(format, tm);
+ if (!outputbuf) return EINA_FALSE;
+
+ eina_strbuf_append(buf, outputbuf);
+ free(outputbuf);
+
+ return EINA_TRUE;
+}
+
+EAPI Eina_Bool
+eina_strbuf_insert_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm, size_t pos)
+{
+ char *outputbuf;
+
+ outputbuf = eina_strftime(format, tm);
+ if (!outputbuf) return EINA_FALSE;
+
+ eina_strbuf_insert_length(buf, outputbuf, strlen(outputbuf), pos);
+ free(outputbuf);
+
+ return EINA_TRUE;
+}
+
/* Unicode */
#include "eina_strbuf_template_c.x"
EAPI char* eina_strbuf_release(Eina_Strbuf *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
/**
+ * @brief append the given buffer based on strftime output.
+ *
+ * @param tm Pointer to a tm structure needed by strftime.
+ * @param fmt String containing format specifiers needed by strftime.
+ * @return #EINA_TRUE on success, #EINA_FALSE on failure.
+ *
+ * This will add append buffer of exact required size based on strftime output
+ *
+ * Example usage:
+ * @code
+ * time_t curr_time;
+ * struct tm *info;
+ * Eina_Strbuf *buf = eina_strbuf_new();
+ * curr_time = time(NULL);
+ * info = localtime(&curr_time);
+ * eina_strbuf_append_strftime(buf, "%I:%M%p", info);
+ * //after use
+ * eina_strbuf_free(buf);
+ * @endcode #EINA_TRUE on success, #EINA_FALSE on failure.
+ *
+ * @since 1.21
+ */
+EAPI Eina_Bool eina_strbuf_append_strftime(Eina_Strbuf *buf, const char *fmt, const struct tm *tm);
+
+/**
+ * @brief insert the given buffer based on strftime output at given position
+ *
+ * @param buf The string buffer to prepend to.
+ * @param fmt String containing format specifiers needed by strftime.
+ * @param tm Pointer to a tm structure needed by strftime.
+ * @return #EINA_TRUE on success, #EINA_FALSE on failure.
+ *
+ * This will add append buffer of exact required size based on strftime output
+ *
+ * Example usage:
+ * @code
+ * time_t curr_time;
+ * struct tm *info;
+ * Eina_Strbuf *buf = eina_strbuf_new();
+ * curr_time = time(NULL);
+ * info = localtime(&curr_time);
+ * eina_strbuf_insert_strftime(buf, "%I:%M%p", info, 2);
+ * //after use
+ * eina_strbuf_free(buf);
+ * @endcode
+ *
+ * @since 1.21
+ */
+EAPI Eina_Bool eina_strbuf_insert_strftime(Eina_Strbuf *buf, const char *fmt, const struct tm *tm, size_t pos);
+
+/**
+ * @def eina_strbuf_prepend_strftime(buf, fmt, tm)
+ * @brief Prepends the given string to the given buffer.
+ *
+ * @param buf The string buffer to prepend to.
+ * @param fmt The string to prepend.
+ * @param tm The variable arguments.
+ * @return #EINA_TRUE on success, #EINA_FALSE on failure.
+ *
+ * This macro is calling eina_strbuf_insert_strftime() at position 0. If @p buf
+ * can't prepend it, #EINA_FALSE is returned, otherwise #EINA_TRUE is
+ * returned.
+ *
+ * @since 1.21
+ */
+#define eina_strbuf_prepend_strftime(buf, fmt, tm) eina_strbuf_insert_strftime(buf, fmt, tm, 0)
+
+/**
* @}
*/
}
END_TEST
+START_TEST(strbuf_strftime_test)
+{
+ Eina_Strbuf *buf;
+ time_t curr_time;
+ struct tm *info;
+ char cbuf[32];
+ const char *str;
+
+ curr_time = time(NULL);
+ info = localtime(&curr_time);
+
+ strftime(cbuf, 32, "%I:%M%p", info);
+
+ buf = eina_strbuf_new();
+ eina_strbuf_append_strftime(buf, "%I:%M%p", info);
+ str = eina_strbuf_string_get(buf);
+ fail_if(str == NULL || strcmp(str, cbuf) != 0);
+ eina_strbuf_reset(buf);
+
+ buf = eina_strbuf_new();
+ eina_strbuf_append(buf, "Hours: Minutes");
+ eina_strbuf_prepend_strftime(buf, "%I ", info);
+ eina_strbuf_insert_strftime(buf, "%M ", info, 10);
+ strftime(cbuf, 32, "%I Hours: %M Minutes", info);
+ str = eina_strbuf_string_get(buf);
+ fail_if(str == NULL || strcmp(str, cbuf) != 0);
+
+ eina_strbuf_free(buf);
+}
+
void
eina_test_strbuf(TCase *tc)
{
tcase_add_test(tc, strbuf_substr_get);
tcase_add_test(tc, strbuf_prepend_print);
tcase_add_test(tc, strbuf_release_test);
+ tcase_add_test(tc, strbuf_strftime_test);
}