-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimeUtils.php
171 lines (151 loc) · 4.46 KB
/
TimeUtils.php
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
<?php
namespace FinanCalc\Utils\Time {
use Exception;
use FinanCalc\Constants\ErrorMessages;
use FinanCalc\Utils\Config;
use FinanCalc\Utils\MathFuncs;
/**
* Class TimeUtils
* @package FinanCalc\Utils
*/
class TimeUtils
{
/**
* @param $numberOfDays
* @return string
* @throws Exception
*/
public static function getYearsFromDays($numberOfDays)
{
return MathFuncs::div(
$numberOfDays,
self::getCurrentDayCountConvention()['days_in_a_year']
);
}
/**
* @param $numberOfMonths
* @return string
*/
public static function getYearsFromMonths($numberOfMonths)
{
return MathFuncs::div(
$numberOfMonths,
12
);
}
/**
* @param $numberOfYears
* @return string
*/
public static function getYearsFromYears($numberOfYears)
{
return MathFuncs::div(
$numberOfYears,
1
);
}
/**
* @param $numberOfDays
* @return string
* @throws Exception
*/
public static function getMonthsFromDays($numberOfDays)
{
return MathFuncs::div(
$numberOfDays,
self::getCurrentDayCountConvention()['days_in_a_month']
);
}
/**
* @param $numberOfMonths
* @return string
*/
public static function getMonthsFromMonths($numberOfMonths)
{
return MathFuncs::div(
$numberOfMonths,
1
);
}
/**
* @param $numberOfYears
* @return string
*/
public static function getMonthsFromYears($numberOfYears)
{
return MathFuncs::mul(
$numberOfYears,
12
);
}
/**
* @param $numberOfDays
* @return string
*/
public static function getDaysFromDays($numberOfDays)
{
return MathFuncs::div(
$numberOfDays,
1
);
}
/**
* @param $numberOfMonths
* @return string
* @throws Exception
*/
public static function getDaysFromMonths($numberOfMonths)
{
return MathFuncs::mul(
$numberOfMonths,
self::getCurrentDayCountConvention()['days_in_a_month']
);
}
/**
* @param $numberOfYears
* @return string
* @throws Exception
*/
public static function getDaysFromYears($numberOfYears)
{
return MathFuncs::mul(
$numberOfYears,
self::getCurrentDayCountConvention()['days_in_a_year']
);
}
/**
* @return mixed
* @throws Exception
*/
public static function getCurrentDayCountConvention()
{
$dayCountConventionIdentifier = Config::getConfigField('day_count_convention');
$availableDayCountConventions = Config::getConfigField('available_day_count_conventions');
if (is_array($availableDayCountConventions) &&
array_key_exists($dayCountConventionIdentifier, $availableDayCountConventions)
) {
$dayCountConvention = $availableDayCountConventions[$dayCountConventionIdentifier];
if (self::isDayCountConventionValid($dayCountConvention)) {
$dayCountConvention['days_in_a_month'] = MathFuncs::div($dayCountConvention['days_in_a_year'], 12);
return $dayCountConvention;
}
}
throw new Exception(ErrorMessages::getDayCountConventionNotDefinedMessage());
}
/**
* @param $dayCountConvention
* @return bool
*/
public static function isDayCountConventionValid($dayCountConvention)
{
if (is_array($dayCountConvention)) {
$dayCountConventionField = $dayCountConvention['days_in_a_year'];
if (!is_string($dayCountConventionField) || !ctype_digit($dayCountConventionField)) {
return false;
}
return true;
}
return false;
}
}
}