Kentico CMS 6.0 Developer's Guide

Managing time zones

Managing time zones

Previous topic Next topic Mail us feedback on this topic!  

Managing time zones

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a time zone.

 

private void 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);
}

 

The following example gets and updates a time zone.

 

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;
}

 

The following example gets and bulk updates time zones.

 

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;
}

 

The following example deletes a time zone.

 

private bool DeleteTimezone()
{

  // Get the timezone
  TimeZoneInfo deleteTimezone = TimeZoneInfoProvider.GetTimeZoneInfo("MyNewTimezone");

 
    // Delete the timezone
    TimeZoneInfoProvider.DeleteTimeZoneInfo(deleteTimezone);
 
    return (deleteTimezone != null);
}

 

The following example gets the time according to the current user's time zone settings.

 

private bool ConvertTime()
{

  // Get user
  UserInfo user = UserInfoProvider.GetFullUserInfo(CMSContext.CurrentUser.UserID);

 
    // If user exist
    if (user != null)
    {

      // Get converted time
       System.DateTime convertedTime = TimeZoneHelper.ConvertUserDateTime(System.DateTime.Now, user);

 
        return true;
    }
 
    return false;
}