summaryrefslogtreecommitdiff
path: root/src/python/LibicalWrap_icaltime.i
blob: 46b0cd91ca99df02565d2eb118285d065622ab71 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

/*======================================================================
  FILE: LibicalWrap_icaltime.i

  (C) COPYRIGHT 2010 Glenn Washburn

  The contents of this file are subject to the Mozilla Public License
  Version 1.0 (the "License"); you may not use this file except in
  compliance with the License. You may obtain a copy of the License at
  https://www.mozilla.org/MPL/

  Software distributed under the License is distributed on an "AS IS"
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  the License for the specific language governing rights and
  limitations under the License.

  The original author is Glenn Washburn (crass@berlios.de)

  Contributions from:

  ======================================================================*/

// Add some methods to the icaltimetype struct
%extend icaltimetype {

    /* ***** Special methods ***** */

    int __cmp__(const icaltimetype b) { return icaltime_compare(*($self), b); }

    /* ***** Conversion methods ***** */

    const char* as_ical_string() { return icaltime_as_ical_string(*($self)); }
    time_t as_timet(const icaltimezone *zone=NULL) {
        return icaltime_as_timet_with_zone(*($self), zone);
    }

    /* ***** Accessor methods ***** */

    const char *get_tzid() { return icaltime_get_tzid(*($self)); }
    int day_of_year() { return icaltime_day_of_year(*($self)); }
    int day_of_week() { return icaltime_day_of_week(*($self)); }

    /** Returns the day of the year for the first day of the week that the
       given time is within. */
    int start_doy_week(int fdow) {
        return icaltime_start_doy_week(*($self), fdow);
    }

    /** Returns the week number for the week the given time is within */
    int week_number() { return icaltime_week_number(*($self)); }


    /* ***** Query methods ***** */

    int is_null_time() { return icaltime_is_null_time(*($self)); }

    /** Returns false if the time is clearly invalid, but is not null. This
       is usually the result of creating a new time type buy not clearing
       it, or setting one of the flags to an illegal value. */
    int is_valid_time() { return icaltime_is_valid_time(*($self)); }

    /* is_date and is_utc are both over shadowed by the struct accessors,
       but they do the same thing. */
    int is_date() { return icaltime_is_date(*($self)); }
    int is_utc() { return icaltime_is_utc(*($self)); }

    /* ***** Modify, compare and utility methods ***** */

    /** Returns -1, 0, or 1 to indicate that a<b, a==b or a>b */
    int compare(const icaltimetype b) { return icaltime_compare(*($self), b); }

    /** like icaltime_compare, but only use the date parts. */
    int compare_date_only(const icaltimetype b, icaltimezone *tz=NULL) {
        if (tz == NULL)
            tz = icaltimezone_get_utc_timezone();
        return icaltime_compare_date_only_tz(*($self), b, tz);
    }

    /** Adds or subtracts a number of days, hours, minutes and seconds. */
    void  adjust(const int days, const int hours, const int minutes, const int seconds) {
        return icaltime_adjust($self, days, hours, minutes, seconds);
    }

    /** Normalizes the icaltime, so that all fields are within the normal range. */
    icaltimetype normalize() { return icaltime_normalize(*($self)); }

    icaltimetype convert_to_zone(icaltimezone *zone) {
        return icaltime_convert_to_zone(*($self), zone);
    }

    /* ***** Static methods ***** */

    static icaltimetype from_timet(const time_t tm,
            const int is_date=0, const icaltimezone *zone=NULL) {
        return icaltime_from_timet_with_zone(tm, is_date, zone);
    }

    static icaltimetype null_time(void) { return icaltime_null_time(); }
    static icaltimetype null_date(void) { return icaltime_null_date(); }

    static icaltimetype current_time(const icaltimezone *zone=NULL) {
        return icaltime_current_time_with_zone(zone);
    }

    static icaltimetype today(void) { return icaltime_today(); }

#if 0
    static icaltimetype from_string(const char* str, const icaltimezone *zone=NULL) {
        /* return _with_zone(str, zone); */
        (void)zone;
        return icaltime_from_string(str);
    }
#else
    /* For the time being do not allow specifying a timezone because this
       is unimplemented as of yet. */
    static icaltimetype from_string(const char* str) {
        return icaltime_from_string(str);
    }
#endif

    /** Returns the number of days in the given month */
    static int days_in_month(const int month, const int year) {
        return icaltime_days_in_month(month, year);
    }

    /** Returns whether you've specified a leap year or not. */
    static int is_leap_year (const int year) {
        return icaltime_is_leap_year(year);
    }

    /** Returns the number of days in this year */
    /* static int days_in_year (const int year) { return icaltime_days_in_year(year); } */

}

// This is a hackish way to support adding the __str__ method to
// a class in python.  Its much easier than writing in C (that
// I've figured out).
%pythoncode %{

def __icaltimetype_str__(self):
    return "<icaltimetype (%d, %d, %d, %d, %d, %d, %d, %d)>" % (
        self.year, self.month, self.day, self.hour, self.minute,
        self.second, self.is_date, self.is_daylight)
icaltimetype.__str__ = __icaltimetype_str__

import datetime
def icaltimetype_as_datetime(self):
    "as_datetime() -> returns datetime object"
    return datetime.datetime(self.year, self.month, self.day, self.hour,
        self.minute, self.second, 0, self.timezone)
icaltimetype.as_datetime = icaltimetype_as_datetime

def icaltimetype_from_datetime(dt):
    "from_datetime() -> returns icaltimetype object"
    tt = icaltimetype()

    tt.year = dt.year
    tt.month = dt.month
    tt.day = dt.day
    tt.hour = dt.hour
    tt.minute = dt.minute
    tt.second = dt.second
    if dt.tzinfo:
        # TODO: convert to the right timezone, assume for now we are UTC
        tt.zone = 0
    tt.is_date = False
    tt.isdaylight = False

    return tt
icaltimetype.from_datetime = staticmethod(icaltimetype_from_datetime)

# Remove accessors to private structure members
icaltimetype_delprops = ["is_date", "is_utc", "zone"]

_swig_remove_private_properties(icaltimetype, icaltimetype_delprops)


# Set/Overwrite icaltimetype properties
icaltimetype_props = {
    "zone": (_LibicalWrap.icaltime_get_timezone, _LibicalWrap.icaltime_set_timezone, ),
    "is_null_time": (_LibicalWrap.icaltime_is_null_time, ),
    "is_valid_time": (_LibicalWrap.icaltime_is_valid_time, ),
    # These do essentially the same thing as the default swig generated
    # accessors is_date and is_utc, but by not defining the setter, we
    # make them immutable from python
    "is_date": (_LibicalWrap.icaltime_is_date, ),
    "is_utc": (_LibicalWrap.icaltime_is_utc, ),
}

_swig_set_properties(icaltimetype, icaltimetype_props)

%}

// TODO: Add icaltime_span_* to icaltime_spantype