Add PBD API to hard-link files
[ardour.git] / libs / pbd / localtime_r.cc
1 /*
2  * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
3  * Copyright (C) 2013 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifdef WAF_BUILD
22 #include "libpbd-config.h"
23 #endif
24
25 #ifndef HAVE_LOCALTIME_R
26 #include <time.h>
27 #include <string.h>
28
29 #include "pbd/pthread_utils.h"
30 #include "pbd/localtime_r.h"
31
32 #ifdef localtime_r
33 #undef localtime_r
34 #endif
35
36 struct tm *
37 localtime_r(const time_t *const timep, struct tm *p_tm)
38 {
39         static pthread_mutex_t time_mutex;
40         static int time_mutex_inited = 0;
41         struct tm *tmp;
42
43         if (!time_mutex_inited)
44         {
45                 time_mutex_inited = 1;
46                 pthread_mutex_init(&time_mutex, NULL);
47         }
48
49         pthread_mutex_lock(&time_mutex);
50         tmp = localtime(timep);
51         if (tmp)
52         {
53                 memcpy(p_tm, tmp, sizeof(struct tm));
54                 tmp = p_tm;
55         }
56         pthread_mutex_unlock(&time_mutex);
57
58         return tmp;
59 }
60
61 #endif
62
63 #ifdef __MINGW64__
64 struct tm *
65 __cdecl localtime(const long int *_Time)
66 {
67         if (_Time == NULL)
68         {
69                 return localtime((const time_t *const)NULL); // Unpredictable behavior in case of _Time == NULL;
70         }
71         else
72         {
73                 const time_t tempTime = *_Time;
74                 return localtime(&tempTime);
75         }
76 }
77 #endif