1

I have this method that takes in a string such as 2014-03-10T08:00:00 and converts it to a unix number like this: 1394456400

Below is said method:

public string convertToUnix(string dateString)
{
    string format;
    DateTime result;
    CultureInfo provider = CultureInfo.InvariantCulture;

    format = "yyyy-MM-dd'T'HH:mm:ss";

    result = DateTime.ParseExact(dateString, format, provider);
    return (result - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds.ToString();
}

My format variable is the condition for those DateTimes that do not have timezone offsets.

My code works perfectly for the above format, but I am interested in converting numbers that have an offset which is the timezone, such as:

2014-03-10T08:00:00-07:00

The new method need not check for both conditions -- just the input with offset is fine.

I have looked at these threads such as:

  1. Parsing a *supposed* Unix date time (with timezone info) to DateTime object

  2. Parsing Datetime with timezone offsets

I have also tried: format = "yyyy-MM-dd'T'HH:mm:ss-HH:mm"; as my condition, but I don't think this works because it requires an addition or subtraction of the offset.

EDIT:

On self-experimentation and Jon's comment of using DateTimeOffset, I came up with this:

public static double convertToUnix2(string dateString)
{
    var dateTimeOffset = DateTimeOffset.Parse(dateString, null);
    return (dateTimeOffset - new DateTime(1970, 1, 1)).TotalSeconds;
}

On an input of 2014-03-10T08:00:00-07:00, it outputs 1394445600.

I also have this which indicates the parse is successful:

public static string convertToUnix3(string dateString)
{
    var dateTimeOffset = DateTimeOffset.Parse(dateString, null);
    return dateTimeOffset.ToString();
}

Input of 2014-03-10T08:00:00-07:00 and outputs 3/10/2014 8:00:00 AM -07:00.

Is the logic of taking the DateTime object and subtracting it from the Epoch time a correct approach?

Community
  • 1
  • 1
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • So you have a method - what does it do, vs what you want it to do? (Personally I would *strongly* avoid using local times anywhere, but that's a different matter...) And why are you returning a *string* if you're really trying to convert to a *number*? Let the caller do that if they want to... – Jon Skeet Mar 10 '14 at 16:09
  • Also: look at `DateTimeOffset.Parse`... – Jon Skeet Mar 10 '14 at 16:10
  • It takes a time format like `2014-03-10T08:00:00` and converts it to a unix time format like this: `1394456400`. I want the input condition to also involve a timezone offset that looks like this `-X0:00`. I thought I made it pretty clear in my question. – theGreenCabbage Mar 10 '14 at 16:10
  • What you haven't made clear is whether the code you've already got *works* for you, and you just need the UTC offset handling, or whether that's also not quite doing what you want (and if so how). And do you mean that you want a second method for the other format, or that you want this method to cope with both formats? – Jon Skeet Mar 10 '14 at 16:11
  • Okay, will clarify in an edit. – theGreenCabbage Mar 10 '14 at 16:12
  • Hi @JonSkeet I took your advice of using `DateTimeOffset`. Is how I implemented it the correct approach? – theGreenCabbage Mar 10 '14 at 16:31
  • I wouldn't just use `DateTimeOffset.Parse` like that - you know the exact format you're expecting, so I'd specify that and use the invariant culture. I'd also make the Unix epoch a static readonly field somewhere, and initialize it with a `DateTimeKind` of `Utc`. – Jon Skeet Mar 10 '14 at 17:12
  • I've done the second part of your reply (static readonly field for the epoch time), but I don't quite understand what you mean by "specify the exact format I am expecting". How should I use the `DateTimeOffset.Parse` Jon? – theGreenCabbage Mar 10 '14 at 20:00
  • Use `DateTimeOffset.ParseExact`, just as you've done in the first method - but with a format string that includes the offset. (I can't remember the specifier immediately, but it'll be in the documentation.) Or of course you could use [Noda Time](http://nodatime.org) which has various parser patterns built in ;) – Jon Skeet Mar 10 '14 at 20:02

0 Answers0