I am not an expert on the internals of C# so this question might actually be downright silly. If so, please correct me. I have some data (in UTC format) taken out from a SQL server.
2011-03-26 11:03:58.000
2011-03-26 11:04:25.000
...
I am parsing this file inside C# and am using the following:
DateTime date = DateTime.Parse(value);
to fetch the value into the DateTime
object. Now, I am subtracting some arbitrary time 6 hours from this time as follows:
date = date.Subtract(new TimeSpan(6, 0, 0));
And finally, I am writing this back into another file as follows:
output.WriteLine(date.ToString("yyyy-MM-dd HH:mm:ss.fff"));
Because I have not done any implicit conversions, the output is also UTC. My question is, is this kind of addition/subtraction of time allowed on the parsed date or do I need to do some UTC to C# specific conversion before being able to manipulate time? I am having difficulties wrapping my head around this. Would someone please clarify this?
EDIT: Attempt to write a concise question The original date is in UTC. I want to add/subtract some time and write it back in UTC. I want to know if I can manipulate the parsed date directly or I need to do some conversions i.e. tell C# explicitly that the date is in UTC format and then manipulate it and then tell it again to write back UTC date.
The gmtime() function in C++ change the time, which is given to UTC(Universal Time Coordinated) time (i.e., the time at the GMT timezone). The gmtime() is defined in ctime header file. Parameters: The function accepts one mandatory parameter current_time : which specifies a pointer to a time_t object.
Current UTC Time - Java8 OffsetDateTime OffsetDateTime is an immutable representation of a date-time with an offset that is mainly used for storing date-time fields into the precision of nanoseconds. d1 = OffsetDateTime. now(ZoneOffset. UTC);
The gmtime() function is a library function which is defined in <time. h> header file, it is used to get the current Coordinated Universal Time (UTC) time. Syntax: tm* gmtime ( const time_t* current_time );
Rule #1 - STORE DATETIMES IN UTC IN YOUR DATABASE, AND BACK END CODE. It is important that there is consistency across all your date-related data. When storing dates in the database, they should always be in UTC.
It doesn't really matter. A date is a date, no matter what time zone it's in. Unless you're converting it to a different time zone, .Net doesn't know or care what time zone it is.
If you want to, you can call DateTime.SpecifyKind(value, DateTimeKind.Utc)
.
You do not need to parse datetimes from SQL Server. The ADO.Net will return a SqlDateTime
object for a datetime
type column in a result. ORM libraries (LinqToSQL, Entity Framework etc) are also perfectly capable of mapping datetime
type columns to DateTime
properties. If you find yourself parsing a string, you're doing it wrong (not to mention all the implications of SET DATEFORMAT
...)
As a note the operation you described can be performed straight on the server, eg. add one hour to an UTC datetime field and save it back as UTC datetime:
UPDATE table SET column = DATEADD(hour, 1, column) WHERE ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With