Store UTC date and time in the database (Legacy)
If you are running an Optimizely Content Management System (CMS) site with a version prior to CMS 10, you can convert the database to UTC using the provided PowerShell scripts in this topic.
Note
This legacy content applies to Optimizely Content Management System (CMS) versions 10 and 11.
CMS uses Coordinated Universal Time (UTC) to store dates and times in the database by default. One of the advantages of this is that you can change the web server’s time zone or move the database between time zones.
After switching to UTC in the database, API calls return local time before the switch continues 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. The same code is executed in both cases, but the cmdlet in the Package Manager Console is a convenient way to start the migration from Visual Studio.
The migration script converts datetime columns in the database. This process should take about a minute on a small site on a fast database server and up to 30-60 minutes on a large site on a slower database server. You can run the script on a running site if you restart the site after the migration is complete. Any dates saved are incorrect during migration, so you should not change content now. The migration script places locks on tables during migration, so the performance of a site that uses a lot of SQL queries is negatively affected and the process might slow down the migration, and the site, if possible, runs the migration while 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 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 – Convert 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 with 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 – The development machine is in the same time zone as the site
PM> Convert-EPiDatabaseToUtc
Scenario 2 – The 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 – Switch 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 – Convert 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 the 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
).
Updated 9 months ago