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