1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro
*
* This program is free software under the GPL (>=v2)
* Read the file GPL.TXT coming with GRASS for details.
*/
#include <stdio.h>
#include <string.h>
#include <grass/datetime.h>
static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
/*!
* \brief
*
* formats DateTime structure as a human-readable string
* returns 0 when successful and 'buf' is filled with the string
* returns a negative number on error
*
* \param dt
* \param buf
* \return int
*/
int datetime_format(const DateTime *dt, char *buf)
{
/* Format the DateTime structure as a human-readable string */
/* Returns 0 when successful, and buf is filled with the
formatted data.
Returns a negative number as an error code if the DateTime
structure is not valid.
*/
char temp[128];
int n;
double sec;
*buf = 0;
if (!datetime_is_valid_type(dt))
return datetime_error_code();
if (datetime_is_absolute(dt)) {
if (datetime_get_day(dt, &n) == 0) {
sprintf(temp, "%d", n);
strcat(buf, temp);
}
if (datetime_get_month(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
strcat(buf, months[n - 1]);
}
if (datetime_get_year(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d", n);
strcat(buf, temp);
if (datetime_is_negative(dt))
strcat(buf, " bc");
}
if (datetime_get_hour(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%02d", n);
strcat(buf, temp);
}
if (datetime_get_minute(dt, &n) == 0) {
if (*buf)
strcat(buf, ":");
sprintf(temp, "%02d", n);
strcat(buf, temp);
}
if (datetime_get_second(dt, &sec) == 0) {
if (*buf)
strcat(buf, ":");
if (datetime_get_fracsec(dt, &n) != 0)
n = 0;
sprintf(temp, "%02.*f", n, sec);
strcat(buf, temp);
}
if (datetime_get_timezone(dt, &n) == 0) {
int hour, minute;
if (*buf)
strcat(buf, " ");
datetime_decompose_timezone(n, &hour, &minute);
sprintf(temp, "%s%02d%02d", n < 0 ? "-" : "+", hour, minute);
strcat(buf, temp);
}
}
if (datetime_is_relative(dt)) {
if (datetime_is_negative(dt))
strcat(buf, "-");
if (datetime_get_year(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d year%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_month(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d month%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_day(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d day%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_hour(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d hour%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_minute(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
sprintf(temp, "%d minute%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_second(dt, &sec) == 0) {
if (*buf)
strcat(buf, " ");
if (datetime_get_fracsec(dt, &n) != 0)
n = 0;
sprintf(temp, "%.*f second%s", n, sec,
(sec == 1.0 && n == 0) ? "" : "s");
strcat(buf, temp);
}
}
return 0;
}
|