How can I get the Original Install Date of the Windows using C#?
From this website, using the registry rather than WMI (untested):
public static DateTime GetWindowsInstallationDateTime(string computerName)
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, computerName);
key = key.OpenSubKey(#"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
if (key != null)
{
DateTime installDate =
DateTime.FromFileTimeUtc(
Convert.ToInt64(
key.GetValue("InstallDate").ToString()));
return installDate;
}
return DateTime.MinValue;
}
The HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate is the Windows InstallDate using a Unix timestamp, but technically it's the wrong date.
Let me explain;
The definition of UNIX timestamp is time zone independent. The UNIX timestamp is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 and not counting leap seconds.
In other words, if you have installed you computer in Seattle, WA and moved to New York,NY the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate will give you the date in NY timezone, not in Seattle timezone where Windows was original installed. It's the wrong date, it doesn't store timezone where the computer was initially installed.
Solution
Change you computer time zone (right-click on you clock->Adjust date/time->Adjust time zone) to the time zone where windows was installed, or first turned on. Then run systeminfo.exe find /i "Original Install Date"
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\InstallDate but you have add the time zone where windows was installed, or first turned on.
Related
For example, when I try to do the following.
TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")
I get the error, that the TimeZone is not available on the local computer. When I run this locally it works, but there I run it on windows. When deployed it runs on a Unix machine in Nginx. I can see that FindSystemTimeZoneById is looking in the wrong folder when it comes to Unix. Is there any way to make this work?
.Net Core using system timezone. Unfortunately Windows and Linux have different timezone system. Now you have two ways:
Use other (and universal) impementation of timezone like Noda time
Translate
between Windows and IANA time zones, e.g. using the TimeZoneConverter micro-library.
Working of off the previous answer, we can avoid the expensive try/catch by checking which OS we're running on:
using System;
using System.Runtime.InteropServices;
TimeZoneInfo easternStandardTime;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}
Can you please try this?
TimeZoneInfo easternZone;
try
{
easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
}
catch (TimeZoneNotFoundException)
{
easternZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
}
You can review the list of IANA time zones here https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
If you want to try a Windows time zone and then fallback on a IANA one if the Windows one doesn't exist:
var tzi = TimeZoneInfo.GetSystemTimeZones().Any(x => x.Id == "Eastern Standard Time") ?
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time") :
TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
Starting with .NET 6, it is finally possible to work with time zones in a cross-platform manner.
The TimeZoneInfo.FindSystemTimeZoneById(string) method automatically accepts either Windows or IANA time zones on either platform and convert them if needed.
// Both of these will now work on any supported OS where ICU and time zone data are available.
TimeZoneInfo tzi1 = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
TimeZoneInfo tzi2 = TimeZoneInfo.FindSystemTimeZoneById("Australia/Sydney");
Note that, as specified on the link, the .NET Core Alpine Linux-based Docker images do not have the necessary tzdata installed by default, so it must be installed in your Dockerfile for this to work correctly.
I was able to support this use-case in my development docker image by doing the following:
cp /usr/share/zoneinfo/America/Los_Angeles "/usr/share/zoneinfo/Pacific Standard Time"
Obviously, I don't think that would be a good idea for production deployments. But it might help in some scenarios.
Quick and dirty solution: serialize your TimeZoneInfo with ToSerializedString in a dummy app on Windows, save the output, then deserialize with FromSerializedString where you need it.
On Windows:
Console.WriteLine(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").ToSerializedString());
Output:
Eastern Standard Time;-300;(UTC-05:00) Eastern Time (US & Canada);Eastern Standard Time;Eastern Daylight Time;[01:01:0001;12:31:2006;60;[0;02:00:00;4;1;0;];[0;02:00:00;10;5;0;];][01:01:2007;12:31:9999;60;[0;02:00:00;3;2;0;];[0;02:00:00;11;1;0;];];
Then:
// TimeZoneInfo is immutable
public static readonly TimeZoneInfo EST = TimeZoneInfo.FromSerializedString(
"Eastern Standard Time;-300;(UTC-05:00) Eastern Time (US & Canada);Eastern Standard Time;Eastern Daylight Time;[01:01:0001;12:31:2006;60;[0;02:00:00;4;1;0;];[0;02:00:00;10;5;0;];][01:01:2007;12:31:9999;60;[0;02:00:00;3;2;0;];[0;02:00:00;11;1;0;];];");
I ended up writing a small helper function:
public static TimeZoneInfo GetTimeZone(string unixId, string windowsId)
{
foreach (TimeZoneInfo timezone in TimeZoneInfo.GetSystemTimeZones())
{
if (timezone.Id == windowsId|| timezone.Id == unixId)
{
return timezone;
}
}
return null;
}
I have a c# desktop application that consumes an API. My Client is from a different time zone. So when we both consume the same API, we get different date time even the date is the same in the database. How can I set the default time zone for my application in c# code so that when I access it from the development environment, I can set my timezone and when I deploy it in production then I can set my client's time zone? I already debugged and found .NET automatically converts the time as soon as it gets the response from the API, so I can not do anything in the response stream.
if the dateTime is changing for you and your client, it means that there is an offset value attached to the dateTime when it is retrieved from the API.
So, do a check for development environment in your application and fetch the UTC DateTime using "UtcDateTime" property of DateTimeOffset type and display it in the UI so that it is the same for both you and your client. In case of the production environment, just keep the usual flowing going.
if(IsDevEnvironment)
DateTimeToDisplay = ApiResponse.DateTimeInDB.UtcDateTime; // assuming DateTimeInDB is of DateTimeOffset type
else
DateTimeToDisplay = ApiResponse.DateTimeInDB
Here is a way to set a timezone, example eastern timezone.
TimeZoneInfo easternZone;
try { easternZone = TimeZoneInfo.FindSystemTimeZoneById("E. Africa Standard Time"); }
catch (TimeZoneNotFoundException) { easternZone = TimeZoneInfo.FindSystemTimeZoneById("Africa/Nairobi"); }
var timestamp = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, easternZone);
try that ;
HAVING (Time BETWEEN DATEADD(d, - 1, GETUTCDATE()) AND GETUTCDATE())
it is helpful for me.
example
ELECT Time, username, COUNT(username) AS CountOfusername
FROM dbo.tabl
GROUP BY Time, username
HAVING (Time BETWEEN DATEADD(d, - 1, GETUTCDATE()) AND GETUTCDATE())
edit
DateTime hwTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Arab Standard Time");
//DateTime hwTime = new DateTime();
try
{
TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Arab Standard Time");
Console.WriteLine("{0} {1} is {2} local time.",
hwTime,
hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName,
TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The registry does not define the Arab Standard Time zone.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the Arab Standard Time zone has been corrupted.");
}
us this code in your application
I have a .NET Core 2.2 web app running inside a docker-compose app. I'm saving different UTC time stamps to a database, like this:
//set asked state on question
Question questionInDb = c.Questions.Single(x => x.Id == id);
questionInDb.Asked = true;
questionInDb.AskTime = DateTime.UtcNow;
c.SaveChanges();
In the apppsettings.json I have a section for my app's settings, containing the locale the application is supposed to run on. I set the CultureInfo.CurrentCulture like so:
//get locale from settings, defaulting to de-DE
string locale = "de-DE";
locale = Configuration.GetValue<string>("AppSettings:Locale");
var ci = new CultureInfo(locale, false);
//actually setting locale
CultureInfo.CurrentCulture = ci;
I set up my Entity Framework to set all DateTimes to DateTimeKind.Utc, so the app knows the DateTimes it gets are all UTC.
Now, when displaying the time on a view, it's not the right hour. In the view.cshtml I'm using #q.AskTime.ToString("HH:mm:ss dd.MM.yy"), but it always returns the exact value from the database. It's supposed to show one hour more.
I even tried setting the container's timezone to CET (Centran Euopean Time) using ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone (in my docker-compose I set the environment variable TZ to Europe/Berlin).
I'm out of ideas.
Cultures (also known as locales) are used for displaying and formatting data and is a different concept than timezones. You can find and create a specific TimeZoneInfo from a timezone name and convert an UTC date and time using the ConvertTimeFromUtc() method.
Keep in mind that different operating systems use different timezone names. For example you can use Europe/Berlin for Linux and Central Europe Standard Time for Windows.
Example:
TimeZoneInfo tz;
try
{
// Linux
tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
}
catch (TimeZoneNotFoundException)
{
try
{
// Windows
tz = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
}
catch (TimeZoneNotFoundException)
{
// Fallback to UTC
tz = TimeZoneInfo.Utc;
}
}
var converted = tz.ConvertTimeFromUtc(DateTime.UtcNow);
I'm creating a C# MVC project that uses Oracle Database. While trying to connect to the DB, I get following error:
ORA-00604: error occurred at recursive SQL level 1
ORA-01882: timezone region not found.
When I try to connect via SQL developer it works fine. Is there any way that I can solve this problem ?
NOTE: I'm using IIS express for testing
I was facing the same issue using ODP.NET for .NET Core on Pivotal Cloud Foundry. The core issue is missing timezone information.
Added a timezone by setting "TZ" environment variable for my instance. That fixed the issue for me.
Actually, this error because of some issue with application Oracle driver which you use and Oracle version of the DB.
NOTE: Please check Both of them and use Updated Oracle versions for your application.
ORA-01882: timezone region not found
Had the same problem in .Net Core 3.1 API with controllers.
had the error on debug, and solved it adding TZ on \Properties\launchSettings.json:
"Docker": {
...
"environmentVariables": {
"TZ": "WET"
}
}
This code does not provide an answer but it may help to find your problem.
The code is too big for a comment.
Please execute this program. What is the output? Where does it fail?
static void Main(string[] args)
{
Console.WriteLine("OracleGlobalization.TimeZone = {0}", Oracle.DataAccess.Client.OracleGlobalization.GetClientInfo().TimeZone);
Console.WriteLine(String.Empty);
Console.WriteLine("TimeZone.CurrentTimeZone.StandardName = {0}", TimeZone.CurrentTimeZone.StandardName);
Console.WriteLine("TimeZone.CurrentTimeZone.DaylightName = {0}", TimeZone.CurrentTimeZone.DaylightName);
Console.WriteLine(String.Empty);
Console.WriteLine("TimeZoneInfo.Local.DisplayName = {0}", TimeZoneInfo.Local.DisplayName);
Console.WriteLine("TimeZoneInfo.Local.Id = {0}", TimeZoneInfo.Local.Id);
Console.WriteLine("TimeZoneInfo.Local.StandardName = {0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("TimeZoneInfo.Local.DaylightName = {0}", TimeZoneInfo.Local.DaylightName);
Console.WriteLine(String.Empty);
var str = new Oracle.DataAccess.Client.OracleConnectionStringBuilder();
str.UserID = "<username>";
str.Password = "<password>";
str.DataSource = "<database name>";
using ( var con = new Oracle.DataAccess.Client.OracleConnection(str.ConnectionString) ) {
con.Open();
Console.WriteLine("Oracle.DataAccess: OracleConnection -> SessionInfo.TimeZone = {0}", con.GetSessionInfo().TimeZone);
Console.WriteLine("Oracle.DataAccess: Version = {0}", FileVersionInfo.GetVersionInfo(con.GetType().Assembly.Location).FileVersion.ToString());
var tz = new Oracle.DataAccess.Client.OracleCommand("SELECT SESSIONTIMEZONE FROM dual", con).ExecuteScalar();
Console.WriteLine("Oracle.DataAccess: SESSIONTIMEZONE = {0}", tz.ToString());
con.Close();
}
Console.WriteLine(String.Empty);
var strm = new Oracle.ManagedDataAccess.Client.OracleConnectionStringBuilder();
str.UserID = "<username>";
str.Password = "<password>";
str.DataSource = "<database name>";
using ( var con = new Oracle.ManagedDataAccess.Client.OracleConnection(str.ConnectionString) ) {
con.Open();
Console.WriteLine("Oracle.ManagedDataAccess: OracleConnection -> SessionInfo.TimeZone = {0}", con.GetSessionInfo().TimeZone);
Console.WriteLine("Oracle.ManagedDataAccess: Version = {0}", FileVersionInfo.GetVersionInfo(con.GetType().Assembly.Location).FileVersion.ToString());
var tz = new Oracle.ManagedDataAccess.Client.OracleCommand("SELECT SESSIONTIMEZONE FROM dual", con).ExecuteScalar();
Console.WriteLine("Oracle.ManagedDataAccess: SESSIONTIMEZONE = {0}", tz.ToString());
con.Close();
}
}
Update
According to your profile you are based in Sri Lanka. Time zone of Sri Lanka seems to be fairly "volatile", see text from IANA TimeZone Database:
Sri Lanka
From Paul Eggert (2013-02-21):
Milne says "Madras mean time use from May 1, 1898. Prior to this Colombo
mean time, 5h. 4m. 21.9s. F., was used." But 5:04:21.9 differs considerably
from Colombo's meridian 5:19:24, so for now ignore Milne and stick with
Shanks and Pottenger.
From Paul Eggert (1996-09-03):
"Sri Lanka advances clock by an hour to avoid blackout"
(http://www.virtual-pc.com/lankaweb/news/items/240596-2.html, 1996-05-24,
no longer available as of 1999-08-17)
reported "the country's standard time will be put forward by one hour at
midnight Friday (1830 GMT) 'in the light of the present power crisis'."
From Dharmasiri Senanayake, Sri Lanka Media Minister (1996-10-24), as quoted
by Shamindra in Daily News - Hot News Section
(1996-10-26):
With effect from 12.30 a.m. on 26th October 1996
Sri Lanka will be six (06) hours ahead of GMT.
From Jesper Nørgaard Welen (2006-04-14), quoting Sri Lanka News Online
http://news.sinhalaya.com/wmview.php?ArtID=11002 (2006-04-13):
0030 hrs on April 15, 2006 (midnight of April 14, 2006 +30 minutes)
at present, become 2400 hours of April 14, 2006 (midnight of April 14, 2006).
From Peter Apps and Ranga Sirila of Reuters (2006-04-12) in:
http://today.reuters.co.uk/news/newsArticle.aspx?type=scienceNews&storyID=2006-04-12T172228Z_01_COL295762_RTRIDST_0_SCIENCE-SRILANKA-TIME-DC.XML
[The Tamil Tigers] never accepted the original 1996 time change and simply
kept their clocks set five and a half hours ahead of Greenwich Mean
Time (GMT), in line with neighbor India.
From Paul Eggert (2006-04-18):
People who live in regions under Tamil control can use [TZ='Asia/Kolkata'],
as that zone has agreed with the Tamil areas since our cutoff date of 1970.
From Sadika Sumanapala (2016-10-19):
According to http://www.sltime.org (maintained by Measurement Units,
Standards & Services Department, Sri Lanka) abbreviation for Sri Lanka
standard time is SLST.
From Paul Eggert (2016-10-18):
"SLST" seems to be reasonably recent and rarely-used outside time
zone nerd sources. I searched Google News and found three uses of
it in the International Business Times of India in February and
March of this year when discussing cricket match times, but nothing
since then (though there has been a lot of cricket) and nothing in
other English-language news sources. Our old abbreviation "LKT" is
even worse. For now, let's use a numeric abbreviation; we can
switch to "SLST" if it catches on.
I assume your database does not recognize your current local time zone of your computer. There might be three possible solutions:
Change your computer locale settings to a more stable one, e.g. India
Update database with latest version of time zone file, see Upgrading the Time Zone File and Timestamp with Time Zone Data
In case you use the ODP.NET Unmanaged driver Oracle.DataAccess.dll (unfortunately you did not tell us) you can set ORA_SDTZ environment variable or Registry key HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_<Oracle home name>\ORA_SDTZ / HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ORACLE\KEY_<Oracle home name>\ORA_SDTZ to desired time zone value. Note, the ODP.NET Managed Driver does not read this Registry values.
According to this straight from an Oracle employee, set UseHourOffsetForUnsupportedTimezone property for the Connection to true, before opening it.
con.UseHourOffsetForUnsupportedTimezone = true;
con.Open();
This worked for me as I did not have control on the OracleDB server, and in my crossplatform application, the environment variable usage was not being consistent across linux distros. I had ran Weinfried's snippet on all of them to test and there was no timezone descrepency with OracleDB in my case(both on UTC, no DST) but was still failing on the same plank.
According to this and this:
In C:\Program Files\datamodeler 3\datamodeler\bin\datamodeler.conf add
AddVMOption -Duser.timezone="+02:00"
I am logging time in many places
If Request.DynamicSettings.AirlineSettings.AirlineGeneralSettings.TimeLogEnabled Then
StartTime = DateTime.Now
LogTime(Reflection.MethodBase.GetCurrentMethod.DeclaringType.FullName, Reflection.MethodBase.GetCurrentMethod.Name, StartTime, DateTime.Now, "AB-SCR(I)", 0,)
End If
all places i have used
DateTime.Now
I am facing an issue now,
I am currently hosting this in a gulf server, GMT +4:00
I need to host this same project for another country at Gmt +3Gmt
for this hosting i need time to be logged using that country's local time.
Is there any way to do this, without having to modify each and every line of my code.
i have seen this article timzone with asp.net but as my service is already up i have a lot of codes to change, i am looking for a simpler solution.
thanks.
A few things:
You cannot change the time zone in the IIS configuration or web.config. This is not an IIS problem, but rather a problem in your application code.
DateTime.Now should never be used in a server side application, such as an ASP.Net web application. Read The case against DateTime.Now.
If you are just timing how long something takes to run, don't use DateTime at all. Instead, use System.Diagnostics.Stopwatch.
Stopwatch sw = Stopwatch.StartNew();
// ...do some work ...
sw.Stop();
TimeSpan elapsed = sw.Elapsed; // how long it took will be in the Elapsed property
If you actually want the current time in a specific time zone, then you need to know the time zone identifier. (GMT+4 and GMT+3 are not time zones, but rather time zone offsets see "Time Zone != Offset" in the timezone tag wiki.) You can see a list of Windows time zones by using TimeZoneInfo.GetSystemTimeZones(), or by calling tzutil /l on the command line.
Then in your application:
string tz = "Arabian Standard Time";
DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
You should probably refactor your code such that this is done inside your LogTime method. Then you will have only one place to set the time zone for your application.