Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit edc3b7a

Browse files
committed
Revert "Revert "Aggiunti metodi statici""
This reverts commit 0c1e34c.
1 parent 0c1e34c commit edc3b7a

File tree

7 files changed

+283
-0
lines changed

7 files changed

+283
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace NET6CustomLibrary.DateTime.Converters;
2+
3+
public class DateOnlyConverter : JsonConverter<DateOnly>
4+
{
5+
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
6+
{
7+
var value = reader.GetString();
8+
return DateOnly.Parse(value);
9+
}
10+
11+
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
12+
=> writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace NET6CustomLibrary.DateTime.Converters;
2+
3+
public class TimeOnlyConverter : JsonConverter<TimeOnly>
4+
{
5+
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
6+
{
7+
var value = reader.GetString();
8+
return TimeOnly.Parse(value!);
9+
}
10+
11+
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
12+
=> writer.WriteStringValue(value.ToString("HH:mm:ss.fff"));
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace NET6CustomLibrary.DateTime.Converters;
2+
3+
public class UtcDateTimeConverter : JsonConverter<System.DateTime>
4+
{
5+
private readonly string serializationFormat;
6+
7+
public UtcDateTimeConverter() : this(null)
8+
{
9+
}
10+
11+
public UtcDateTimeConverter(string serializationFormat)
12+
{
13+
this.serializationFormat = serializationFormat ?? "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffff'Z'";
14+
}
15+
16+
public override System.DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
17+
=> reader.GetDateTime().ToUniversalTime();
18+
19+
public override void Write(Utf8JsonWriter writer, System.DateTime value, JsonSerializerOptions options)
20+
=> writer.WriteStringValue((value.Kind == DateTimeKind.Local ? value.ToUniversalTime() : value).ToString(serializationFormat));
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.ComponentModel;
2+
using System.Globalization;
3+
4+
namespace NET6CustomLibrary.DateTime.TypeConverters;
5+
6+
public class DateOnlyTypeConverter : TypeConverter
7+
{
8+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
9+
{
10+
if (sourceType == typeof(string))
11+
{
12+
return true;
13+
}
14+
15+
return base.CanConvertFrom(context, sourceType);
16+
}
17+
18+
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
19+
{
20+
if (destinationType == typeof(string))
21+
{
22+
return true;
23+
}
24+
25+
return base.CanConvertTo(context, destinationType);
26+
}
27+
28+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
29+
{
30+
if (value is string input)
31+
{
32+
return DateOnly.Parse(input);
33+
}
34+
35+
return base.ConvertFrom(context, culture, value);
36+
}
37+
38+
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
39+
{
40+
if (destinationType == typeof(string) && value is DateOnly dateOnly)
41+
{
42+
return dateOnly.ToString();
43+
}
44+
45+
return base.ConvertTo(context, culture, value, destinationType);
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.ComponentModel;
2+
using System.Globalization;
3+
4+
namespace NET6CustomLibrary.DateTime.TypeConverters;
5+
6+
public class TimeOnlyTypeConverter : TypeConverter
7+
{
8+
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
9+
{
10+
if (sourceType == typeof(string))
11+
{
12+
return true;
13+
}
14+
15+
return base.CanConvertFrom(context, sourceType);
16+
}
17+
18+
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
19+
{
20+
if (destinationType == typeof(string))
21+
{
22+
return true;
23+
}
24+
25+
return base.CanConvertTo(context, destinationType);
26+
}
27+
28+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
29+
{
30+
if (value is string input)
31+
{
32+
return TimeOnly.Parse(input);
33+
}
34+
35+
return base.ConvertFrom(context, culture, value);
36+
}
37+
38+
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
39+
{
40+
if (destinationType == typeof(string) && value is TimeOnly timeOnly)
41+
{
42+
return timeOnly.ToString();
43+
}
44+
45+
return base.ConvertTo(context, culture, value, destinationType);
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Date and Time only configuration
2+
3+
4+
## Registering services at Startup
5+
6+
```csharp
7+
public Startup(IConfiguration configuration)
8+
{
9+
Configuration = configuration;
10+
}
11+
12+
public IConfiguration Configuration { get; }
13+
14+
public void ConfigureServices(IServiceCollection services)
15+
{
16+
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
17+
TypeDescriptor.AddAttributes(typeof(TimeOnly), new TypeConverterAttribute(typeof(TimeOnlyTypeConverter)));
18+
19+
services.AddControllers()
20+
.AddDateTimeJsonOptions();
21+
}
22+
```
23+
24+
25+
## Added options in swagger configuration
26+
```csharp
27+
public void ConfigureServices(IServiceCollection services)
28+
{
29+
services.AddSwaggerGen(options =>
30+
{
31+
options.SwaggerDoc("v1", new OpenApiInfo
32+
{
33+
//OMISSIS
34+
});
35+
36+
options.AddDateTimeSwaggerGenOptions();
37+
38+
//OMISSIS
39+
});
40+
}
41+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.OpenApi.Any;
3+
using Microsoft.OpenApi.Models;
4+
using NET6CustomLibrary.DateTime.Converters;
5+
using NET6CustomLibrary.Serilog.Services;
6+
using Serilog;
7+
using Swashbuckle.AspNetCore.SwaggerGen;
8+
9+
namespace NET6CustomLibrary.Extensions;
10+
11+
public static class DependencyInjection
12+
{
13+
#region "SERILOG"
14+
public static IServiceCollection AddSerilogServices(this IServiceCollection services)
15+
{
16+
services.AddTransient<ILoggerService, LoggerService>();
17+
18+
return services;
19+
}
20+
21+
public static WebApplication AddSerilogConfigureServices(this WebApplication application)
22+
{
23+
application.UseSerilogRequestLogging(options =>
24+
{
25+
options.IncludeQueryInRequestPath = true;
26+
});
27+
28+
return application;
29+
}
30+
31+
public static WebApplicationBuilder AddSerilogOptionsBuilder(this WebApplicationBuilder builder)
32+
{
33+
builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>
34+
{
35+
loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
36+
});
37+
38+
return builder;
39+
}
40+
#endregion
41+
42+
#region "MULTI LANGUAGE"
43+
public static IServiceCollection AddSupportedCultures(this IServiceCollection services, string[] cultures)
44+
{
45+
var supportedCultures = cultures;
46+
var localizationOptions = new RequestLocalizationOptions()
47+
.AddSupportedCultures(supportedCultures)
48+
.AddSupportedUICultures(supportedCultures)
49+
.SetDefaultCulture(supportedCultures[0]);
50+
51+
services.Configure<RequestLocalizationOptions>(options =>
52+
{
53+
options.SupportedCultures = localizationOptions.SupportedCultures;
54+
options.SupportedUICultures = localizationOptions.SupportedUICultures;
55+
options.DefaultRequestCulture = localizationOptions.DefaultRequestCulture;
56+
});
57+
58+
return services;
59+
}
60+
61+
public static WebApplication UseLocalizationConfiguration(this WebApplication app)
62+
{
63+
app.UseRequestLocalization();
64+
65+
return app;
66+
}
67+
#endregion
68+
69+
#region "DATE and TIME ONLY"
70+
public static IMvcBuilder AddDateTimeJsonOptions(this IMvcBuilder builder)
71+
{
72+
builder.AddJsonOptions(options =>
73+
{
74+
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
75+
options.JsonSerializerOptions.Converters.Add(new DateOnlyConverter());
76+
options.JsonSerializerOptions.Converters.Add(new TimeOnlyConverter());
77+
options.JsonSerializerOptions.Converters.Add(new UtcDateTimeConverter());
78+
});
79+
80+
return builder;
81+
}
82+
83+
public static SwaggerGenOptions AddDateTimeSwaggerGenOptions(this SwaggerGenOptions options)
84+
{
85+
options.MapType<DateOnly>(() => new OpenApiSchema
86+
{
87+
Type = "string",
88+
Format = "date"
89+
});
90+
91+
options.MapType<TimeOnly>(() => new OpenApiSchema
92+
{
93+
Type = "string",
94+
Format = "time",
95+
Example = new OpenApiString(TimeOnly.FromDateTime(System.DateTime.Now).ToString("HH:mm:ss"))
96+
});
97+
98+
return options;
99+
}
100+
#endregion
101+
}

0 commit comments

Comments
 (0)