|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (C) 2025 Gonzalo José Carracedo Carballal |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU Lesser General Public License as |
| 7 | + published by the Free Software Foundation, version 3. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, but |
| 10 | + WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this program. If not, see |
| 16 | + <http://www.gnu.org/licenses/> |
| 17 | +
|
| 18 | +*/ |
| 19 | + |
| 20 | +#ifndef _UTIL_UNITS_H |
| 21 | +#define _UTIL_UNITS_H |
| 22 | + |
| 23 | +#include <sigutils/types.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +SUINLINE const char * |
| 27 | +suscan_units_format_frequency(SUFREQ freq, char *buf, size_t size) |
| 28 | +{ |
| 29 | + if (freq < 1e3) |
| 30 | + snprintf(buf, size, "%.0lf Hz", freq); |
| 31 | + else if (freq < 1e6) |
| 32 | + snprintf(buf, size, "%.3lf kHz", freq * 1e-3); |
| 33 | + else if (freq < 1e9) |
| 34 | + snprintf(buf, size, "%.6lf MHz", freq * 1e-6); |
| 35 | + else if (freq < 1e12) |
| 36 | + snprintf(buf, size, "%.9lf GHz", freq * 1e-9); |
| 37 | + else |
| 38 | + snprintf(buf, size, "%.12lf THz", freq * 1e-12); |
| 39 | + |
| 40 | + return buf; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +SUINLINE const char * |
| 45 | +suscan_units_format_time(SUFLOAT delta, char *buf, size_t size) |
| 46 | +{ |
| 47 | + unsigned int hour, min, sec; |
| 48 | + SUFLOAT decim; |
| 49 | + |
| 50 | + if (delta > 1) { |
| 51 | + sec = SU_FLOOR(delta); |
| 52 | + decim = delta - sec; |
| 53 | + |
| 54 | + hour = sec / 3600; |
| 55 | + min = (sec / 60) % 60; |
| 56 | + sec %= 60; |
| 57 | + } |
| 58 | + |
| 59 | + if (delta < 1e-9) |
| 60 | + snprintf(buf, size, "%.3g ps", delta * 1e12); |
| 61 | + else if (delta < 1e-6) |
| 62 | + snprintf(buf, size, "%.3g ns", delta * 1e9); |
| 63 | + else if (delta < 1e-3) |
| 64 | + snprintf(buf, size, "%.3g us", delta * 1e6); |
| 65 | + else if (delta < 1) |
| 66 | + snprintf(buf, size, "%.3g ms", delta * 1e3); |
| 67 | + else if (delta < 60) |
| 68 | + snprintf(buf, size, "%.3g s", delta); |
| 69 | + else if (delta < 3600) |
| 70 | + snprintf(buf, size, "00:%02d:%02d.%.3g", min, sec, decim); |
| 71 | + else |
| 72 | + snprintf(buf, size, "%02d:%02d:%02d", hour, min, sec); |
| 73 | + |
| 74 | + return buf; |
| 75 | +} |
| 76 | + |
| 77 | +#endif /* _UTIL_UNITS_H */ |
0 commit comments