1#!/usr/bin/env python3
 2
 3"""Time converter functions used by timeconv web page and time cmd tool."""
 4
 5import time
 6from datetime import datetime
 7
 8
 9def to_day_of_year(dt):
10    """Convert datetime to day of year."""
11    return '{0:03}'.format((dt - datetime(dt.year, 1, 1)).days + 1)
12
13
14def to_week(dt):
15    """Convert datetime to ISO week number."""
16    return dt.isocalendar()[1]
17
18
19def to_day_of_week(dt):
20    """Convert datetime to weekday name."""
21    return [None, 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
22            'Friday', 'Saturday', 'Sunday'][dt.isoweekday()]
23
24
25def to_unixtime(dt):
26    """Convert datetime to unix time."""
27    return int(time.mktime(dt.timetuple()))
28
29
30def to_matlab(dt):
31    """Convert datetime to matlab time."""
32    matlab_delta = dt - datetime(2000, 1, 1)
33    return "{0:f}".format(
34        730486 + matlab_delta.days + (matlab_delta.seconds * 1000 +
35                                      float(matlab_delta.microseconds) / 1000) / 86400000)