Twitter time to local time c# - c#

How do i convert the time of tweets made by the user to localtime? The created_at gives me the time of the tweet created and is based on the user's country, how to i change it to my localtime using c#. The format of the time is "Fri Jul 27 15:14:11 +0000 2012". What do they mean by utc_offset and how can it be used to change the timezone?
I am not really familiar with the timezone stuff, thank you for your help :)

You'll want to use the DateTimeOffset structure. This allows you to capture a date and a time as well as offset from UTC as well.
The format that you want to use for Twitter times is:
string twitterDate = "Fri Jul 27 15:14:11 +0000 2012";
DateTimeOffset dt = DateTimeOffset.ParseExact(twitterDate,
"ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Note that while not documented, zzzz does work as a custom date time format and succeeds in parsing the offset correctly. This means you should be careful in relying on it in the future (unless the documentation changes to reflect this functionality).
Once you have the DateTimeOffset, you know the offset from UTC. If you want to get it in your local time, you can call the ToLocalTime method on the DateTimeOffset that was parsed and it will return an instance that has an offset based on your local settings.

This should be it:
DateTime localDateTime = DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss zzz yyyy", new CultureInfo("en-US"));
Unless you give a DateTimeKind in the constructor of a DateTime, the DateTime will be in the timezone the administrator of the workstation/server has selected for Windows.

Related

Parsing Twitter Created At Date as UTC

I realize there are many threads about parsing twitter dates, but I am having a peculiar issue, and haven't been able to find any threads specific to this.
I am retrieving this post: https://twitter.com/bodegamcallen/status/1033757489567805440
which has a created date of 8/26/2018 at 9:46 AM Central Time.
The created date in the api is this: "Sun Aug 26 16:46:06 +0000 2018"
Which makes sense as 16:46pm (2:46pm) UTC is 9:46 AM Central (-500).
However, I'm parsing this using this code:
statusModel.DatePosted = DateTime.ParseExact(
this.created_at, "ddd MMM dd HH:mm:ss zzzz yyyy",
CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeUniversal);
and instead of getting the desired UTC time shown above, I'm getting this: 8/26/2018 4:46:06 PM
If I change the DateTimeStyle to None or AssumeLocal, I get this: 8/26/2018 11:46:06 AM
Neither of which is correct!
I've tried different formats from other threads like:
"ddd MMM dd HH:mm:ss K yyyy"
"ddd MMM dd HH:mm:ss Z yyyy"
"ddd MMM dd HH:mm:ss zzz yyyy"
"ddd MMM dd HH:mm:ss +ffff yyyy"
I also tried using DateTimeOffset.Parse and changing the culture to mine (en-us)
they all give me the same results.
What am I doing wrong? How do I get the datetime to be the same UTC time as the date value I'm getting from the API?
Aha! It turns out that my twitter account settings were configured to be in Pacific Time, rather than Central Time, and that would account for the two hour difference.
I changed my time zone settings for my account as described here: https://help.twitter.com/en/managing-your-account/how-to-change-time-zone-settings
And after changing to Central Time, the API data is coming in correct!
hope this helps someone else. Time zones, amirite?
A few things:
zzzz is not a valid specifier. Always check these docs if you're uncertain.
K is the only time zone specifier that is appropriate for DateTime parsing. The others (z, zz, and zzz) are for DateTimeOffset parsing.
When there is any offset or time zone information in the input string (even if it's +0000 or Z), DateTime.Parse and DateTime.ParseExact will assume by default that you want the output kind to be DateTimeKind.Local and that you want the value adjusted to local whatever it is in the input string. Examine the output .Kind if you're not certain if it's working correctly. If you want UTC instead, you should pass DateTimeStyles.AdjustToUniversal.
You don't need to pass DateTimeStyles.AssumeUniversal, because there is time zone information present in the input string.
It's not invalid, but you don't need to pass CultureInfo.InvariantCulture.DateTimeFormat. It's sufficient, and more common, to pass CultureInfo.InvariantCulture.
Putting this all together, your code should be:
statusModel.DatePosted = DateTime.ParseExact(
this.created_at, "ddd MMM dd HH:mm:ss K yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
And of course, you'd save some hassle and confusion if you changed your DatePosted to be a DateTimeOffset type instead. It's superior for this sort of scenario, and then you don't have to worry about DateTimeKind or DateTimeStyles.

convert datetime string to Utc

how to convert datetime string to utc time format in GMT.
var x = "02/01/2017 10:00";
var z = DateTime.ParseExact(x, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);
Actually the thing you have tried is wrong, what you can do is, get the DateTime value equivalent to the given date and then convert them to the UTC time, try something like the following:
string inputDateStr = "02/01/2017 10:00";
DateTime inputDate;
if (DateTime.TryParseExact(inputDateStr, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
Console.WriteLine("Date time Now : {0} ", inputDate);
Console.WriteLine("Date time UTC : {0} ", inputDate.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));
}
Working Example
I am not sure what you wanted to do. But, Universal Time, Zulu time, and UTC are effectively modern names for Greenwich Mean Time (GMT). So both the times will be same. You can use the DateTime.ToUniversalTime() or DateTime.UtcNow method to get the UTC time.
It's not completely clear what you are asking... UTC isn't a format, it's a timezone, equivalent to GMT (well, actually that isn't strictly true, but for the purposes of this question it should be ok.
What point in time is your string representing? Is it UTC? Or is it the local time in Moscow? You need to have the answer to that, because they are completely different times.
If the string represents a UTC time, then you can do something like this:
// first parse it to a DateTime object. Notice that the format string corresponds with the string you have - in your code, they were completely different.
var dateTimeWithUnspecifiedKind = DateTime.ParseExact(x, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
So how you have a datetime object. It has a "Kind" property of Unspecified. So it isn't storing any information to specify that it's supposed to be UTC, rather than Moscow or New York time.
// so we do this...
var dateTimeAsUtc = DateTime.SpecifyKind(dateTimeWithUnspecifiedKind, DateTimeKind.Utc);
That means, "keep the numbers the same, but make a note that the datetime is to be interpreted as a UTC datetime".
Then, if you want to convert it back into a string, you can do something like:
var s = dateTimeAsUtc.ToString("O");
Which will give you a nice representation:
2017-01-02T10:00:00.0000000Z
You are lucky you are interested in UTC. The datetime class can only do UTC, "local" (whatever that is), or "Unspecified". It's not super useful for this sort of thing. DateTimeOffset is a bit better - it's basically a DateTime but it also stores the difference between UTC and the offset that's in force at the time.
That may be all you need. If you ever need real clarity around this stuff though, take a look at Noda time - an alternative set of date and time classes for .NET.

How to parse default git date format in C#

How do you parse the default git format to a DateTime in C#?
As per What is the format for --date parameter of git commit
The default date format from git looks like Mon Jul 3 17:18:43 2006 +0200.
Right now I don't have control over the output, this strings comes from another tool that printed the date and I need to parse it.
I wouldn't parse this to DateTime, I would parse it to DateTimeOffset since it has a UTC offset value inside of it.
Why? Because if you parse it to DateTime, you will get a DateTime as Local and it might generate different results for different machines since they can have timezone offsets of that time.
For example, I'm in Istanbul and we use Eastern European Time which use UTC+02:00. If I run the example of your code with ParseExact method, I will get the 07/03/2006 18:18:43 as a Local time.
Why? Because in 3 July 2006, my timezone was in a daylight saving time which is UTC+03:00. That's why it generates 1 hour forwarded result. That's the part makes it ambiguous when you parse it to DateTime.
string s = "Mon Jul 3 17:18:43 2006 +0200";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM d HH:mm:ss yyyy K",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dto))
{
Console.WriteLine(dto);
}
Now, you have a DateTimeOffset as 07/03/2006 17:18:43 +02:00. You can still get the DateTime part with it's .DateTime property but it's Kind will be Unspecified in that case.
But of course, I suggest to use Noda Time instead which can solve most of the DateTime weirdness.
So far the best format string I found is ddd MMM d HH:mm:ss yyyy K.
DateTime date;
DateTime.TryParseExact(
gitDateString,
"ddd MMM d HH:mm:ss yyyy K",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out date
);

Troubles parsing DateTime from string

I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this:
Thu Jul 12 08:39:56 GMT+0100 2012
At first I just did a string.split and took out everything after the time (GMT+0100 2012) and inserted 2012 after the date.
This worked great until the date changed to:
Thu Jul 12 08:39:56 GMT+0000 2012
So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#.
Any advice would be appreciated.
You can use DateTime.ParseExact with a custom date and time format string:
DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012",
"ddd MMM dd hh:mm:ss 'GMT'K yyyy",
CultureInfo.InvariantCulture)
This will throw a format exception if the string and format string do not match exactly, so you may want to use DateTime.TryParseExact that will return a false if it fails.
Instead of DateTime you may want to use DateTimeOffset that preserved timezone information , as #Keith commented - this may be important to your application.
Two things you can do: First, you should be able to use a custom format string with a ParseExact method, either from DateTime or DateTimeOffset (I would use DateTimeOffset if the actual time zone of the stamp is important, and not just the equivalent time in UTC or your local time zone).
Have a look: DateTime custom format string
The format string would probably be something like #"ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy".
However, there's one snag; the .NET time zone offset ("zzzz" or simply "K") always includes a colon between the hour and minute when expressed as a string, which your input strings do not have. There is no way I know of to specify that the time zone offset doesn't/shouldn't have this colon, and I'm pretty sure that trying to parse it without a colon would cause an error.
The simplest workaround is to remove that specific colon from the string prior to parsing it. The code for that given your input is simply to remove the last colon character in the string:
var updatedString = inputString.Remove(inputString.LastIndexOf(':'), 1);
Try DateTime.Parse method to parse your date.
This should work:
XmlConvert.ToDateTime(textBox1.Text, "ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy");

Need help with C# ParseExact syntax and moving between timezones

Okay, I have decided to let the magic of Stackoverflow work for me!
I have a date in the format: "Apr 18 2011 19:30 EDT" that I need to push into a DateTime object in C#. One caviat, I also want to shift it to UTC too. Obivisouly when DST is over it'll come over as EST.
I know that I need a statement like:
DateTime.ParseExact("Apr 18 2011 19:30 EDT", "MMM DD yyyy something something ", CultureInfo.InvariantCulture, DateTimeStyles.None, out convertedDate);
But getting it over to UTC is above my knowlwedge level.
So in summary, I need:
To turn Apr 18 2011 19:30 EDT into a DateTime
Convert the EDT timezone to UTC time.
End up with a DateTime object.
What's the code, wizards?
Well, if it's always going to be in Eastern Daylight Time, you can do something like:
// Parse string. We don't need escaping since E,D and T
// are not considered special characters by ParseExact.
var dateTimeInEasternTime = DateTime.ParseExact("Apr 18 2011 19:30 EDT",
"MMM dd yyyy HH:mm EDT",
CultureInfo.InvariantCulture);
// Convert from the relevant timezone to UTC.
var dateTimeInUTC = TimeZoneInfo.ConvertTime
(dateTimeInEasternTime,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"),
TimeZoneInfo.Utc);
You can cache the TimeZoneInfo representing EST (which becomes EDT while daylight savings is on) to prevent the lookup.
If the string could end with a three letter code representing some arbitrary time-zone, it's going to be a lot more difficult since there are many conventions for them, none of which (AFAIK) are currently supported by .NET. Your best bet would be to first build a lookup from the code to the relevant TimeZoneInfo (perhaps through the Id property) after which you can do the conversion with TimeZoneInfo.ConvertTime as usual.
I'm not sure exactly where you're getting hung up. It sounds like you have successfully parsed the string into a DateTime.
To convert the value to UTC, call the ToUniversalTime() method. Note that this will assume the current time value is relative to your system's current time zone.
ToUniversalTime() converts to a DateTime value.

Categories

Resources