+ strcpy(dt+7, dt+9);
+ return dt;
+}
+
+
+/* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
+char *bstrutime(char *dt, int maxlen, utime_t tim)
+{
+ time_t ttime = (time_t)tim;
+ struct tm tm;
+ localtime_r(&ttime, &tm);
+ strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
+ return dt;
+}
+
+/* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
+utime_t str_to_utime(char *str)
+{
+ struct tm tm;
+ time_t ttime;
+
+ if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+ &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+ return 0;
+ }
+ if (tm.tm_mon > 0) {
+ tm.tm_mon--;
+ } else {
+ return 0;
+ }
+ if (tm.tm_year >= 1900) {
+ tm.tm_year -= 1900;
+ } else {
+ return 0;
+ }
+ tm.tm_wday = tm.tm_yday = 0;
+ tm.tm_isdst = -1;
+ ttime = mktime(&tm);
+ if (ttime == -1) {
+ ttime = 0;
+ }
+ return (utime_t)ttime;
+}
+
+
+/*
+ * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
+ * the number of microseconds since Epoch Time (1 Jan 1970) UTC.
+ */
+
+btime_t get_current_btime()
+{
+ struct timeval tv;
+ if (gettimeofday(&tv, NULL) != 0) {
+ tv.tv_sec = (long)time(NULL); /* fall back to old method */
+ tv.tv_usec = 0;
+ }
+ return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
+}
+
+/* Convert btime to Unix time */
+time_t btime_to_unix(btime_t bt)
+{
+ return (time_t)(bt/1000000);
+}
+
+/* Convert btime to utime */
+utime_t btime_to_utime(btime_t bt)
+{
+ return (utime_t)(bt/1000000);
+}
+
+/*
+ * Return the week of the month, base 0 (wom)
+ * given tm_mday and tm_wday. Value returned
+ * can be from 0 to 4 => week1, ... week5
+ */
+int tm_wom(int mday, int wday)
+{
+ int fs; /* first sunday */
+ fs = (mday%7) - wday;
+ if (fs <= 0) {
+ fs += 7;
+ }
+ if (mday <= fs) {
+// Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
+ return 0;
+ }
+ int wom = 1 + (mday - fs - 1) / 7;
+// Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
+ return wom;
+}
+
+/*
+ * Given a Unix date return the week of the year.
+ * The returned value can be 0-53. Officially
+ * the weeks are numbered from 1 to 53 where week1
+ * is the week in which the first Thursday of the
+ * year occurs (alternatively, the week which contains
+ * the 4th of January). We return 0, if the week of the
+ * year does not fall in the current year.
+ */
+int tm_woy(time_t stime)
+{
+ int woy, fty, tm_yday;
+ time_t time4;
+ struct tm tm;
+ memset(&tm, 0, sizeof(struct tm));
+ localtime_r(&stime, &tm);
+ tm_yday = tm.tm_yday;
+ tm.tm_mon = 0;
+ tm.tm_mday = 4;
+ time4 = mktime(&tm);
+ localtime_r(&time4, &tm);
+ fty = 1 - tm.tm_wday;
+ if (fty <= 0) {
+ fty += 7;
+ }
+ woy = tm_yday - fty + 4;
+ if (woy < 0) {
+ return 0;
+ }
+ return 1 + woy / 7;