GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* 00002 * Copyright (C) 1995. Bill Brown <brown@gis.uiuc.edu> & Michael Shapiro 00003 * 00004 * This program is free software under the GPL (>=v2) 00005 * Read the file GPL.TXT coming with GRASS for details. 00006 */ 00007 #include <stdio.h> 00008 #include <string.h> 00009 #include <grass/datetime.h> 00010 00011 00012 static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 00013 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 00014 }; 00015 00028 int datetime_format(const DateTime * dt, char *buf) 00029 { 00030 /* Format the DateTime structure as a human-readable string */ 00031 /* Returns 0 when successful, and buf is filled with the 00032 formatted data. 00033 Returns a negative number as an error code if the DateTime 00034 structure is not valid. 00035 */ 00036 char temp[128]; 00037 int n; 00038 double sec; 00039 00040 *buf = 0; 00041 if (!datetime_is_valid_type(dt)) 00042 return datetime_error_code(); 00043 00044 if (datetime_is_absolute(dt)) { 00045 if (datetime_get_day(dt, &n) == 0) { 00046 sprintf(temp, "%d", n); 00047 strcat(buf, temp); 00048 } 00049 00050 if (datetime_get_month(dt, &n) == 0) { 00051 if (*buf) 00052 strcat(buf, " "); 00053 strcat(buf, months[n - 1]); 00054 } 00055 00056 if (datetime_get_year(dt, &n) == 0) { 00057 if (*buf) 00058 strcat(buf, " "); 00059 sprintf(temp, "%d", n); 00060 strcat(buf, temp); 00061 if (datetime_is_negative(dt)) 00062 strcat(buf, " bc"); 00063 } 00064 00065 if (datetime_get_hour(dt, &n) == 0) { 00066 if (*buf) 00067 strcat(buf, " "); 00068 sprintf(temp, "%02d", n); 00069 strcat(buf, temp); 00070 } 00071 00072 if (datetime_get_minute(dt, &n) == 0) { 00073 if (*buf) 00074 strcat(buf, ":"); 00075 sprintf(temp, "%02d", n); 00076 strcat(buf, temp); 00077 } 00078 00079 if (datetime_get_second(dt, &sec) == 0) { 00080 if (*buf) 00081 strcat(buf, ":"); 00082 if (datetime_get_fracsec(dt, &n) != 0) 00083 n = 0; 00084 sprintf(temp, "%02.*f", n, sec); 00085 strcat(buf, temp); 00086 } 00087 00088 if (datetime_get_timezone(dt, &n) == 0) { 00089 int hour, minute; 00090 00091 if (*buf) 00092 strcat(buf, " "); 00093 datetime_decompose_timezone(n, &hour, &minute); 00094 sprintf(temp, "%s%02d%02d", n < 0 ? "-" : "+", hour, minute); 00095 strcat(buf, temp); 00096 } 00097 } 00098 00099 if (datetime_is_relative(dt)) { 00100 if (datetime_is_negative(dt)) 00101 strcat(buf, "-"); 00102 00103 if (datetime_get_year(dt, &n) == 0) { 00104 if (*buf) 00105 strcat(buf, " "); 00106 sprintf(temp, "%d year%s", n, n == 1 ? "" : "s"); 00107 strcat(buf, temp); 00108 } 00109 00110 if (datetime_get_month(dt, &n) == 0) { 00111 if (*buf) 00112 strcat(buf, " "); 00113 sprintf(temp, "%d month%s", n, n == 1 ? "" : "s"); 00114 strcat(buf, temp); 00115 } 00116 00117 if (datetime_get_day(dt, &n) == 0) { 00118 if (*buf) 00119 strcat(buf, " "); 00120 sprintf(temp, "%d day%s", n, n == 1 ? "" : "s"); 00121 strcat(buf, temp); 00122 } 00123 00124 if (datetime_get_hour(dt, &n) == 0) { 00125 if (*buf) 00126 strcat(buf, " "); 00127 sprintf(temp, "%d hour%s", n, n == 1 ? "" : "s"); 00128 strcat(buf, temp); 00129 } 00130 00131 if (datetime_get_minute(dt, &n) == 0) { 00132 if (*buf) 00133 strcat(buf, " "); 00134 sprintf(temp, "%d minute%s", n, n == 1 ? "" : "s"); 00135 strcat(buf, temp); 00136 } 00137 00138 if (datetime_get_second(dt, &sec) == 0) { 00139 if (*buf) 00140 strcat(buf, " "); 00141 if (datetime_get_fracsec(dt, &n) != 0) 00142 n = 0; 00143 sprintf(temp, "%.*f second%s", n, sec, 00144 (sec == 1.0 && n == 0) ? "" : "s"); 00145 strcat(buf, temp); 00146 } 00147 } 00148 00149 return 0; 00150 }