summaryrefslogtreecommitdiffstats
path: root/common/sys/time.c
blob: 5cf20dc1cc965d5eab48ee44964677a189bc1bc9 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <time.h>
#include <stdio.h>
#include "cmsys.h"

static char cdate_buffer[32];

/**
 * 閏年
 */
int is_leap_year(int year)
{
    return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

/**
 * 給日期求星座
 *
 * @return  1..12
 */
int getHoroscope(int m, int d)
{
    if (m > 12 || m < 1)
    return 1;

    // 摩羯 水瓶 雙魚 牡羊 金牛 雙子 巨蟹 獅子 處女 天秤 天蠍 射手
    const int firstday[12] = {
    /* Jan. */ 20, 19, 21, 20, 21, 21, 23, 23, 23, 23, 22, 22
    };
    if (d >= firstday[m - 1]) {
    if (m == 12)
        return 1;
    else
        return m + 1;
    }
    else
    return m;
}

/**
 * 23+1 bytes, "12/31/2007 00:00:00 Mon\0"
 */
const char*
Cdate(const time4_t *clock)
{
    time_t          temp = (time_t)*clock;
    struct tm       mytm;

    localtime_r(&temp, &mytm);
    strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y %T %a", &mytm);
    return cdate_buffer;
}

/**
 * 19+1 bytes, "12/31/2007 00:00:00\0"
 */
const char*
Cdatelite(const time4_t *clock)
{
    time_t          temp = (time_t)*clock;
    struct tm       mytm;

    localtime_r(&temp, &mytm);
    strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y %T", &mytm);
    return cdate_buffer;
}

/**
 * 10+1 bytes, "12/31/2007\0"
 */
const char*
Cdatedate(const time4_t * clock)
{
    time_t          temp = (time_t)*clock;
    struct tm       mytm;

    localtime_r(&temp, &mytm);
    strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y", &mytm);
    return cdate_buffer;
}

/**
 * 5+1 bytes, "12/31\0"
 */
const char*
Cdate_md(const time4_t * clock)
{
    time_t          temp = (time_t)*clock;
    struct tm       mytm;

    localtime_r(&temp, &mytm);
    strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d", &mytm);
    return cdate_buffer;
}

/**
 * 11+1 bytes, "12/31 10:01\0"
 */
const char*
Cdate_mdHM(const time4_t * clock)
{
    time_t          temp = (time_t)*clock;
    struct tm       mytm;

    localtime_r(&temp, &mytm);
    strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d %H:%M", &mytm);
    return cdate_buffer;
}

#ifdef TIMET64
char           *
ctime4(const time4_t *clock)
{
    time_t temp = (time_t)*clock;
    
    return ctime(&temp);
}

char *
ctime4_r(const time4_t *t, char *buf)
{
    time_t temp = (time_t)*t;

    return ctime_r(&temp, buf);
}

// XXX TODO change this to localtime_r style someday.
struct tm *localtime4(const time4_t *t)
{
    if( t == NULL )
    return localtime(NULL);
    else {
    time_t  temp = (time_t)*t;
    return localtime(&temp);
    }
}

struct tm*
localtime4_r(const time4_t *t, struct tm *pt)
{
    if (t)
    {
    time_t temp  = (time_t)*t;
    localtime_r(&temp, pt);
    }
    else
    localtime_r(NULL, pt);
    return pt;
}


time4_t time4(time4_t *ptr)
{
    if( ptr == NULL )
    return time(NULL);
    else
    return *ptr = (time4_t)time(NULL);
}
#endif