Cygwin: strptime: add release note
[newlib-cygwin.git] / winsup / cygwin / times.cc
blob71a7210318fa19b3af7180fd35c5501b0788a93c
1 /* times.cc
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #define __timezonefunc__
10 #include "winsup.h"
11 #include <sys/times.h>
12 #include <sys/timeb.h>
13 #include <sys/param.h>
14 #include <utime.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include "cygerrno.h"
18 #include "security.h"
19 #include "path.h"
20 #include "fhandler.h"
21 #include "dtable.h"
22 #include "cygheap.h"
23 #include "pinfo.h"
24 #include "thread.h"
25 #include "cygtls.h"
26 #include "ntdll.h"
27 #include "spinlock.h"
29 static inline void __attribute__ ((always_inline))
30 get_system_time (PLARGE_INTEGER systime)
32 GetSystemTimePreciseAsFileTime ((LPFILETIME) systime);
35 /* Cygwin internal */
36 static uint64_t
37 __to_clock_t (PLARGE_INTEGER src, int flag)
39 uint64_t total = src->QuadPart;
40 /* Convert into clock ticks - the total is in 10ths of a usec. */
41 if (flag)
42 total -= FACTOR;
44 total /= NS100PERSEC / CLOCKS_PER_SEC;
45 return total;
48 /* times: POSIX 4.5.2.1 */
49 extern "C" clock_t
50 times (struct tms *buf)
52 static SYSTEM_TIMEOFDAY_INFORMATION stodi;
53 KERNEL_USER_TIMES kut;
54 LARGE_INTEGER ticks;
55 clock_t tc = (clock_t) -1;
57 __try
59 /* Fetch boot time if we haven't already. */
60 if (!stodi.BootTime.QuadPart)
61 NtQuerySystemInformation (SystemTimeOfDayInformation,
62 &stodi, sizeof stodi, NULL);
64 NtQueryInformationProcess (NtCurrentProcess (), ProcessTimes,
65 &kut, sizeof kut, NULL);
66 get_system_time (&ticks);
68 /* uptime */
69 ticks.QuadPart -= stodi.BootTime.QuadPart;
70 /* ticks is in in 100ns, convert to clock ticks. */
71 tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NS100PERSEC);
73 /* Linux allows a NULL buf and just returns tc in that case, so
74 mimic that */
75 if (buf)
77 buf->tms_stime = __to_clock_t (&kut.KernelTime, 0);
78 buf->tms_utime = __to_clock_t (&kut.UserTime, 0);
79 timeval_to_filetime (&myself->rusage_children.ru_stime, &kut.KernelTime);
80 buf->tms_cstime = __to_clock_t (&kut.KernelTime, 1);
81 timeval_to_filetime (&myself->rusage_children.ru_utime, &kut.UserTime);
82 buf->tms_cutime = __to_clock_t (&kut.UserTime, 1);
85 __except (EFAULT)
87 tc = (clock_t) -1;
89 __endtry
90 syscall_printf ("%D = times(%p)", tc, buf);
91 return tc;
94 EXPORT_ALIAS (times, _times)
96 /* settimeofday: BSD */
97 extern "C" int
98 settimeofday (const struct timeval *tv, const struct timezone *tz)
100 SYSTEMTIME st;
101 struct tm *ptm;
102 int res = -1;
104 __try
106 if (tv->tv_usec < 0 || tv->tv_usec >= USPERSEC)
108 set_errno (EINVAL);
109 return -1;
112 ptm = gmtime (&tv->tv_sec);
113 st.wYear = ptm->tm_year + 1900;
114 st.wMonth = ptm->tm_mon + 1;
115 st.wDayOfWeek = ptm->tm_wday;
116 st.wDay = ptm->tm_mday;
117 st.wHour = ptm->tm_hour;
118 st.wMinute = ptm->tm_min;
119 st.wSecond = ptm->tm_sec;
120 st.wMilliseconds = tv->tv_usec / (USPERSEC / MSPERSEC);
122 res = -!SetSystemTime (&st);
123 if (res)
124 set_errno (EPERM);
126 __except (EFAULT)
128 res = -1;
130 __endtry
131 syscall_printf ("%R = settimeofday(%p, %p)", res, tv, tz);
132 return res;
135 /* stime: SVr4 */
136 extern "C" int
137 stime (const time_t *t)
139 struct timeval tv = { *t, 0 };
140 return settimeofday(&tv, NULL);
143 /* timezone: standards? */
144 extern "C" char *
145 timezone (void)
147 char *b = _my_tls.locals.timezone_buf;
149 tzset ();
150 __small_sprintf (b,"GMT%+d:%02d", (int) (-_timezone / 3600), (int) (abs (_timezone / 60) % 60));
151 return b;
154 /* Cygwin internal */
155 void
156 totimeval (struct timeval *dst, PLARGE_INTEGER src, int sub, int flag)
158 int64_t x = __to_clock_t (src, flag);
160 x *= (int64_t) USPERSEC / CLOCKS_PER_SEC; /* Turn x into usecs */
161 x -= (int64_t) sub * USPERSEC;
163 dst->tv_usec = x % USPERSEC; /* And split */
164 dst->tv_sec = x / USPERSEC;
167 /* FIXME: Make thread safe */
168 extern "C" int
169 gettimeofday (struct timeval *__restrict tv, void *__restrict tzvp)
171 struct timezone *tz = (struct timezone *) tzvp;
172 static bool tzflag;
173 LONGLONG now = get_clock (CLOCK_REALTIME)->usecs ();
175 if (tv)
177 tv->tv_sec = now / USPERSEC;
178 tv->tv_usec = now % USPERSEC;
181 if (tz)
183 if (!tzflag)
185 tzset ();
186 tzflag = true;
188 tz->tz_minuteswest = _timezone / 60;
189 tz->tz_dsttime = _daylight;
192 return 0;
195 EXPORT_ALIAS (gettimeofday, _gettimeofday)
197 /* Cygwin internal */
198 void
199 timespec_to_filetime (const struct timespec *time_in, PLARGE_INTEGER out)
201 if (time_in->tv_nsec == UTIME_OMIT)
202 out->QuadPart = 0;
203 else
204 out->QuadPart = time_in->tv_sec * NS100PERSEC
205 + time_in->tv_nsec / (NSPERSEC/NS100PERSEC) + FACTOR;
208 /* Cygwin internal */
209 void
210 timeval_to_filetime (const struct timeval *time_in, PLARGE_INTEGER out)
212 out->QuadPart = time_in->tv_sec * NS100PERSEC
213 + time_in->tv_usec * (NS100PERSEC/USPERSEC) + FACTOR;
216 /* Cygwin internal */
217 bool
218 timeval_to_ms (const struct timeval *time_in, DWORD &ms)
220 if (time_in->tv_sec < 0 || time_in->tv_usec < 0
221 || time_in->tv_usec >= USPERSEC)
222 return false;
223 if ((time_in->tv_sec == 0 && time_in->tv_usec == 0)
224 || time_in->tv_sec >= (time_t) (INFINITE / MSPERSEC))
225 ms = INFINITE;
226 else
227 ms = time_in->tv_sec * MSPERSEC
228 + (time_in->tv_usec + (USPERSEC / MSPERSEC) - 1)
229 / (USPERSEC / MSPERSEC);
230 return true;
233 /* Cygwin internal */
234 static timeval
235 time_t_to_timeval (time_t in)
237 timeval res;
238 res.tv_sec = in;
239 res.tv_usec = 0;
240 return res;
243 /* Cygwin internal */
244 static const struct timespec *
245 timeval_to_timespec (const struct timeval *tvp, struct timespec *tmp)
247 if (!tvp)
248 return NULL;
250 tmp[0].tv_sec = tvp[0].tv_sec;
251 tmp[0].tv_nsec = tvp[0].tv_usec * (NSPERSEC/USPERSEC);
252 if (tmp[0].tv_nsec < 0)
253 tmp[0].tv_nsec = 0;
254 else if (tmp[0].tv_nsec >= NSPERSEC)
255 tmp[0].tv_nsec = NSPERSEC - 1;
257 tmp[1].tv_sec = tvp[1].tv_sec;
258 tmp[1].tv_nsec = tvp[1].tv_usec * (NSPERSEC/USPERSEC);
259 if (tmp[1].tv_nsec < 0)
260 tmp[1].tv_nsec = 0;
261 else if (tmp[1].tv_nsec >= NSPERSEC)
262 tmp[1].tv_nsec = NSPERSEC - 1;
264 return tmp;
267 /* Cygwin internal */
268 /* Convert a Win32 time to "UNIX" format. */
269 time_t
270 to_time_t (PLARGE_INTEGER ptr)
272 /* A file time is the number of 100ns since jan 1 1601
273 stuffed into two long words.
274 A time_t is the number of seconds since jan 1 1970. */
276 int64_t x = ptr->QuadPart;
278 /* pass "no time" as epoch */
279 if (x == 0)
280 return 0;
282 x -= FACTOR; /* number of 100ns between 1601 and 1970 */
283 x /= NS100PERSEC; /* number of 100ns in a second */
284 return x;
287 /* Cygwin internal */
288 /* Convert a Win32 time to "UNIX" timestruc_t format. */
289 void
290 to_timestruc_t (PLARGE_INTEGER ptr, timestruc_t *out)
292 /* A file time is the number of 100ns since jan 1 1601
293 stuffed into two long words.
294 A timestruc_t is the number of seconds and microseconds since jan 1 1970
295 stuffed into a time_t and a long. */
296 int64_t x = ptr->QuadPart;
298 /* pass "no time" as epoch */
299 if (x == 0)
301 out->tv_sec = 0;
302 out->tv_nsec = 0;
303 return;
306 x -= FACTOR; /* number of 100ns between 1601 and 1970 */
307 out->tv_sec = x / NS100PERSEC;
308 out->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
311 /* Cygwin internal */
312 /* Get the current time as a "UNIX" timestruc_t format. */
313 void
314 time_as_timestruc_t (timestruc_t * out)
316 LARGE_INTEGER systime;
318 get_system_time (&systime);
319 to_timestruc_t (&systime, out);
322 /* time: POSIX 4.5.1.1, C 4.12.2.4 */
323 /* Return number of seconds since 00:00 UTC on jan 1, 1970 */
324 extern "C" time_t
325 time (time_t * ptr)
327 time_t res;
328 LARGE_INTEGER systime;
330 get_system_time (&systime);
331 res = to_time_t (&systime);
332 if (ptr)
333 *ptr = res;
335 syscall_printf ("%d = time(%p)", res, ptr);
337 return res;
341 utimens_worker (path_conv &win32, const struct timespec *tvp)
343 int res = -1;
345 if (win32.error)
346 set_errno (win32.error);
347 else
349 fhandler_base *fh = NULL;
350 bool fromfd = false;
352 cygheap_fdenum cfd (true);
353 while (cfd.next () >= 0)
354 if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE)
355 && RtlEqualUnicodeString (cfd->pc.get_nt_native_path (),
356 win32.get_nt_native_path (),
357 cfd->pc.objcaseinsensitive ()))
359 fh = cfd;
360 fromfd = true;
361 break;
364 if (!fh)
366 if (!(fh = build_fh_pc (win32)))
367 goto error;
369 if (fh->error ())
371 debug_printf ("got %d error from build_fh_pc", fh->error ());
372 set_errno (fh->error ());
376 res = fh->utimens (tvp);
378 if (!fromfd)
379 delete fh;
382 error:
383 syscall_printf ("%R = utimes(%S, %p)", res, win32.get_nt_native_path (), tvp);
384 return res;
387 /* utimes: POSIX/SUSv3 */
388 extern "C" int
389 utimes (const char *path, const struct timeval tvp[2])
391 path_conv win32 (path, PC_POSIX | PC_SYM_FOLLOW, stat_suffixes);
392 struct timespec tmp[2];
393 return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
396 /* BSD */
397 extern "C" int
398 lutimes (const char *path, const struct timeval tvp[2])
400 path_conv win32 (path, PC_POSIX | PC_SYM_NOFOLLOW, stat_suffixes);
401 struct timespec tmp[2];
402 return utimens_worker (win32, timeval_to_timespec (tvp, tmp));
405 /* futimens: POSIX/SUSv4 */
406 extern "C" int
407 futimens (int fd, const struct timespec tvp[2])
409 int res;
411 cygheap_fdget cfd (fd);
412 if (cfd < 0)
413 res = -1;
414 else if (cfd->get_access () & (FILE_WRITE_ATTRIBUTES | GENERIC_WRITE))
415 res = cfd->utimens (tvp);
416 else
417 res = utimens_worker (cfd->pc, tvp);
418 syscall_printf ("%d = futimens(%d, %p)", res, fd, tvp);
419 return res;
422 /* BSD */
423 extern "C" int
424 futimes (int fd, const struct timeval tvp[2])
426 struct timespec tmp[2];
427 return futimens (fd, timeval_to_timespec (tvp, tmp));
430 /* utime: POSIX 5.6.6.1 */
431 extern "C" int
432 utime (const char *path, const struct utimbuf *buf)
434 struct timeval tmp[2];
436 if (buf == 0)
437 return utimes (path, 0);
439 debug_printf ("incoming utime act %lx", buf->actime);
440 tmp[0] = time_t_to_timeval (buf->actime);
441 tmp[1] = time_t_to_timeval (buf->modtime);
443 return utimes (path, tmp);
446 /* ftime: standards? */
447 extern "C" int
448 ftime (struct timeb *tp)
450 struct timeval tv;
451 struct timezone tz;
453 if (gettimeofday (&tv, &tz) < 0)
454 return -1;
456 tp->time = tv.tv_sec;
457 tp->millitm = tv.tv_usec / 1000;
458 tp->timezone = tz.tz_minuteswest;
459 tp->dstflag = tz.tz_dsttime;
461 return 0;
464 extern "C" int
465 clock_gettime (clockid_t clk_id, struct timespec *tp)
467 clk_t *clock = get_clock (clk_id);
469 if (!clock)
471 set_errno (EINVAL);
472 return -1;
474 __try
476 return clock->nsecs (clk_id, tp);
478 __except (EFAULT) {}
479 __endtry
480 return -1;
483 extern "C" int
484 clock_settime (clockid_t clk_id, const struct timespec *tp)
486 struct timeval tv;
488 if (CLOCKID_IS_PROCESS (clk_id) || CLOCKID_IS_THREAD (clk_id))
489 /* According to POSIX, the privileges to set a particular clock
490 * are implementation-defined. On Linux, CPU-time clocks are not
491 * settable; do the same here.
494 set_errno (EPERM);
495 return -1;
498 if (clk_id != CLOCK_REALTIME_COARSE && clk_id != CLOCK_REALTIME)
500 set_errno (EINVAL);
501 return -1;
504 __try
506 tv.tv_sec = tp->tv_sec;
507 tv.tv_usec = tp->tv_nsec / 1000;
509 __except (EFAULT)
511 return -1;
513 __endtry
515 return settimeofday (&tv, NULL);
518 extern "C" int
519 clock_getres (clockid_t clk_id, struct timespec *tp)
521 clk_t *clock = get_clock (clk_id);
523 if (!clock)
525 set_errno (EINVAL);
526 return -1;
528 __try
530 clock->resolution (tp);
532 __except (EFAULT)
534 return -1;
536 __endtry
537 return 0;
540 extern "C" int
541 clock_setres (clockid_t clk_id, struct timespec *tp)
543 /* Don't use this function. It only exists in QNX. Just return
544 success on CLOCK_REALTIME for backward compat. */
545 if (clk_id != CLOCK_REALTIME)
547 set_errno (EINVAL);
548 return -1;
550 return 0;
553 extern "C" int
554 clock_getcpuclockid (pid_t pid, clockid_t *clk_id)
556 if (pid != 0)
558 pinfo p (pid);
559 if (!p || !p->exists ())
560 return (ESRCH);
562 *clk_id = (clockid_t) PID_TO_CLOCKID (pid);
563 return 0;
566 extern "C" int
567 timespec_get (struct timespec *ts, int base)
569 if (base != TIME_UTC)
570 return 0;
571 clock_gettime (CLOCK_REALTIME, ts);
572 return base;