logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

localtime.3p (6968B)


  1. '\" et
  2. .TH LOCALTIME "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. localtime,
  12. localtime_r
  13. \(em convert a time value to a broken-down local time
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <time.h>
  18. .P
  19. struct tm *localtime(const time_t *\fItimer\fP);
  20. struct tm *localtime_r(const time_t *restrict \fItimer\fP,
  21. struct tm *restrict \fIresult\fP);
  22. .fi
  23. .SH DESCRIPTION
  24. For
  25. \fIlocaltime\fR():
  26. The functionality described on this reference page is aligned with the
  27. ISO\ C standard. Any conflict between the requirements described here and the
  28. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  29. .P
  30. The
  31. \fIlocaltime\fR()
  32. function shall convert the time in seconds since the Epoch pointed
  33. to by
  34. .IR timer
  35. into a broken-down time, expressed as a local time. The function
  36. corrects for the timezone and any seasonal time adjustments.
  37. Local timezone information is used as though
  38. \fIlocaltime\fR()
  39. calls
  40. \fItzset\fR().
  41. .P
  42. The relationship between a time in seconds since the Epoch used as an
  43. argument to
  44. \fIlocaltime\fR()
  45. and the
  46. .BR tm
  47. structure (defined in the
  48. .IR <time.h>
  49. header) is that the result shall be as specified in the expression
  50. given in the definition of seconds since the Epoch (see the Base Definitions volume of POSIX.1\(hy2017,
  51. .IR "Section 4.16" ", " "Seconds Since the Epoch")
  52. corrected for timezone and any seasonal time adjustments, where the
  53. names in the structure and in the expression correspond.
  54. .P
  55. The same relationship shall apply for
  56. \fIlocaltime_r\fR().
  57. .P
  58. The
  59. \fIlocaltime\fR()
  60. function need not be thread-safe.
  61. .P
  62. The
  63. \fIasctime\fR(),
  64. \fIctime\fR(),
  65. \fIgmtime\fR(),
  66. and
  67. \fIlocaltime\fR()
  68. functions shall return values in one of two static objects: a
  69. broken-down time structure and an array of type
  70. .BR char .
  71. Execution of any of the functions may overwrite the information
  72. returned in either of these objects by any of the other functions.
  73. .P
  74. The
  75. \fIlocaltime_r\fR()
  76. function shall convert the time in seconds since the Epoch pointed
  77. to by
  78. .IR timer
  79. into a broken-down time stored in the structure to which
  80. .IR result
  81. points. The
  82. \fIlocaltime_r\fR()
  83. function shall also return a pointer to that same structure.
  84. .P
  85. Unlike
  86. \fIlocaltime\fR(),
  87. the
  88. \fIlocaltime_r\fR()
  89. function is not required to set
  90. .IR tzname .
  91. If
  92. \fIlocaltime_r\fR()
  93. sets
  94. .IR tzname ,
  95. it shall also set
  96. .IR daylight
  97. and
  98. .IR timezone .
  99. If
  100. \fIlocaltime_r\fR()
  101. does not set
  102. .IR tzname ,
  103. it shall not set
  104. .IR daylight
  105. and shall not set
  106. .IR timezone .
  107. .SH "RETURN VALUE"
  108. Upon successful completion, the
  109. \fIlocaltime\fR()
  110. function shall return a pointer to the broken-down time structure.
  111. If an error is detected,
  112. \fIlocaltime\fR()
  113. shall return a null pointer
  114. and set
  115. .IR errno
  116. to indicate the error.
  117. .P
  118. Upon successful completion,
  119. \fIlocaltime_r\fR()
  120. shall return a pointer to the structure pointed to by the argument
  121. .IR result .
  122. If an error is detected,
  123. \fIlocaltime_r\fR()
  124. shall return a null pointer and set
  125. .IR errno
  126. to indicate the error.
  127. .SH ERRORS
  128. The
  129. \fIlocaltime\fR()
  130. and
  131. \fIlocaltime_r\fR()
  132. functions shall fail if:
  133. .TP
  134. .BR EOVERFLOW
  135. The result cannot be represented.
  136. .LP
  137. .IR "The following sections are informative."
  138. .SH EXAMPLES
  139. .SS "Getting the Local Date and Time"
  140. .P
  141. The following example uses the
  142. \fItime\fR()
  143. function to calculate the time elapsed, in seconds, since January 1,
  144. 1970 0:00 UTC (the Epoch),
  145. \fIlocaltime\fR()
  146. to convert that value to a broken-down time, and
  147. \fIasctime\fR()
  148. to convert the broken-down time values into a printable string.
  149. .sp
  150. .RS 4
  151. .nf
  152. #include <stdio.h>
  153. #include <time.h>
  154. .P
  155. int main(void)
  156. {
  157. time_t result;
  158. .P
  159. result = time(NULL);
  160. printf("%s%ju secs since the Epoch\en",
  161. asctime(localtime(&result)),
  162. (uintmax_t)result);
  163. return(0);
  164. }
  165. .fi
  166. .P
  167. .RE
  168. .P
  169. This example writes the current time to
  170. .IR stdout
  171. in a form like this:
  172. .sp
  173. .RS 4
  174. .nf
  175. Wed Jun 26 10:32:15 1996
  176. 835810335 secs since the Epoch
  177. .fi
  178. .P
  179. .RE
  180. .SS "Getting the Modification Time for a File"
  181. .P
  182. The following example prints the last data modification timestamp
  183. in the local timezone for a given file.
  184. .sp
  185. .RS 4
  186. .nf
  187. #include <stdio.h>
  188. #include <time.h>
  189. #include <sys/stat.h>
  190. .P
  191. int
  192. print_file_time(const char *pathname)
  193. {
  194. struct stat statbuf;
  195. struct tm *tm;
  196. char timestr[BUFSIZ];
  197. .P
  198. if(stat(pathname, &statbuf) =\|= -1)
  199. return -1;
  200. if((tm = localtime(&statbuf.st_mtime)) =\|= NULL)
  201. return -1;
  202. if(strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm) =\|= 0)
  203. return -1;
  204. printf("%s: %s.%09ld\en", pathname, timestr, statbuf.st_mtim.tv_nsec);
  205. return 0;
  206. }
  207. .fi
  208. .P
  209. .RE
  210. .SS "Timing an Event"
  211. .P
  212. The following example gets the current time, converts it to a string
  213. using
  214. \fIlocaltime\fR()
  215. and
  216. \fIasctime\fR(),
  217. and prints it to standard output using
  218. \fIfputs\fR().
  219. It then prints the number of minutes to an event being timed.
  220. .sp
  221. .RS 4
  222. .nf
  223. #include <time.h>
  224. #include <stdio.h>
  225. \&...
  226. time_t now;
  227. int minutes_to_event;
  228. \&...
  229. time(&now);
  230. printf("The time is ");
  231. fputs(asctime(localtime(&now)), stdout);
  232. printf("There are still %d minutes to the event.\en",
  233. minutes_to_event);
  234. \&...
  235. .fi
  236. .P
  237. .RE
  238. .SH "APPLICATION USAGE"
  239. The
  240. \fIlocaltime_r\fR()
  241. function is thread-safe and returns values in a user-supplied buffer
  242. instead of possibly using a static data area that may be overwritten by
  243. each call.
  244. .SH RATIONALE
  245. None.
  246. .SH "FUTURE DIRECTIONS"
  247. None.
  248. .SH "SEE ALSO"
  249. .IR "\fIasctime\fR\^(\|)",
  250. .IR "\fIclock\fR\^(\|)",
  251. .IR "\fIctime\fR\^(\|)",
  252. .IR "\fIdifftime\fR\^(\|)",
  253. .IR "\fIgetdate\fR\^(\|)",
  254. .IR "\fIgmtime\fR\^(\|)",
  255. .IR "\fImktime\fR\^(\|)",
  256. .IR "\fIstrftime\fR\^(\|)",
  257. .IR "\fIstrptime\fR\^(\|)",
  258. .IR "\fItime\fR\^(\|)",
  259. .IR "\fItzset\fR\^(\|)",
  260. .IR "\fIutime\fR\^(\|)"
  261. .P
  262. The Base Definitions volume of POSIX.1\(hy2017,
  263. .IR "Section 4.16" ", " "Seconds Since the Epoch",
  264. .IR "\fB<time.h>\fP"
  265. .\"
  266. .SH COPYRIGHT
  267. Portions of this text are reprinted and reproduced in electronic form
  268. from IEEE Std 1003.1-2017, Standard for Information Technology
  269. -- Portable Operating System Interface (POSIX), The Open Group Base
  270. Specifications Issue 7, 2018 Edition,
  271. Copyright (C) 2018 by the Institute of
  272. Electrical and Electronics Engineers, Inc and The Open Group.
  273. In the event of any discrepancy between this version and the original IEEE and
  274. The Open Group Standard, the original IEEE and The Open Group Standard
  275. is the referee document. The original Standard can be obtained online at
  276. http://www.opengroup.org/unix/online.html .
  277. .PP
  278. Any typographical or formatting errors that appear
  279. in this page are most likely
  280. to have been introduced during the conversion of the source files to
  281. man page format. To report such errors, see
  282. https://www.kernel.org/doc/man-pages/reporting_bugs.html .