File: localtime_r.c

package info (click to toggle)
gnucash 1%3A2.6.4-3~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 101,252 kB
  • sloc: ansic: 380,872; lisp: 44,440; sh: 12,213; makefile: 6,106; python: 5,741; cpp: 2,014; xml: 1,964; perl: 634
file content (56 lines) | stat: -rw-r--r-- 1,085 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include "config.h"

#if !HAVE_LOCALTIME_R
#include <time.h>
#include <string.h>
#include "localtime_r.h"

#if HAVE_PTHREAD_MUTEX_INIT
#include <pthread.h>

/* New mingw pthread package seems to define localtime_r as a macro */
#ifdef localtime_r
#undef localtime_r
#endif

struct tm *
localtime_r(const time_t *const timep, struct tm *p_tm)
{
    static pthread_mutex_t time_mutex;
    static int time_mutex_inited = 0;
    struct tm *tmp;

    if (!time_mutex_inited)
    {
        time_mutex_inited = 1;
        pthread_mutex_init(&time_mutex, NULL);
    }

    pthread_mutex_lock(&time_mutex);
    tmp = localtime(timep);
    if (tmp)
    {
        memcpy(p_tm, tmp, sizeof(struct tm));
        tmp = p_tm;
    }
    pthread_mutex_unlock(&time_mutex);

    return tmp;
}
#else
struct tm *
localtime_r(const time_t *const timep, struct tm *p_tm)
{
    static struct tm* tmp;
    tmp = localtime(timep);
    if (tmp)
    {
        memcpy(p_tm, tmp, sizeof(struct tm));
        tmp = p_tm;
    }
    return tmp;
}
#endif /* HAVE_PTHREAD_MUTEX_INIT */

#endif /* !HAVE_LOCALTIME_R */