Hide menu Last updated: Oct 27 2016
Area: Episerver CMS Applies to versions: 10 and higher
Other versions:

Storing UTC date and time in the database

By default, Episerver CMS uses Coordinated Universal Time (UTC) for storing date and time in the database. One of the advantages of this is that it is possible to change the web server’s time zone or move the database between time zones. If you are running an Episerver CMS site with a version prior to CMS 10, you can convert the database to UTC using the provided PowerShell scripts in this topic.

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 so it is recommended not to make changes to content at this time. 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, so if possible run the migration whilst keeping the site offline or during low-traffic periods.

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.

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).

Comments