diff --git a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp index 73e68aceb..7887bc348 100644 --- a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp +++ b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp @@ -254,18 +254,11 @@ namespace CefSharp DateTime TypeUtils::ConvertCefTimeToDateTime(CefTime time) { - auto epoch = time.GetDoubleT(); - if(epoch == 0) - { - return DateTime::MinValue; - } - return DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime(); + return DateTimeUtils::FromCefTime(time.GetDoubleT()); } CefTime TypeUtils::ConvertDateTimeToCefTime(DateTime dateTime) { - auto timeSpan = dateTime - DateTime(1970, 1, 1); - - return CefTime(timeSpan.TotalSeconds); + return CefTime(DateTimeUtils::ToCefTime(dateTime)); } } \ No newline at end of file diff --git a/CefSharp.Core/CookieManager.cpp b/CefSharp.Core/CookieManager.cpp index 4fc696c40..e2a148c7d 100644 --- a/CefSharp.Core/CookieManager.cpp +++ b/CefSharp.Core/CookieManager.cpp @@ -11,6 +11,8 @@ #include "Internals\CefDeleteCookiesCallbackAdapter.h" #include "Cef.h" +using namespace CefSharp::Internals; + namespace CefSharp { void CookieManager::ThrowIfDisposed() @@ -47,20 +49,11 @@ namespace CefSharp if (cookie->Expires.HasValue) { auto expires = cookie->Expires.Value; - c.expires.year = expires.Year; - c.expires.month = expires.Month; - c.expires.day_of_month = expires.Day; - c.expires.hour = expires.Hour; - c.expires.minute = expires.Minute; - c.expires.second = expires.Second; - c.expires.millisecond = expires.Millisecond; + c.expires = CefTime(DateTimeUtils::ToCefTime(expires)); } - auto timeSpan = cookie->Creation- DateTime(1970, 1, 1); - c.creation = CefTime(timeSpan.TotalSeconds); - - timeSpan = cookie->LastAccess - DateTime(1970, 1, 1); - c.last_access = CefTime(timeSpan.TotalSeconds); + c.creation = CefTime(DateTimeUtils::ToCefTime(cookie->Creation)); + c.last_access = CefTime(DateTimeUtils::ToCefTime(cookie->LastAccess)); return _cookieManager->SetCookie(StringUtils::ToNative(url), c, wrapper); } diff --git a/CefSharp.Core/Internals/CookieVisitor.cpp b/CefSharp.Core/Internals/CookieVisitor.cpp index 09964dd2b..29011fc63 100644 --- a/CefSharp.Core/Internals/CookieVisitor.cpp +++ b/CefSharp.Core/Internals/CookieVisitor.cpp @@ -21,60 +21,13 @@ namespace CefSharp cookie->Secure = cefCookie.secure == 1; cookie->HttpOnly = cefCookie.httponly == 1; - try + if (cefCookie.has_expires) { - if (cefCookie.has_expires) - { - cookie->Expires = DateTime( - cefCookie.expires.year, - cefCookie.expires.month, - cefCookie.expires.day_of_month, - cefCookie.expires.hour, - cefCookie.expires.minute, - cefCookie.expires.second, - cefCookie.expires.millisecond - ); - } - } - catch (Exception^ ex) - { - cookie->Expires = DateTime::MinValue; + cookie->Expires = DateTimeUtils::FromCefTime(CefTime(cefCookie.expires).GetDoubleT()); } - //TODO: There is a method in TypeUtils that's in BrowserSubProcess that convers CefTime, need to make it accessible. - try - { - cookie->Creation = DateTime( - cefCookie.creation.year, - cefCookie.creation.month, - cefCookie.creation.day_of_month, - cefCookie.creation.hour, - cefCookie.creation.minute, - cefCookie.creation.second, - cefCookie.creation.millisecond - ); - } - catch (Exception^ ex) - { - cookie->Creation = DateTime::MinValue; - } - - try - { - cookie->LastAccess = DateTime( - cefCookie.last_access.year, - cefCookie.last_access.month, - cefCookie.last_access.day_of_month, - cefCookie.last_access.hour, - cefCookie.last_access.minute, - cefCookie.last_access.second, - cefCookie.last_access.millisecond - ); - } - catch (Exception^ ex) - { - cookie->LastAccess = DateTime::MinValue; - } + cookie->Creation = DateTimeUtils::FromCefTime(CefTime(cefCookie.creation).GetDoubleT()); + cookie->LastAccess = DateTimeUtils::FromCefTime(CefTime(cefCookie.last_access).GetDoubleT()); } return _visitor->Visit(cookie, count, total, deleteCookie); diff --git a/CefSharp.Core/ResourceHandlerWrapper.cpp b/CefSharp.Core/ResourceHandlerWrapper.cpp index cdb4970d5..51c059264 100644 --- a/CefSharp.Core/ResourceHandlerWrapper.cpp +++ b/CefSharp.Core/ResourceHandlerWrapper.cpp @@ -77,62 +77,15 @@ namespace CefSharp cookie->Secure = cefCookie.secure == 1; cookie->HttpOnly = cefCookie.httponly == 1; - try - { - if (cefCookie.has_expires) - { - cookie->Expires = DateTime( - cefCookie.expires.year, - cefCookie.expires.month, - cefCookie.expires.day_of_month, - cefCookie.expires.hour, - cefCookie.expires.minute, - cefCookie.expires.second, - cefCookie.expires.millisecond - ); - } - } - catch (Exception^ ex) - { - cookie->Expires = DateTime::MinValue; - } - - //TODO: There is a method in TypeUtils that's in BrowserSubProcess that convers CefTime, need to make it accessible. - try - { - cookie->Creation = DateTime( - cefCookie.creation.year, - cefCookie.creation.month, - cefCookie.creation.day_of_month, - cefCookie.creation.hour, - cefCookie.creation.minute, - cefCookie.creation.second, - cefCookie.creation.millisecond - ); - } - catch (Exception^ ex) - { - cookie->Creation = DateTime::MinValue; - } - - try - { - cookie->LastAccess = DateTime( - cefCookie.last_access.year, - cefCookie.last_access.month, - cefCookie.last_access.day_of_month, - cefCookie.last_access.hour, - cefCookie.last_access.minute, - cefCookie.last_access.second, - cefCookie.last_access.millisecond - ); - } - catch (Exception^ ex) - { - cookie->LastAccess = DateTime::MinValue; - } + if (cefCookie.has_expires) + { + cookie->Expires = DateTimeUtils::FromCefTime(CefTime(cefCookie.expires).GetDoubleT()); + } + + cookie->Creation = DateTimeUtils::FromCefTime(CefTime(cefCookie.creation).GetDoubleT()); + cookie->LastAccess = DateTimeUtils::FromCefTime(CefTime(cefCookie.last_access).GetDoubleT()); } return cookie; } -} +} \ No newline at end of file diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index c06fb058c..a7896f873 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -101,6 +101,7 @@ + diff --git a/CefSharp/Internals/DateTimeUtils.cs b/CefSharp/Internals/DateTimeUtils.cs new file mode 100644 index 000000000..2738c2e52 --- /dev/null +++ b/CefSharp/Internals/DateTimeUtils.cs @@ -0,0 +1,40 @@ +// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. +using System; +namespace CefSharp.Internals +{ + /// + /// Mapping to/from CefTime + /// + public static class DateTimeUtils + { + private static DateTime FirstOfTheFirstNineteenSeventy = new DateTime(1970, 1, 1, 0, 0, 0); + + /// + /// Takes the epoch and creates a + /// + /// cef datetime represented as difference between 01/01/1970 + /// + public static DateTime FromCefTime(double epoch) + { + if (epoch > 0) + { + return FirstOfTheFirstNineteenSeventy.AddSeconds(epoch).ToLocalTime(); + } + return DateTime.MinValue; + } + + /// + /// Returns epoch (different from 01/01/1970) + /// + /// datetime + /// epoch + public static double ToCefTime(DateTime dateTime) + { + var timeSpan = dateTime - FirstOfTheFirstNineteenSeventy; + + return timeSpan.TotalSeconds; + } + } +}