using System; using System.Data; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.UIControls; using TimeZoneInfo = CMS.SiteProvider.TimeZoneInfo; [Title(Text = "Time zones", ImageUrl = "Objects/CMS_TimeZone/object.png")] public partial class CMSAPIExamples_Code_Development_TimeZones_Default : CMSAPIExamplePage { #region "Initialization" protected void Page_Load(object sender, EventArgs e) { // Timezone apiCreateTimezone.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateTimezone); apiGetAndUpdateTimezone.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateTimezone); apiGetAndBulkUpdateTimezones.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateTimezones); apiDeleteTimezone.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteTimezone); apiConvertTime.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(ConvertTime); } #endregion #region "Mass actions" /// /// Runs all creating and managing examples. /// public override void RunAll() { base.RunAll(); // Timezone apiCreateTimezone.Run(); apiGetAndUpdateTimezone.Run(); apiGetAndBulkUpdateTimezones.Run(); apiConvertTime.Run(); } /// /// Runs all cleanup examples. /// public override void CleanUpAll() { base.CleanUpAll(); // Timezone apiDeleteTimezone.Run(); } #endregion #region "API examples - Timezone" /// /// Creates timezone. Called when the "Create timezone" button is pressed. /// private bool CreateTimezone() { // Create new timezone object TimeZoneInfo newTimezone = new TimeZoneInfo(); // Set the properties newTimezone.TimeZoneDisplayName = "My new timezone"; newTimezone.TimeZoneName = "MyNewTimezone"; newTimezone.TimeZoneGMT = -12; newTimezone.TimeZoneDaylight = true; newTimezone.TimeZoneRuleStartRule = "MAR|SUN|1|LAST|3|0|1"; newTimezone.TimeZoneRuleEndRule = "OCT|SUN|1|LAST|3|0|0"; newTimezone.TimeZoneRuleStartIn = TimeZoneInfoProvider.CreateRuleDateTime(newTimezone.TimeZoneRuleStartRule); newTimezone.TimeZoneRuleEndIn = TimeZoneInfoProvider.CreateRuleDateTime(newTimezone.TimeZoneRuleEndRule); // Save the timezone TimeZoneInfoProvider.SetTimeZoneInfo(newTimezone); return true; } /// /// Gets and updates timezone. Called when the "Get and update timezone" button is pressed. /// Expects the CreateTimezone method to be run first. /// private bool GetAndUpdateTimezone() { // Get the timezone TimeZoneInfo updateTimezone = TimeZoneInfoProvider.GetTimeZoneInfo("MyNewTimezone"); if (updateTimezone != null) { // Update the properties updateTimezone.TimeZoneDisplayName = updateTimezone.TimeZoneDisplayName.ToLower(); // Save the changes TimeZoneInfoProvider.SetTimeZoneInfo(updateTimezone); return true; } return false; } /// /// Gets and bulk updates timezones. Called when the "Get and bulk update timezones" button is pressed. /// Expects the CreateTimezone method to be run first. /// private bool GetAndBulkUpdateTimezones() { // Prepare the parameters string where = "TimeZoneName LIKE N'MyNewTimezone%'"; // Get the data DataSet timezones = TimeZoneInfoProvider.GetTimeZones(where, null); if (!DataHelper.DataSourceIsEmpty(timezones)) { // Loop through the individual items foreach (DataRow timezoneDr in timezones.Tables[0].Rows) { // Create object from DataRow TimeZoneInfo modifyTimezone = new TimeZoneInfo(timezoneDr); // Update the properties modifyTimezone.TimeZoneDisplayName = modifyTimezone.TimeZoneDisplayName.ToUpper(); // Save the changes TimeZoneInfoProvider.SetTimeZoneInfo(modifyTimezone); } return true; } return false; } /// /// Deletes timezone. Called when the "Delete timezone" button is pressed. /// Expects the CreateTimezone method to be run first. /// private bool DeleteTimezone() { // Get the timezone TimeZoneInfo deleteTimezone = TimeZoneInfoProvider.GetTimeZoneInfo("MyNewTimezone"); // Delete the timezone TimeZoneInfoProvider.DeleteTimeZoneInfo(deleteTimezone); return (deleteTimezone != null); } /// /// Converts time by user timezone. Called when the "Convert time" button is pressed. /// private bool ConvertTime() { // Get user UserInfo user = UserInfoProvider.GetFullUserInfo(CMSContext.CurrentUser.UserID); // If user exist if (user != null) { // Get converted time DateTime convertedTime = TimeZoneHelper.ConvertUserDateTime(DateTime.Now, user); return true; } return false; } #endregion }