Skip to content

Commit 227b005

Browse files
committed
v1.0.2
1 parent 3a5eede commit 227b005

File tree

6 files changed

+108
-14
lines changed

6 files changed

+108
-14
lines changed

YANLib/YANDateTime.Nullable.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using static System.DateTime;
2+
using static System.Math;
3+
4+
namespace YANLib;
5+
6+
public static partial class YANDateTime
7+
{
8+
/// <summary>
9+
///
10+
/// </summary>
11+
/// <param name="dt"></param>
12+
/// <param name="tzSrc"></param>
13+
/// <param name="tzDst"></param>
14+
/// <returns></returns>
15+
public static DateTime? ChangeTimeZone(this DateTime? dt, int tzSrc, int tzDst)
16+
{
17+
if (dt.HasValue)
18+
{
19+
var diff = tzDst - tzSrc;
20+
if (diff < 0 && (dt.Value - MinValue).TotalHours >= Abs(diff))
21+
{
22+
dt = dt.Value.AddHours(diff);
23+
}
24+
else if (diff > 0 && (MaxValue - dt.Value).TotalHours >= diff)
25+
{
26+
dt = dt.Value.AddHours(diff);
27+
}
28+
}
29+
return dt;
30+
}
31+
}

YANLib/YANDateTime.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace YANLib;
88

9-
public static class YANDateTime
9+
public static partial class YANDateTime
1010
{
1111
/// <summary>
1212
///
@@ -83,4 +83,25 @@ public static class YANDateTime
8383
/// <param name="dt2"></param>
8484
/// <returns></returns>
8585
public static int TotalMonths(DateTime dt1, DateTime dt2) => Abs((dt1.Year - dt2.Year) * 12 + dt1.Month - dt2.Month);
86+
87+
/// <summary>
88+
///
89+
/// </summary>
90+
/// <param name="dt"></param>
91+
/// <param name="tzSrc"></param>
92+
/// <param name="tzDst"></param>
93+
/// <returns></returns>
94+
public static DateTime ChangeTimeZone(this DateTime dt, int tzSrc, int tzDst)
95+
{
96+
var diff = tzDst - tzSrc;
97+
if (diff < 0 && (dt - MinValue).TotalHours >= Abs(diff))
98+
{
99+
dt.AddHours(diff);
100+
}
101+
else if (diff > 0 && (MaxValue - dt).TotalHours >= diff)
102+
{
103+
dt.AddHours(diff);
104+
}
105+
return dt;
106+
}
86107
}

YANLib/YANLib.csproj

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Title>YANLib</Title>
1010
<Authors>Yami An</Authors>
1111
<Company>YAN</Company>
12-
<Description>Yami An Nephilim library</Description>
12+
<Description>Yami An Nephilim Library</Description>
1313
<Copyright>Copyright © 2023</Copyright>
1414
<PackageIcon>icon.png</PackageIcon>
1515
<PackageProjectUrl>https://github.com/Tynab</PackageProjectUrl>
@@ -19,19 +19,11 @@
1919
<PackageReadmeFile>README.md</PackageReadmeFile>
2020
<RepositoryType>git</RepositoryType>
2121
<PackageReleaseNotes>Fix:
22-
- HasValue (YANText)
23-
- HasCharater (YANText)
24-
Add:
25-
- HasValues (YANText)
26-
- HasCharaters (YANText)
27-
- WaitAnyWithCondition (YANTask)
28-
- ProcessKiller (YANProcess)
29-
- IsValidPasswordStandard (YANPass)
30-
- IsValidPassword (YANPass)</PackageReleaseNotes>
22+
- ChangeTimeZone (YANDateTime)</PackageReleaseNotes>
3123
<PackageLicenseExpression>MIT</PackageLicenseExpression>
3224
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
3325
<PackageId>Tynab.YANLib</PackageId>
34-
<Version>1.0.1</Version>
26+
<Version>1.0.3</Version>
3527
</PropertyGroup>
3628

3729
<ItemGroup>

YANLib/YANList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace YANLib;
44

5-
public static class YANList
5+
public static partial class YANList
66
{
77
/// <summary>
88
/// Check if list has item.

YANLib/YANModel.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections;
2+
3+
namespace YANLib;
4+
5+
public static partial class YANModel
6+
{
7+
/// <summary>
8+
///
9+
/// </summary>
10+
/// <typeparam name="T"></typeparam>
11+
/// <param name="mdl"></param>
12+
/// <param name="tzSrc"></param>
13+
/// <param name="tzDst"></param>
14+
/// <returns></returns>
15+
public static T ChangeTimeZoneAllProperties<T>(this T mdl, int tzSrc, int tzDst)
16+
{
17+
if (mdl != null)
18+
{
19+
var props = mdl.GetType().GetProperties();
20+
foreach (var prop in props)
21+
{
22+
var propType = prop.PropertyType;
23+
if (propType == typeof(DateTime?))
24+
{
25+
prop.SetValue(mdl, ((DateTime?)prop.GetValue(mdl, null)).ChangeTimeZone(tzSrc, tzDst), null);
26+
}
27+
else if (propType == typeof(DateTime))
28+
{
29+
var val = prop.GetValue(mdl, null);
30+
if (val != null)
31+
{
32+
prop.SetValue(mdl, ((DateTime)val).ChangeTimeZone(tzSrc, tzDst), null);
33+
}
34+
}
35+
else if (propType.Namespace != null && !propType.Namespace.StartsWith("System"))
36+
{
37+
prop.GetValue(mdl, null).ChangeTimeZoneAllProperties(tzSrc, tzDst);
38+
}
39+
else if (propType.IsGenericType && prop.GetValue(mdl, null) is IList list)
40+
{
41+
foreach (var item in list)
42+
{
43+
item.ChangeTimeZoneAllProperties(tzSrc, tzDst);
44+
}
45+
}
46+
}
47+
}
48+
return mdl;
49+
}
50+
}

YANLib/YANRandom.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace YANLib;
22

3-
public static class YANRandom
3+
public static partial class YANRandom
44
{
55
/// <summary>
66
/// Returns a non-negative random integer.

0 commit comments

Comments
 (0)