-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_date.py
66 lines (53 loc) · 1.96 KB
/
utils_date.py
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
# import datetime
# def string_to_date(string):
# (y, m, d) = map(int, string.split('-'))
# return datetime.date(year=y, month=m, day=d)
# def year_difference(start_date, end_date):
# start_ord = start_date.toordinal()
# end_ord = end_date.toordinal()
# return (end_ord - start_ord) / 365.25
# def year_delta(start_date, years):
# days = round(years * 365.25)
# delta = datetime.timedelta(days=days)
# return start_date + delta
# def numerical_date(date, format = '%Y-%m-%d'):
# """Takes a calendar date and a numerical dates in terms of years"""
# """If date is missing precision, still return numerical date"""
# date = date.replace("XX", "01")
# if isinstance(date, basestring):
# try:
# date = datetime.datetime.strptime(date, format).date()
# except:
# date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
# start_date = datetime.date(year=date.year, month=1, day=1)
# start_ord = start_date.toordinal()
# end_ord = date.toordinal()
# return date.year + (end_ord - start_ord) / 365.25
# def calendar_date(numerical_date):
# """Takes a numerical date and returns a calendar date"""
# year_part = int(numerical_date)
# date = datetime.date(year=year_part, month = 1, day = 1)
# days = round((numerical_date % 1) * 365.25)
# delta = datetime.timedelta(days=days)
# return date + delta
# def string_to_numerical_date(string):
# date = string_to_date(string)
# return numerical_date(date)
# def date_to_day(date):
# if isinstance(date, datetime.date):
# return date.toordinal()
# elif isinstance(date,basestring):
# return datetime.datetime.strptime(date, '%Y-%m-%d').toordinal()
# elif isinstance(date, int):
# return date
# else:
# print "unknown date format", date
# return np.nan
# def main():
# date1 = string_to_date('2011-06-12')
# date2 = string_to_date('2013-03-15')
# print year_difference(date1, date2)
# print numerical_date(date1)
# print string_to_numerical_date('2014-02-01')
# if __name__ == "__main__":
# main()