format.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  11. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  12. };
  13. /*!
  14. * \brief
  15. *
  16. * formats DateTime structure as a human-readable string
  17. * returns 0 when successful and 'buf' is filled with the string
  18. * returns a negative number on error
  19. *
  20. * \param dt
  21. * \param buf
  22. * \return int
  23. */
  24. int datetime_format(const DateTime * dt, char *buf)
  25. {
  26. /* Format the DateTime structure as a human-readable string */
  27. /* Returns 0 when successful, and buf is filled with the
  28. formatted data.
  29. Returns a negative number as an error code if the DateTime
  30. structure is not valid.
  31. */
  32. char temp[128];
  33. int n;
  34. double sec;
  35. *buf = 0;
  36. if (!datetime_is_valid_type(dt))
  37. return datetime_error_code();
  38. if (datetime_is_absolute(dt)) {
  39. if (datetime_get_day(dt, &n) == 0) {
  40. sprintf(temp, "%d", n);
  41. strcat(buf, temp);
  42. }
  43. if (datetime_get_month(dt, &n) == 0) {
  44. if (*buf)
  45. strcat(buf, " ");
  46. strcat(buf, months[n - 1]);
  47. }
  48. if (datetime_get_year(dt, &n) == 0) {
  49. if (*buf)
  50. strcat(buf, " ");
  51. sprintf(temp, "%d", n);
  52. strcat(buf, temp);
  53. if (datetime_is_negative(dt))
  54. strcat(buf, " bc");
  55. }
  56. if (datetime_get_hour(dt, &n) == 0) {
  57. if (*buf)
  58. strcat(buf, " ");
  59. sprintf(temp, "%02d", n);
  60. strcat(buf, temp);
  61. }
  62. if (datetime_get_minute(dt, &n) == 0) {
  63. if (*buf)
  64. strcat(buf, ":");
  65. sprintf(temp, "%02d", n);
  66. strcat(buf, temp);
  67. }
  68. if (datetime_get_second(dt, &sec) == 0) {
  69. if (*buf)
  70. strcat(buf, ":");
  71. if (datetime_get_fracsec(dt, &n) != 0)
  72. n = 0;
  73. sprintf(temp, "%02.*f", n, sec);
  74. strcat(buf, temp);
  75. }
  76. if (datetime_get_timezone(dt, &n) == 0) {
  77. int hour, minute;
  78. if (*buf)
  79. strcat(buf, " ");
  80. datetime_decompose_timezone(n, &hour, &minute);
  81. sprintf(temp, "%s%02d%02d", n < 0 ? "-" : "+", hour, minute);
  82. strcat(buf, temp);
  83. }
  84. }
  85. if (datetime_is_relative(dt)) {
  86. if (datetime_is_negative(dt))
  87. strcat(buf, "-");
  88. if (datetime_get_year(dt, &n) == 0) {
  89. if (*buf)
  90. strcat(buf, " ");
  91. sprintf(temp, "%d year%s", n, n == 1 ? "" : "s");
  92. strcat(buf, temp);
  93. }
  94. if (datetime_get_month(dt, &n) == 0) {
  95. if (*buf)
  96. strcat(buf, " ");
  97. sprintf(temp, "%d month%s", n, n == 1 ? "" : "s");
  98. strcat(buf, temp);
  99. }
  100. if (datetime_get_day(dt, &n) == 0) {
  101. if (*buf)
  102. strcat(buf, " ");
  103. sprintf(temp, "%d day%s", n, n == 1 ? "" : "s");
  104. strcat(buf, temp);
  105. }
  106. if (datetime_get_hour(dt, &n) == 0) {
  107. if (*buf)
  108. strcat(buf, " ");
  109. sprintf(temp, "%d hour%s", n, n == 1 ? "" : "s");
  110. strcat(buf, temp);
  111. }
  112. if (datetime_get_minute(dt, &n) == 0) {
  113. if (*buf)
  114. strcat(buf, " ");
  115. sprintf(temp, "%d minute%s", n, n == 1 ? "" : "s");
  116. strcat(buf, temp);
  117. }
  118. if (datetime_get_second(dt, &sec) == 0) {
  119. if (*buf)
  120. strcat(buf, " ");
  121. if (datetime_get_fracsec(dt, &n) != 0)
  122. n = 0;
  123. sprintf(temp, "%.*f second%s", n, sec,
  124. (sec == 1.0 && n == 0) ? "" : "s");
  125. strcat(buf, temp);
  126. }
  127. }
  128. return 0;
  129. }