### Author Topic: Converting Epoch Seconds to date values (yy mm dd hh mm ss)  (Read 5008 times)

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« on: June 22, 2012, 06:35:23 am »
Hi All,

I have been searching the source code of version 17 for this, but have not found it.
Any hint on how to do this would be greatly appreciated.

Regards,
Morten A. Steien

#### Graeme

• Senior Community Member
• Posts: 2298
• Hero Points: 301
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #1 on: June 22, 2012, 07:50:48 am »

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #2 on: June 22, 2012, 08:01:48 am »
I must admit that I have seen this, but still I was hoping for a better solution in v17...

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #3 on: June 22, 2012, 08:07:03 am »
Looks like the DateTime class has functions toTimeB and fromTimeB.
What is the relation between binary time and epoch seconds? This should be a fixed value.
ie something like:
- Epoch * 1000 + Offset = Binary
- Binary put in DateTime class
- DateTime.toString

Any thoughts?

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #4 on: June 22, 2012, 10:41:53 am »
This seems to do the trick...

Code: [Select]
`#import "se/datetime/DateTime.e"int Epoch=1340360050; // Just an example time_str Year="0", Month="0", Day="0";_str Hour="0", Minute="0", Second="0";se.datetime.DateTime myTime;myTime = se.datetime.DateTime.fromTimeB((Epoch+10302239676800)*1000);parse myTime.toStringLocal() with Year '-' Month '-' Day 'T' Hour ':' Minute ':' Second '.';insert_line(Hour ':' Minute ':' Second ' of ' Year '-' Month '-' Day);`

#### Graeme

• Senior Community Member
• Posts: 2298
• Hero Points: 301
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #5 on: June 22, 2012, 11:01:18 am »
I tried calling fromTimeB with random strings and got slick C stacks  - suggesting that not all strings are valid time/date  - so maybe it's created by bitshifting various things.

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #6 on: June 23, 2012, 07:45:47 am »
The parse is wasted of caurse... I see the DateTime class have all the necessary functions....

#### Bamsen

• Community Member
• Posts: 47
• Hero Points: 1
##### Re: Converting Epoch Seconds to date values (yy mm dd hh mm ss)
« Reply #7 on: November 15, 2012, 10:44:14 am »
Graeme is right, of cause. The SE binary time is two part and can not be converted in that fashion.
I ended up just doing it with brute force.
Code: [Select]
`_command MAS_EPOCH_To_Date() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL){  int eSecond=0;  int Kolonne=p_col;  MAS_Copy_Word(); // Just select the word under the cursor  if (isnumber(MST_Get_Selected_Text())) {    eSecond=MST_Get_Selected_Text();    eSecond+=3600; // Plus one hour for CET    int eYear=1970;    while (eSecond >= MST_Seconds_In_Year(eYear)) {      eSecond-=MST_Seconds_In_Year(eYear);      eYear+=1;    }    int eMonth=1;    while (eSecond >= MST_Seconds_In_Month(eMonth,eYear)) {      eSecond-=MST_Seconds_In_Month(eMonth,eYear);      eMonth+=1;    }    int eDay=eSecond/86400;    eSecond-=eDay*86400;    eDay++; // There is no day 0.    int eHour=eSecond/3600;    eSecond-=eHour*3600;    int eMinute=eSecond/60;    eSecond-=eMinute*60;    _str Result=''_pad(eYear,2,'0','L')'-'_pad(eMonth,2,'0','L')'-'_pad(eDay,2,'0','L')' '_pad(eHour,2,'0','L')':'_pad(eMinute,2,'0','L')':'_pad(eSecond,2,'0','L');    _insert_text('='Result);  }  deselect();  p_col=Kolonne;  down(1);}int MST_Seconds_In_Year(int lYear){  int i=31536000;  if ((lYear % 4) == 0) { i+= 86400 }  return i;}int MST_Seconds_In_Month(int lMonth,int lYear){  int i=0;  switch (lMonth) {  case 1:        // Jan  case 3:        // Mar  case 5:        // Mai  case 7:        // Jul  case 8:        // Aug  case 10:       // Okt  case 12:       // Dec    i=86400*31;    break;  case 2:        // Feb    i=86400*28;    if ((lYear % 4) == 0) { i+= 86400 }    break;  case 4:        // Apr  case 6:        // Jun  case 9:        // Sep  case 11:       // Nov    i=86400*30;    break;  }  return i;}`