Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:27:25+00:00 2026-05-30T18:27:25+00:00

I wrote two extension methods to convert times from local time to UTC and

  • 0

I wrote two extension methods to convert times from local time to UTC and back. This is what I have:

public static DateTime ConvertUserTimeToUTCTime(this DateTime TheUserTime, string TheTimezoneID)
{
    TheUserTime = new DateTime(TheUserTime.Year, TheUserTime.Month,
       TheUserTime.Day, TheUserTime.Hour, TheUserTime.Minute,
       TheUserTime.Second, DateTimeKind.Local);

    TimeZoneInfo TheTZ = TimeZoneInfo.FindSystemTimeZoneById(TheTimezoneID);
    TimeSpan TheOffset = TheTZ.GetUtcOffset(TheUserTime);

    DateTimeOffset TheUTCTimeOffset = new DateTimeOffset(
        TheUserTime, TheOffset).ToUniversalTime();

    DateTime TheUTCTime = new DateTime(TheUTCTimeOffset.Year,
        TheUTCTimeOffset.Month, TheUTCTimeOffset.Day, TheUTCTimeOffset.Hour,
        TheUTCTimeOffset.Minute, 0, DateTimeKind.Utc);

   return TheUTCTime;
}




public static DateTime ConvertUTCTimeToUserTime(this DateTime TheUTCTime,
    string TheTimezoneID)
{
    TimeZoneInfo TheTZ = TimeZoneInfo.FindSystemTimeZoneById(TheTimezoneID);

    DateTime UTCTime = new DateTime(TheUTCTime.Year, TheUTCTime.Month, 
       TheUTCTime.Day, TheUTCTime.Hour, TheUTCTime.Minute, 0, DateTimeKind.Utc);

    DateTime TheUserTime = TimeZoneInfo.ConvertTime(UTCTime, TheTZ);

    return TheUserTime;
}

I use these two quite frequently in my app and I was wondering if they’re thread safe. Also, would there be any benefit to putting these two method in an abstract class and then having all the classes that involve time operations inherit from this abstract class?

Thanks for your suggestions on this time conversion topic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:27:26+00:00Added an answer on May 30, 2026 at 6:27 pm

    Yes, they’re thread-safe. They’re ugly in terms of variable naming (why “The” before everything, and why the Pascal-casing?), and you should consider using DateTime.SpecifyKind, but they’re not doing anything with shared state… unless TimeZoneInfo has thread safety problems, it should be fine. (TimeZoneInfo actually specifies that instance members aren’t guaranteed to be thread-safe, but it’s also noted to be immutable. I’d expect it to be thread-safe.)

    You definitely shouldn’t put these in an abstract class as a base class – that would be an abuse of inheritance. It doesn’t really represent some abstract notion you want to specialize, does it? Extension methods are reasonable here.

    You should also think very carefully about how you want the code in the first method to behave in the case of ambiguous or invalid conversions: for example, if the clock goes forward at 1am to 2am, then 1:30am is invalid in that time zone on that day. Likewise if it goes back from 2am to 1am, then 1:30am occurs twice. Check the docs for TimeZoneInfo.GetUtcOffset to make sure that the value returned from it is what you want in those situations.

    Finally, consider using Noda Time instead, my alternative .NET date/time API which (I believe) keeps things rather cleaner – and makes your choices around things like date/time conversion very explicit.

    (I wrote a blog post about this very topic just the other day, in terms of the choices I’ve been thinking about for the API. Feedback welcome.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote two methods in class Util: public static final <T> T[] copy1(T[] source)
I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this
I wrote two methods to check there performance public class Test1 { private String
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I wrote two methods with a void type parameter: procedure Method1(const MyVar; size: cardinal);
I wrote two queries to find duplicates in the array var groups = from
I have wrote an application that syncs two folders together. The problem with the
Two days ago I wrote this question: How can I retrieve an object on
When I have to write methods which return two values , I usually go
I have a class that I have to call one or two methods a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.