Loading...
Searching...
No Matches
datetime/format.c
Go to the documentation of this file.
/*
* 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) {
snprintf(temp, sizeof(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, " ");
snprintf(temp, sizeof(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, " ");
snprintf(temp, sizeof(temp), "%02d", n);
strcat(buf, temp);
}
if (datetime_get_minute(dt, &n) == 0) {
if (*buf)
strcat(buf, ":");
snprintf(temp, sizeof(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;
snprintf(temp, sizeof(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);
snprintf(temp, sizeof(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, " ");
snprintf(temp, sizeof(temp), "%d year%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_month(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
snprintf(temp, sizeof(temp), "%d month%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_day(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
snprintf(temp, sizeof(temp), "%d day%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_hour(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
snprintf(temp, sizeof(temp), "%d hour%s", n, n == 1 ? "" : "s");
strcat(buf, temp);
}
if (datetime_get_minute(dt, &n) == 0) {
if (*buf)
strcat(buf, " ");
snprintf(temp, sizeof(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;
snprintf(temp, sizeof(temp), "%.*f second%s", n, sec,
(sec == 1.0 && n == 0) ? "" : "s");
strcat(buf, temp);
}
}
return 0;
}
datetime_error_code
int datetime_error_code(void)
returns an error code
Definition datetime/error.c:44
datetime_format
int datetime_format(const DateTime *dt, char *buf)
formats DateTime structure as a human-readable string returns 0 when successful and 'buf' is filled w...
Definition datetime/format.c:25
datetime_is_negative
int datetime_is_negative(const DateTime *dt)
Returns: 1 if the DateTime is negative 0 otherwise.
Definition sign.c:34
datetime_is_valid_type
int datetime_is_valid_type(const DateTime *dt)
Returns: 1 if datetime_check_type() returns 0 0 if not.
Definition type.c:76
datetime_is_absolute
int datetime_is_absolute(const DateTime *dt)
Returns: 1 if dt.mode is absolute 0 if not (even if dt.mode is not defined).
Definition type.c:165
datetime_is_relative
int datetime_is_relative(const DateTime *dt)
Returns: 1 if dt.mode is relative 0 if not (even if dt.mode is not defined).
Definition type.c:180
datetime_get_timezone
int datetime_get_timezone(const DateTime *dt, int *minutes)
returns 0 on success
Definition tz1.c:44
datetime_decompose_timezone
void datetime_decompose_timezone(int tz, int *hours, int *minutes)
tz = abs(tz) *hour = tz/60 *minute = tz%60 Note: hour,minute are non-negative. Must look at sign of t...
Definition tz2.c:79
datetime_get_second
int datetime_get_second(const DateTime *dt, double *second)
returns 0 on success or negative value on error
Definition values.c:427
datetime_get_fracsec
int datetime_get_fracsec(const DateTime *dt, int *fracsec)
returns 0 on success or negative value on error
Definition values.c:467
datetime_get_hour
int datetime_get_hour(const DateTime *dt, int *hour)
returns 0 on success or negative value on error
Definition values.c:347
datetime_get_year
int datetime_get_year(const DateTime *dt, int *year)
returns 0 on success or negative value on error
Definition values.c:210
datetime_get_minute
int datetime_get_minute(const DateTime *dt, int *minute)
returns 0 on success or negative value on error
Definition values.c:387
datetime_get_day
int datetime_get_day(const DateTime *dt, int *day)
returns 0 on success or negative value on error
Definition values.c:300
datetime_get_month
int datetime_get_month(const DateTime *dt, int *month)
returns 0 on success or negative value on error
Definition values.c:255