Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conversion from Unix Epoch Time
#1
Hello Gintaras,
Hope all is well!
I have to convert from Unix Epoch Time to regular humna readable Date Time format.
eg. 1556915575 --> '5/3/2019 4:32 PM' 
I am not sure if this is already enabled by the DateTime functions in QM. 
I looked through and wasn't able to figure out myself.
A colleague gave me the code below for conversion. But before I went to figure out how to implement in QM, I thought I would check if this conversion not already supported by existing functions.

Thanks so much,
S
Code:
Copy      Help
 private string sUTCOffset = string.Empty;
private string sUNIXEpoch = "1-Jan-1970 00:00:00";
 
// Determine the daylightTime saving so the offset can be applied correctly.
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
bool isDayLightSavingTime = tzi.IsDaylightSavingTime(DateTime.Now);
if (isDayLightSavingTime)
    sUTCOffset = " - 4/24";
else
sUTCOffset = " - 5/24";
 
// convert oracle unix date_time_created to datetime characters in c# codes
"TO_CHAR((TO_DATE('" + sUNIXEpoch + "','dd-mon-yyyy hh24:mi:ss') + (DOSR_STUDY.DATE_TIME_CREATED/60/60/24 ) " + sUTCOffset + " ), 'MM/DD/YYYY HH24:MI:SS') AS DATE_TIME_CREATED
 
// convert oracle unix date_time_created to datetime in c# codes
to_date('" + sUNIXEpoch + "','dd-mon-yyyy hh24:mi:ss') + (dosr_study.DATE_TIME_CREATED/60/60/24) " + sUTCOffset + " ) AS Date_Time_Created,sysdate - " + Minutes + "/(24*60), "
#2
Member function DateTime.FromUnix
Code:
Copy      Help
function long'time_t

;Initializes this variable from a C/Unix date/time - seconds since midnight January 1, 1970 UTC, aka C type time_t.

;REMARKS
;To store Unix time you can use a variable of type int or long. Use long to avoid the year 2038 problem.
;<link>https://en.wikipedia.org/wiki/Year_2038_problem</link>

;EXAMPLE
;DateTime t.FromUnix(0)
;out t.ToStr(4)
;out t.ToUnix


t=time_t*10000000+116444736000000000

Member function DateTime.ToUnix
Code:
Copy      Help
function'long

;Converts to C/Unix date/time - seconds since midnight January 1, 1970 UTC, aka C type time_t.

;REMARKS
;To store Unix time you can use a variable of type int or long. Use long to avoid the year 2038 problem.
;<link>https://en.wikipedia.org/wiki/Year_2038_problem</link>


ret t-116444736000000000/10000000
#3
Wow!
Thanks for such a rapid reply and useful function!
S
#4
I was confused at first why I was 4 hours off using this function, when I realized about the offset from GMT and whether we were in Daylight Savings Time or not.
For my immediate purposes, I can hardcode these in, but it would be great to have a general use function.
I see the TimeZoneInfo class used in my colleagues example listed under .NET framework.
https://docs.microsoft.com/en-us/dotnet/...work-4.8  
If I understand correctly, .NET functions can't be easily pulled into QM. Is that correct?
S
#5
The DateTime variable will have the same time zone offset as the Unix time variable. If the Unix time is UTC, then the DateTime variable is UTC too.

Macro Macro354
Code:
Copy      Help
DateTime t.FromUnix(0)
t.UtcToLocal
out t.ToStr
t.UtcFromLocal
out t.ToStr
#6
Wow - works perfectly. QM never fails to amaze me!
Thanks so much,
S


Forum Jump:


Users browsing this thread: 1 Guest(s)