localtime.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Tilghman Lesher <tlesher AT digium DOT com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. * \brief Custom localtime functions for multiple timezones
  21. */
  22. #ifndef _ASTERISK_LOCALTIME_H
  23. #define _ASTERISK_LOCALTIME_H
  24. #ifdef HAVE_LOCALE_T_IN_LOCALE_H
  25. #include <locale.h>
  26. #elif defined(HAVE_LOCALE_T_IN_XLOCALE_H)
  27. #include <xlocale.h>
  28. #else
  29. typedef void * locale_t;
  30. #endif
  31. struct ast_tm {
  32. int tm_sec; /*!< Seconds. [0-60] (1 leap second) */
  33. int tm_min; /*!< Minutes. [0-59] */
  34. int tm_hour; /*!< Hours. [0-23] */
  35. int tm_mday; /*!< Day. [1-31] */
  36. int tm_mon; /*!< Month. [0-11] */
  37. int tm_year; /*!< Year - 1900. */
  38. int tm_wday; /*!< Day of week. [0-6] */
  39. int tm_yday; /*!< Days in year.[0-365] */
  40. int tm_isdst; /*!< DST. [-1/0/1]*/
  41. long int tm_gmtoff; /*!< Seconds east of UTC. */
  42. char *tm_zone; /*!< Timezone abbreviation. */
  43. /* NOTE: do NOT reorder this final item. The order needs to remain compatible with struct tm */
  44. int tm_usec; /*!< microseconds */
  45. };
  46. /*!\brief Timezone-independent version of localtime_r(3).
  47. * \param timep Current time, including microseconds
  48. * \param p_tm Pointer to memory where the broken-out time will be stored
  49. * \param zone Text string of a standard system zoneinfo file. If NULL, the system localtime will be used.
  50. * \retval p_tm is returned for convenience
  51. */
  52. struct ast_tm *ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone);
  53. void ast_get_dst_info(const time_t * const timep, int *dst_enabled, time_t *dst_start, time_t *dst_end, int *gmt_off, const char * const zone);
  54. /*!\brief Timezone-independent version of mktime(3).
  55. * \param tmp Current broken-out time, including microseconds
  56. * \param zone Text string of a standard system zoneinfo file. If NULL, the system localtime will be used.
  57. * \retval A structure containing both seconds and fractional thereof since January 1st, 1970 UTC
  58. */
  59. struct timeval ast_mktime(struct ast_tm * const tmp, const char *zone);
  60. /*!\brief Set the thread-local representation of the current locale. */
  61. const char *ast_setlocale(const char *locale);
  62. /*!\brief Special version of strftime(3) that handles fractions of a second.
  63. * Takes the same arguments as strftime(3), with the addition of %q, which
  64. * specifies microseconds.
  65. * \param buf Address in memory where the resulting string will be stored.
  66. * \param len Size of the chunk of memory buf.
  67. * \param format A string specifying the format of time to be placed into buf.
  68. * \param tm Pointer to the broken out time to be used for the format.
  69. * \retval An integer value specifying the number of bytes placed into buf or -1 on error.
  70. */
  71. int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm);
  72. int ast_strftime_locale(char *buf, size_t len, const char *format, const struct ast_tm *tm, const char *locale);
  73. /*!\brief Special version of strptime(3) which places the answer in the common
  74. * structure ast_tm. Also, unlike strptime(3), ast_strptime() initializes its
  75. * memory prior to use.
  76. * \param s A string specifying some portion of a date and time.
  77. * \param format The format in which the string, s, is expected.
  78. * \param tm The broken-out time structure into which the parsed data is expected.
  79. * \retval A pointer to the first character within s not used to parse the date and time.
  80. */
  81. char *ast_strptime(const char *s, const char *format, struct ast_tm *tm);
  82. char *ast_strptime_locale(const char *s, const char *format, struct ast_tm *tm, const char *locale);
  83. /*!\brief Wakeup localtime monitor thread
  84. * For use in testing. Normally, the failsafe monitor thread waits 60 seconds
  85. * between checks to verify whether a timezone file has changed. This routine
  86. * forces the monitor thread to wakeup immediately and check the timezone files.
  87. */
  88. struct ast_test;
  89. void ast_localtime_wakeup_monitor(struct ast_test *info);
  90. /*! \brief ast_strftime for ISO8601 formatting timestamps. */
  91. #define AST_ISO8601_FORMAT "%FT%T.%q%z"
  92. /*! \brief Max length of an null terminated, millisecond resolution, ISO8601 timestamp string. */
  93. #define AST_ISO8601_LEN 29
  94. #endif /* _ASTERISK_LOCALTIME_H */