Last active
August 21, 2018 04:58
-
-
Save danielplawgo/467b5972f4af597eae13c4b5cd398ed8 to your computer and use it in GitHub Desktop.
Jak automatycznie zmieniać czas lokalny na UTC w ASP.NET MVC?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
setTimezone(); | |
}); | |
function setTimezone() | |
{ | |
var tz = Intl.DateTimeFormat().resolvedOptions().timeZone; | |
$.cookie("timezone", tz); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateService : IDateService | |
{ | |
public DateTime ConvertToUtc(DateTime dateTime) | |
{ | |
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(GetTimezone()); | |
var date = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified); | |
return TimeZoneInfo.ConvertTimeToUtc(date, timeZone); | |
} | |
public DateTime ConvertToLocal(DateTime dateTime) | |
{ | |
return TimeZoneInfo.ConvertTimeBySystemTimeZoneId( | |
dateTime, GetTimezone()); | |
} | |
private string GetTimezone() | |
{ | |
var tzdbId = ""; | |
if (HttpContext.Current != null) | |
{ | |
var cookie = HttpContext.Current.Request.Cookies.Get("timezone"); | |
if (cookie != null && string.IsNullOrEmpty(cookie.Value) == false) | |
{ | |
tzdbId = HttpUtility.UrlDecode(cookie.Value); | |
} | |
} | |
if (string.IsNullOrEmpty(tzdbId) == false) | |
{ | |
var mappings = TzdbDateTimeZoneSource.Default.WindowsMapping.MapZones; | |
var map = mappings.FirstOrDefault(x => | |
x.TzdbIds.Any(z => z.Equals(tzdbId, StringComparison.OrdinalIgnoreCase))); | |
if (map != null) | |
{ | |
return map.WindowsId; | |
} | |
} | |
return "Central Standard Time"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatesProfile : Profile | |
{ | |
public DatesProfile() | |
{ | |
CreateMap<DateTime, DateTime>() | |
.ConvertUsing<DateTimeToDateTimeConverter>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateTimeBinder : DefaultModelBinder | |
{ | |
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) | |
{ | |
var value = base.BindModel(controllerContext, bindingContext); | |
if (bindingContext.ModelType == typeof(DateTime)) | |
{ | |
var date = (DateTime) value; | |
return DateTime.SpecifyKind(date, DateTimeKind.Local); | |
} | |
if (bindingContext.ModelType == typeof(DateTime?)) | |
{ | |
var date = value as DateTime?; | |
if (date.HasValue) | |
{ | |
return DateTime.SpecifyKind(date.Value, DateTimeKind.Local) as DateTime?; | |
} | |
} | |
return value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateTimeToDateTimeConverter : ITypeConverter<DateTime, DateTime> | |
{ | |
private IDateService _dateService; | |
public DateTimeToDateTimeConverter(IDateService dateService) | |
{ | |
_dateService = dateService; | |
} | |
public DateTime Convert(DateTime source, DateTime destination, ResolutionContext context) | |
{ | |
if (source.Kind == DateTimeKind.Unspecified) | |
{ | |
source = DateTime.SpecifyKind(source, DateTimeKind.Utc); | |
} | |
if (source.Kind == DateTimeKind.Local) | |
{ | |
return _dateService.ConvertToUtc(source); | |
} | |
return _dateService.ConvertToLocal(source); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Event | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public DateTime Start { get; set; } | |
public DateTime End { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class EventsController : Controller | |
{ | |
private Lazy<IMapper> _mapper; | |
protected IMapper Mapper | |
{ | |
get { return _mapper.Value; } | |
} | |
private Lazy<DataContext> _dataContext; | |
protected DataContext DataContext | |
{ | |
get { return _dataContext.Value; } | |
} | |
public EventsController(Lazy<IMapper> mapper, | |
Lazy<DataContext> dataContext) | |
{ | |
_mapper = mapper; | |
_dataContext = dataContext; | |
} | |
// GET: Events | |
public virtual ActionResult Index() | |
{ | |
var viewModels = Mapper.Map<List<EventViewModel>>(DataContext.Events); | |
return View(viewModels); | |
} | |
public virtual ActionResult Create() | |
{ | |
var viewModel = new EventViewModel() | |
{ | |
Start = DateTime.Today.AddHours(12), | |
End = DateTime.Today.AddHours(13) | |
}; | |
return View(viewModel); | |
} | |
[HttpPost] | |
public virtual ActionResult Create(EventViewModel viewModel) | |
{ | |
if (ModelState.IsValid == false) | |
{ | |
return View(viewModel); | |
} | |
var eventModel = Mapper.Map<Event>(viewModel); | |
DataContext.Events.Add(eventModel); | |
DataContext.SaveChanges(); | |
return RedirectToAction(MVC.Events.Index()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EventViewModel | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public DateTime Start { get; set; } | |
public DateTime End { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelBindersConfig | |
{ | |
public static void Configure() | |
{ | |
var dateBinder = new DateTimeBinder(); | |
ModelBinders.Binders.Add(typeof(DateTime), dateBinder); | |
ModelBinders.Binders.Add(typeof(DateTime?), dateBinder); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AutofacConfig.Configure(); | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
ModelBindersConfig.Configure(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment