@@ -44,11 +44,7 @@ public DateTime Past(int yearsToGoBack = 1, DateTime? refDate = null)
44
44
45
45
var minDate = maxDate . AddYears ( - yearsToGoBack ) ;
46
46
47
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
48
-
49
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
50
-
51
- return maxDate - partTimeSpan ;
47
+ return Between ( minDate , maxDate ) ;
52
48
}
53
49
54
50
/// <summary>
@@ -62,11 +58,7 @@ public DateTimeOffset PastOffset(int yearsToGoBack = 1, DateTimeOffset? refDate
62
58
63
59
var minDate = maxDate . AddYears ( - yearsToGoBack ) ;
64
60
65
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
66
-
67
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
68
-
69
- return maxDate - partTimeSpan ;
61
+ return BetweenOffset ( minDate , maxDate ) ;
70
62
}
71
63
72
64
/// <summary>
@@ -112,11 +104,7 @@ public DateTime Future(int yearsToGoForward = 1, DateTime? refDate = null)
112
104
113
105
var maxDate = minDate . AddYears ( yearsToGoForward ) ;
114
106
115
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
116
-
117
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
118
-
119
- return minDate + partTimeSpan ;
107
+ return Between ( minDate , maxDate ) ;
120
108
}
121
109
122
110
/// <summary>
@@ -130,11 +118,7 @@ public DateTimeOffset FutureOffset(int yearsToGoForward = 1, DateTimeOffset? ref
130
118
131
119
var maxDate = minDate . AddYears ( yearsToGoForward ) ;
132
120
133
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
134
-
135
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
136
-
137
- return minDate + partTimeSpan ;
121
+ return BetweenOffset ( minDate , maxDate ) ;
138
122
}
139
123
140
124
/// <summary>
@@ -144,14 +128,22 @@ public DateTimeOffset FutureOffset(int yearsToGoForward = 1, DateTimeOffset? ref
144
128
/// <param name="end">End time</param>
145
129
public DateTime Between ( DateTime start , DateTime end )
146
130
{
147
- var minTicks = Math . Min ( start . Ticks , end . Ticks ) ;
148
- var maxTicks = Math . Max ( start . Ticks , end . Ticks ) ;
131
+ var startTicks = start . ToUniversalTime ( ) . Ticks ;
132
+ var endTicks = end . ToUniversalTime ( ) . Ticks ;
133
+
134
+ var minTicks = Math . Min ( startTicks , endTicks ) ;
135
+ var maxTicks = Math . Max ( startTicks , endTicks ) ;
149
136
150
137
var totalTimeSpanTicks = maxTicks - minTicks ;
151
138
152
139
var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
153
140
154
- return new DateTime ( minTicks , start . Kind ) + partTimeSpan ;
141
+ var value = new DateTime ( minTicks , DateTimeKind . Utc ) + partTimeSpan ;
142
+
143
+ if ( start . Kind != DateTimeKind . Utc )
144
+ value = value . ToLocalTime ( ) ;
145
+
146
+ return value ;
155
147
}
156
148
157
149
/// <summary>
@@ -161,14 +153,19 @@ public DateTime Between(DateTime start, DateTime end)
161
153
/// <param name="end">End time</param>
162
154
public DateTimeOffset BetweenOffset ( DateTimeOffset start , DateTimeOffset end )
163
155
{
164
- var minTicks = Math . Min ( start . Ticks , end . Ticks ) ;
165
- var maxTicks = Math . Max ( start . Ticks , end . Ticks ) ;
156
+ var startTicks = start . ToUniversalTime ( ) . Ticks ;
157
+ var endTicks = end . ToUniversalTime ( ) . Ticks ;
158
+
159
+ var minTicks = Math . Min ( startTicks , endTicks ) ;
160
+ var maxTicks = Math . Max ( startTicks , endTicks ) ;
166
161
167
162
var totalTimeSpanTicks = maxTicks - minTicks ;
168
163
169
164
var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
170
165
171
- return new DateTimeOffset ( minTicks , start . Offset ) + partTimeSpan ;
166
+ var dateTime = new DateTime ( minTicks , DateTimeKind . Unspecified ) + partTimeSpan ;
167
+
168
+ return new DateTimeOffset ( dateTime + start . Offset , start . Offset ) ;
172
169
}
173
170
174
171
/// <summary>
@@ -182,11 +179,7 @@ public DateTime Recent(int days = 1, DateTime? refDate = null)
182
179
183
180
var minDate = days == 0 ? SystemClock ( ) . Date : maxDate . AddDays ( - days ) ;
184
181
185
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
186
-
187
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
188
-
189
- return maxDate - partTimeSpan ;
182
+ return Between ( minDate , maxDate ) ;
190
183
}
191
184
192
185
/// <summary>
@@ -200,11 +193,7 @@ public DateTimeOffset RecentOffset(int days = 1, DateTimeOffset? refDate = null)
200
193
201
194
var minDate = days == 0 ? SystemClock ( ) . Date : maxDate . AddDays ( - days ) ;
202
195
203
- var totalTimeSpanTicks = ( maxDate - minDate ) . Ticks ;
204
-
205
- var partTimeSpan = RandomTimeSpanFromTicks ( totalTimeSpanTicks ) ;
206
-
207
- return maxDate - partTimeSpan ;
196
+ return BetweenOffset ( minDate , maxDate ) ;
208
197
}
209
198
210
199
/// <summary>
0 commit comments