[SOLVED] STRPTIME and STRFTIME and Daylight saving time

Hello,

can someone please tell me where this extra hour is comming from:

Set(time_var=${STRFTIME(${EPOCH},Europe/Stockholm,%Y-%m-%d %H:%M)});
Noop(${time_var});
Noop(${EPOCH});
Set(time_conv=${STRPTIME(${time_var},Europe/Stockholm,%Y-%m-%d %H:%M)});
Noop(${STRFTIME(${time_conv},Europe/Stockholm,%Y-%m-%d %H:%M)});

And this gives the output:

  -- Executing Set("SIP/001-081afde0", "time_var=2007-04-26 08:06") in new stack
  -- Executing NoOp("SIP/001-081afde0", "2007-04-26 08:06") in new stack
  -- Executing NoOp("SIP/001-081afde0", "1177567577") in new stack
  -- Executing Set("SIP/001-081afde0", "time_conv=1177571160") in new stack
  -- Executing NoOp("SIP/001-081afde0", "2007-04-26 09:06") in new stack

asterisk*CLI> !date
tor apr 26 08:06:59 CEST 2007

As you can see one hour is added in the process, by the STRPTIME.
I use Asterisk 1.4.2.

I guess I could just subtract one hour but it seems like a strange thing to do. Both STRPTIME and STRFTIME behaves the same not passing Europe/Stockholm to them.

Please advice,
/Johan

Setting the date to 24th december doesn’t give this misscalculation.
Seems like a Daylight saving time problem to me. Here in sweden we have GMT+1 normaly, and GMT+2 (CET) in the summer.

This is the output for the date set to 2007-12-24:

  -- Executing Set("SIP/001-081b5030", "time_var=2007-12-24 12:00") in new stack
  -- Executing NoOp("SIP/001-081b5030", "2007-12-24 12:00") in new stack
  -- Executing NoOp("SIP/001-081b5030", "1198494017") in new stack
  -- Executing Set("SIP/001-081b5030", "time_conv=1198494000") in new stack
  -- Executing NoOp("SIP/001-081b5030", "2007-12-24 12:00") in new stack
asterisk*CLI> !date
mån dec 24 12:00:29 CET 2007

STRFTIME takes this into account but STRPTIME doesn’t?

For those who need to use this function, this workaround maybe useful.
It tries to figure out if STRPTIME calculates wrong. The only drawback is if you need to use the function on a that isn’t close to the current date the calculation may be wrong because daylight savnings may have started / ended.

I use it as a macro, in AEL.

macro macro-strptime-hotfix(datetime,timezone,format,result) {
//The result will be stored in the variable ${result}

Set(now=${EPOCH});
Set(nowcalc=${STRPTIME(${STRFTIME(${now},${timezone},%Y-%m-%d %H:%M:%S)},${timezone},%Y-%m-%d %H:%M:%S)});

Set(strptime_calcoffset=$[${now}-${nowcalc}]);
if (${strptime_calcoffset} != 0) {
   Noop(STRPTIME calcs wrong with ${strptime_calcoffset} seconds.);
}

Set(${result}=$[${STRPTIME(${datetime},${timezone},${format})}+${strptime_calcoffset}]);
};


//Example on usage
&macro-strptime-hotfix(1180562399,,%Y-%m-%d %H:%M:%S,date);
Noop(Result: ${date});