~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/bindings/python/src/datetime.cpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright Daniel Wallin 2006. Use, modification and distribution is
2
 
// subject to the Boost Software License, Version 1.0. (See accompanying
3
 
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
 
 
5
 
#include <boost/python.hpp>
6
 
#include <boost/date_time/posix_time/posix_time_types.hpp>
7
 
#include "optional.hpp"
8
 
#include <boost/version.hpp>
9
 
 
10
 
using namespace boost::python;
11
 
 
12
 
#if BOOST_VERSION < 103400
13
 
 
14
 
// From Boost 1.34
15
 
object import(str name)
16
 
{
17
 
    // should be 'char const *' but older python versions don't use 'const' yet.
18
 
    char *n = extract<char *>(name);
19
 
    handle<> module(borrowed(PyImport_ImportModule(n)));
20
 
    return object(module);
21
 
}
22
 
 
23
 
#endif
24
 
 
25
 
object datetime_timedelta;
26
 
object datetime_datetime;
27
 
 
28
 
struct time_duration_to_python
29
 
{
30
 
    static PyObject* convert(boost::posix_time::time_duration const& d)
31
 
    {
32
 
        object result = datetime_timedelta(
33
 
            0 // days
34
 
          , 0 // seconds
35
 
          , d.total_microseconds()
36
 
        );
37
 
 
38
 
        return incref(result.ptr());
39
 
    }
40
 
};
41
 
 
42
 
struct ptime_to_python
43
 
{
44
 
    static PyObject* convert(boost::posix_time::ptime const& pt)
45
 
    {
46
 
        boost::gregorian::date date = pt.date();
47
 
        boost::posix_time::time_duration td = pt.time_of_day();
48
 
 
49
 
        object result = datetime_datetime(
50
 
            (int)date.year()
51
 
          , (int)date.month()
52
 
          , (int)date.day()
53
 
          , td.hours()
54
 
          , td.minutes()
55
 
          , td.seconds()
56
 
        );
57
 
 
58
 
        return incref(result.ptr());
59
 
    }
60
 
};
61
 
 
62
 
void bind_datetime()
63
 
{
64
 
    object datetime = import("datetime").attr("__dict__");
65
 
 
66
 
    datetime_timedelta = datetime["timedelta"];
67
 
    datetime_datetime = datetime["datetime"];
68
 
 
69
 
    to_python_converter<
70
 
        boost::posix_time::time_duration
71
 
      , time_duration_to_python
72
 
    >();
73
 
 
74
 
    to_python_converter<
75
 
        boost::posix_time::ptime
76
 
      , ptime_to_python
77
 
    >();
78
 
 
79
 
    optional_to_python<boost::posix_time::ptime>();
80
 
}
81