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
#
# (C) Copyright 2014 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#
""" A module which supports common Windows types. """
import contextlib
import collections
import time
from datetime import datetime as _datetime
 
 
class error(Exception):
    def __init__(self, *args, **kw):
        nargs = len(args)
        if nargs > 0:
            self.winerror = args[0]
        else:
            self.winerror = None
        if nargs > 1:
            self.funcname = args[1]
        else:
            self.funcname = None
        if nargs > 2:
            self.strerror = args[2]
        else:
            self.strerror = None
        Exception.__init__(self, *args, **kw)
 
 
@contextlib.contextmanager
def pywin32error():
    try:
        yield
    except WindowsError as exception:
        if not hasattr(exception, 'function'):
            exception.function = 'unknown'
        raise error(exception.winerror, exception.function, exception.strerror)
 
 
class datetime(_datetime):
 
    def Format(self, fmt='%c'):
        return self.strftime(fmt)
 
 
def Time(value):
    if isinstance(value, datetime):
        return value
    elif hasattr(value, 'timetuple'):
        timetuple = value.timetuple()
        return datetime.fromtimestamp(time.mktime(timetuple))
    elif isinstance(value, collections.abc.Sequence):
        time_value = time.mktime(value[:9])
        if len(value) == 10:
            time_value += value[9] / 1000.0
        return datetime.fromtimestamp(time_value)
    else:
        try:
            return datetime.fromtimestamp(value)
        except OSError as error:
            if error.errno == 22:
                raise ValueError(error.strerror)
            else:
                raise