Storing UTC date and time in the database
[New in CMS.Core 9.1]
By default, Episerver CMS uses the web server's local date and time for storing date and time in the database. It is possible to store Coordinated Universal Time (UTC) in the database instead by following the steps below. Storing date and time as UTC makes it possible to change web server's time zone or move the database between time zones. This change will be required in the next major version.
After switching to UTC in the database, all API calls that return local time before the switch continue to return local time. The main difference is that they are converted to and from UTC before being stored in and read from the database.
There are two ways to migrate to UTC in the database: using Visual Studio or running the stand-alone Power Shell Script. In both cases, the same code is executed, but the cmdlet in the Package Manager Console is a convenient way to start the migration from Visual Studio.
The migration script converts all datetime columns in the database. This process should take about a minute for a small site on a fast database server, and up to 30-60 minutes on a large site on a slower database server. It is possible to run the script on a running site as long as you restart the site after the migration is complete. During migration, any dates saved will be incorrect. The migration script places locks on tables during migration, so performance of a site that uses a lot of SQL queries will be negatively affected, and the process might slow down both the migration and the site.
Make sure the database is backed up before running the script. If the migration script fails, it can be restarted and will continue the batch where it was stopped but cannot roll back the complete operation. No changes to site configuration are required. The script stores the type of data and time used in the database.
Any custom Dynamic Data Stores (DDS) are not automatically converted, see date and time handling in DDS.
Option 1: Converting with Visual Studio
This option requires that the site is opened in Visual Studio and uses the connection string "EPiServerDB" in web.config. To get help about the cmdlet, run get-help Convert-EPiDatabaseToUtc –full. Open the project and the Package Manager Console to run the cmdlet as in the examples below.
Scenario 1: Development machine is in the same time zone as the site
PM> Convert-EPiDatabaseToUtc
Scenario 2: Development machine is not in the same time zone as the site
PM> Convert-EPiDatabaseToUtc -timeZone:([TimeZoneInfo]::FindSystemTimeZoneById("US Eastern Standard Time"))
Note: Change the time zone to a Windows time zone used in the environment where the content is stored.
Scenario 3: Azure Web Apps + SQL Azure
PM> Convert-EPiDatabaseToUtc –onlySwitchToUtc:$true
Note: Azure Web Apps and SQL Azure already run as UTC by default. But if you have used the WEBSITE_TIME_ZONE app setting in Azure Web Apps, you need to convert as Scenario 2, since the default UTC time is not used.
Scenario 4: Switching to UTC without converting
PM> Convert-EPiDatabaseToUtc –onlySwitchToUtc:$true
Note: If the exact date and time of historic data are unimportant, it is possible to flip the switch and let the CMS treat all existing and future dates and times as UTC.
Option 2: Converting in production without Visual Studio
The ConvertDatabaseToUtc.psm1 Power Shell script file is shipped in the NuGet package EPiServer.Framework but does not require Visual Studio to run. In development, you can find the file in the solution root (for example, packages/EPiServer.Framework.9.0.0/tools/ConvertDatabaseToUtc.psm1). The ConvertEPiDatabaseToUtc function can be run with the appropriate connection string and local time zone.
Start Windows Power Shell console and run:
PM> Import-Module <path to ConvertDatabaseToUtc.psm1>
PM> ConvertEPiDatabaseToUtc –connectionString "The connection string"
The options for this function are identical for the cmdlet, so the same scenarios apply. When running inside Visual Studio, it is a cmdlet (Convert-EPiDatabaseToUtc), but outside VS, it is a function (ConvertEPiDatabaseToUtc).
Do you find this information helpful? Please log in to provide feedback.
Last updated: Oct 12, 2015