c# - Parsing DateTime to Unix with Timezone Offset -


i have method takes in string such 2014-03-10t08:00:00 , converts unix number this: 1394456400

below 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 condition datetimes not have timezone offsets.

my code works above format, interested in converting numbers have offset timezone, such as:

2014-03-10t08:00:00-07:00

the new method need not check both conditions -- input offset fine.

i have looked @ these threads such as:

  1. parsing *supposed* unix date time (with timezone info) datetime object

  2. parsing datetime timezone offsets

i have tried: format = "yyyy-mm-dd't'hh:mm:ss-hh:mm"; condition, don't think works because requires addition or subtraction of offset.

edit:

on self-experimentation , jon's comment of using datetimeoffset, came this:

public static double converttounix2(string datestring) {     var datetimeoffset = datetimeoffset.parse(datestring, null);     return (datetimeoffset - new datetime(1970, 1, 1)).totalseconds; } 

on input of 2014-03-10t08:00:00-07:00, outputs 1394445600.

i have indicates parse 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 , outputs 3/10/2014 8:00:00 -07:00.

is logic of taking datetime object , subtracting epoch time correct approach?


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -