locked
Regarding UTC & Normal time c# RRS feed

  • Question

  • User88744855 posted

    my pc time was 1:58 PM then i just ran the below code and saw this time was stored "8:27:37 AM" in x varaible and this time was stored "1:58:14 PM" in y variable.

                var x=DateTime.UtcNow.ToLongTimeString();
                var y = DateTime.Now.ToLongTimeString();

    i just like to know what is UTC time and how it is calculated. please discuss it briefly.

    UTC time is GMT or IST ? thanks

    Monday, December 8, 2014 3:17 AM

Answers

All replies

  • User-434868552 posted

    @mou_inn   you need to learn to use Google, or your preferred search engine(s).

    http://en.wikipedia.org/wiki/Coordinated_Universal_Time


    "Coordinated Universal Time (French: temps universel coordonné, UTC) is the primary time standard by which the world regulates clocks and time.

      It is one of several closely related successors to Greenwich Mean Time (GMT).

      For most purposes, UTC is used interchangeably with GMT, but GMT is no longer precisely defined by the scientific community; ..."

    mou_inn, read the full article.

    edit:

    mou_inn, MSDN is also a source you should read:

    http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx  "DateTime Structure" and

    http://msdn.microsoft.com/en-us/library/system.datetime.utcnow(v=vs.110).aspx "DateTime.UtcNow Property"

    end edit.

    edit2:

    mou_inn, also study this:

    http://msdn.microsoft.com/en-us/library/system.datetimeoffset(v=vs.110).aspx "DateTimeOffset Structure"

    generally, but not always, the difference between your time and UTC is an even number of hours; for example, i'm in the Eastern Time zone:

    Console.WriteLine (DateTime.UtcNow.ToLongTimeString());
    Console.WriteLine (DateTime.Now.ToLongTimeString());
    Console.WriteLine (DateTimeOffset.Now);

    output:

    09:07:35
    04:07:35
    2014-12-08 04:07:35 -05:00

    you may find this map interesting:  http://www.timeanddate.com/time/map/

    http://en.wikipedia.org/wiki/Time_zone

    http://en.wikipedia.org/wiki/UTC%E2%88%9204:30    4.5 hours

    http://www.timetemperature.com/canada/canada_time_zone.shtml  http://en.wikipedia.org/wiki/UTC%E2%88%9203:30 3.5 hours

    end edit2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 8, 2014 3:51 AM
  • User88744855 posted

    sorry this area is not clear. u said

    Console.WriteLine (DateTime.UtcNow.ToLongTimeString());
    Console.WriteLine (DateTime.Now.ToLongTimeString());
    Console.WriteLine (DateTimeOffset.Now);

    output:

    09:07:35
    04:07:35
    2014-12-08 04:07:35 -05:00

    this line DateTime.UtcNow.ToLongTimeString() gives 09:07:35 which is not ur pc time

    this line DateTime.Now.ToLongTimeString() gives 04:07:35 this is ur pc time.

    i just like to know what causes the difference in time like 7 hrs between two time.

    this line DateTimeOffset.Now showing your pc time 04:07:35 but show a extra data like -05:00 what does it means ?

    suppose if i have utc time and this one -05:00 then how can i calculate user pc time means your pc time 04:07:35. please discuss this calculation.

    Tuesday, December 9, 2014 3:01 AM
  • User-434868552 posted

    @mou_inn

    in an ideal situation, there would be ONE time for the entire planet ...

    historically, we missed that boat ... 

    for example, if you lived in Germany a few hundred years ago, before telephone, before internet, Morgen (morning) was more or less when the sun came up and lasted until Mittag (literally middle of the day), ...

    Moving forward in years and western cultures tend to work a 9 to 5 day, however 09:00 in England, because of the way we set our clocks, is still middle of the night 01:00 in Los Angelous, California because Californian time is 8 hours behind UTC.

    mou_inn, if you look at the links that i provided for you earlier in this thread, you'll see how the world is divided up into more or less arbitrary time zones.

    here's the official U.S. time zone map:  http://www.time.gov/

    what time zone are you in?  your forums.asp.net profile shows 00:00:00 GMT -- is that correct?

    as i write this, looking at the U.S. map above, it's 13:50 Atlantic Time, 12:50 Eastern Time, 11:50 Central Time, 10:50 Mountain Time, 09:50 Pacific Time, 08:50 Alaska TIme, 07:50 Hawaii-Aleutian Time, et cetera.

    Names like Eastern Time and its synonyms like New York Time are for the convenience of humans ... computers are happy with an offset like -05:00 which means that currently Eastern Time of 5 hours behind UTC.

    That is why MY PC in my previous reply shows UTC as 09:07:35 but my Eastern time 04:07:35 (when, if i were smarter, i should have been asleep in my bed).

    The U.S. map also shows you approximately where in the world it is night-time and where it is daytime. 

    mou_inn

    suppose if i have utc time and this one -05:00 then how can i calculate user pc time means your pc time 04:07:35. please discuss this calculation.

    mou_inn, to know some person's time, you need to know that person's time zone ... 

    if Bill Gates moves to India, his time would be +5.5 hours; i would have to add UTC + 5.5 to know his time.

    NOON UTC would be 17:30 for Bill Gates in India but only 07:00 for me (UTC - 5).

    Did you read the links i provided earlier?

    http://en.wikipedia.org/wiki/Time_zone

    "A time zone is a region that has a uniform standard time for legal, commercial, and social purposes. It is convenient for areas in close commercial or other communication to keep the same time, so time zones tend to follow the boundaries of countries and their subdivisions."

    Tuesday, December 9, 2014 1:13 PM
  • User88744855 posted
    Sorry to bother u again. Suppose i know user timezone and user utc then tell me what calculation i need to do to get user local time without using datetime class. It sounds like reinvent the wheel but i like to know the calc behind getting ones local time. If u know then please share the idea
    Wednesday, December 10, 2014 11:49 AM
  • User-434868552 posted

    @mou_inn        please LOOK at the link targets ~~ READ the text; STUDY the maps.   This is important for YOU.

    Your UTC wherever you are is the same as my UTC in Canada is the same as someone's UTC in Russia is the same as someone's UTC in New Zealand.

    That's why is called "universal".

    Local time is relative to UTC.

    Click the following links:

    http://lmgtfy.com/?q=what+time+is+it+in+toronto+right+now

    http://lmgtfy.com/?q=what+time+is+it+in+tokyo+right+now

    http://lmgtfy.com/?q=what+time+is+it+in+UTC+right+now

    UTC is the same for BOTH Toronto and Tokyo!

    mou_inn, the offset from UTC varies depending whether a given time zone is under the influence of Daylight Saving TIme.

    http://weblogs.asp.net/gerrylowry/clarity-is-important-both-in-question-and-in-answer

    more information

    The time in your PC may not be the same as your local time.

    Example:  my PC is UTC -5 ... if i fly to Toyko with my laptop, unless i reset my PC settings, the clocks in Tokyo will be on Tokyo time and my PC will be on Toronto time.

    Even the date can be different:  in Toyko it's already December 11 but for me it's still December 10; if i ever get to go to Tokyo, i must be careful to not be a day late for a meeting.

    if you look at an old e-mail message and check the time of a read receipt, you are likely to notice that the time is given as UTC; so whether your e-mail was read in New York or Berlin, it does not matter.

    DAYLIGHT SAVING TIME

    when DST is active for me, my offset is -4; currently, because DST is over until next year, the offset is -5

    mou_inn

    Suppose i know user timezone and user utc

    The user's UTC == your UTC == everyone's UTC.  You need to know the offset from UTC.

    My offset when DST is active is -4; otherwise -5

    http://msdn.microsoft.com/en-us/library/bb460642(v=vs.110).aspx "TimeZoneInfo.IsDaylightSavingTime Method (DateTime)"

    DateTime myNow  = DateTime.Now;
    DateTime allUTC = myNow.ToUniversalTime();
    Console.WriteLine (myNow.IsDaylightSavingTime());
    Console.WriteLine (DateTimeOffset.Now);
    Console.WriteLine (myNow);
    Console.WriteLine (allUTC);
    Console.WriteLine (myNow.ToUniversalTime());
    Console.WriteLine (allUTC.ToLocalTime());

    output:

    False
    2014-12-10 16:07:14 -05:00
    2014-12-10 16:07:14
    2014-12-10 21:07:14
    2014-12-10 21:07:14
    2014-12-10 16:07:14

    mou_inn

    Suppose i know user timezone

    You absolutely must know the user's time zone.

    Wednesday, December 10, 2014 4:10 PM
  • User88744855 posted

    thanks for your patience and answer. i just try to know if i know user timezone and offset from utc time then how could i calculate user local time without using dotnet datetime class. just i like to know the what calculation i need to do to get user local time if i know user's timezone and offset and utc time.

    i use dotnet datetime related classes then it is very easy to get anyone's local time but i need to develop a routine where i will pass time offset & timezone info then routine will calculate what is user's localtime.

    if possible write a sample routine which take timezone and offset and return user pc local time without taking any help from dotnet datetime class. thanks

    Thursday, December 11, 2014 3:09 AM
  • User-434868552 posted

    @mou_inn     if you've read the links i sent, you would realize this is much more complex that you appear to think.

    i'll get to the arithmetic below.

    mou_inn

    i know user's time zone

    how will you get the user's time zone?

    mou_inn

    i know user's  offset

    how will you get the user's offset?

    mou_inn

    i know user's  utc time

    as i've written already, every person's UTC time is the SAME no matter where on earth the person happens to be.

    mou_inn -- run the code here on your computer:

    http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones(v=vs.110).aspx 

    ID: Canada Central Standard Time
    Display Name: (UTC-06:00) Saskatchewan
    Standard Name: Canada Central Standard Time
    Daylight Name: Canada Central Daylight Time ***Does Not Have Daylight Saving Time***
    Offset from UTC: -6 hours, 0 minutes
    Number of adjustment rules: 0

    ID: Central America Standard Time
    Display Name: (UTC-06:00) Central America
    Standard Name: Central America Standard Time
    Daylight Name: Central America Daylight Time ***Does Not Have Daylight Saving Time***
    Offset from UTC: -6 hours, 0 minutes
    Number of adjustment rules: 0

    For Canada Central Standard Time and Central America Standard Time all you need to know is the offset because neither time zone has DST.

    ID: Central Standard Time
    Display Name: (UTC-06:00) Central Time (US & Canada)
    Standard Name: Central Standard Time
    Daylight Name: Central Daylight Time ***Has Daylight Saving Time***
    Offset from UTC: -6 hours, 0 minutes
    Number of adjustment rules: 2
    Adjustment Rules:
    From 0001-01-01 00:00:00 to 2006-12-31 00:00:00
    Delta: 01:00:00
    Begins at 02:00 on Sunday of week 1 of April
    Ends at 02:00 on Sunday of week 5 of October
    From 2007-01-01 00:00:00 to 9999-12-31 00:00:00
    Delta: 01:00:00
    Begins at 02:00 on Sunday of week 2 of March
    Ends at 02:00 on Sunday of week 1 of November

    mou_inn -- Central Standard Time is more complicated ... it has two  adjustment rules ... one rule for before 2007 and a second rule for after 2006.

    ID: Afghanistan Standard Time
    Display Name: (UTC+04:30) Kabul
    Standard Name: Afghanistan Standard Time
    Daylight Name: Afghanistan Daylight Time ***Does Not Have Daylight Saving Time***
    Offset from UTC: 4 hours, 30 minutes
    Number of adjustment rules: 0

    // where offset is known:
    Double offsetForKabul               =  4.5;
    Double offsetForGerryLowry          = -5.0;  // because DST is NOT active in December
    Double offsetForSaskatchewan        = -6.0;
    DateTime utcForEveryone             = DateTime.UtcNow;
    DateTime localTImeForKabul          = utcForEveryone.AddHours(offsetForKabul);
    DateTime localTimeForGerry          = utcForEveryone.AddHours(offsetForGerryLowry);
    DateTime localTimeForSaskatchewan = utcForEveryone.AddHours(offsetForSaskatchewan);
    Console.WriteLine (utcForEveryone);
    Console.WriteLine (localTImeForKabul);
    Console.WriteLine (localTimeForGerry);
    Console.WriteLine (localTimeForSaskatchewan);

    output:

    2014-12-11 09:16:24
    2014-12-11 13:46:24
    2014-12-11 04:16:24
    2014-12-11 03:16:24

    mou_inn -- if you know the time zone and the UTC time, you need to use the corresponding adjustment rules so the you can compute the offset; once you know the offset, you use the same calculation as above.

    http://en.wikipedia.org/wiki/Time_zone#mediaviewer/File:World_Time_Zones_Map.png 

    edit:  why would you not want to use DateTime methods like .AddHours?  end edit.

    Thursday, December 11, 2014 4:26 AM