Convert Unix Epoch Time to MSZ date and time - unix

Does anyone of you have suggestions how to convert the Unix timestamp to ABAP MEZ/MESZ time and date?
The following code is from the ABAP-Reference, the code is for timestamps with lenght 15 or 21 but the Unix timestamp is currently 10 digits long.
DATA: time_stamp TYPE timestamp,
tz TYPE ttzz-tzone.
tz = 'MESZ'.
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE DATA(dat) TIME DATA(tim)
DAYLIGHT SAVING TIME DATA(dst).
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE dat TIME tim
DAYLIGHT SAVING TIME dst.
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
cl_demo_output=>display( ).

I solved the problem with this code. I am using now 13 digits epoche...
data: i(20) type n.
data: d type sy-datum."
data: t type sy-uzeit.
data: epoche type int8.
data: test type int8.
data: test2 type int8.
epoche = 1522836000000.
i = epoche / 1000.
d = '19700101'.
d = d + i div 86400.
t = i mod 86400.
write: d, t.

If you want to convert using a SAP time zone, this one can work too (no time to convert it to ABAP Objects):
FORM unixtime_2_date_time
USING
i_unixtime TYPE numeric
i_timezone TYPE timezone
CHANGING
e_date TYPE d
e_time TYPE t.
DATA l_tstmp_unix_era TYPE TZNTSTMPL.
DATA l_tstmp TYPE TZNTSTMPL.
CONSTANTS utc TYPE timezone value IS INITIAL.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP l_tstmp_unix_era TIME ZONE utc.
l_tstmp = CL_ABAP_TSTMP=>add( tstmp = l_tstmp_unix_era secs = i_unixtime ).
CONVERT TIME STAMP l_tstmp TIME ZONE i_TIMEZONE INTO DATE e_date TIME e_time .
ENDFORM.

You can use cl_pco_utility Java utility class for that, particularly method convert_java_timestamp_to_abap. It perfectly accepts 10-character Unix time.
CLASS zcl_epoch DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
unix_time_to_timestamp
IMPORTING iv_timestamp TYPE timestamp.
ENDCLASS.
CLASS zcl_epoch IMPLEMENTATION.
METHOD unix_time_to_timestamp.
CONSTANTS: c_tzone TYPE ttzz-tzone VALUE 'CET'.
DATA: lv_timestamp_msec TYPE string,
lv_timestamp TYPE timestamp,
lv_date TYPE datum,
lv_time TYPE uzeit.
lv_timestamp_msec = iv_timestamp * 1000.
cl_pco_utility=>convert_java_timestamp_to_abap(
EXPORTING
iv_timestamp = lv_timestamp_msec
IMPORTING
ev_date = lv_date
ev_time = lv_time
).
lv_timestamp = lv_date && lv_time.
CONVERT TIME STAMP lv_timestamp TIME ZONE c_tzone INTO DATE lv_date TIME lv_time.
cl_demo_output=>display( |{ lv_date DATE = ISO } { lv_time TIME = ISO }| ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
zcl_epoch=>unix_time_to_timestamp( '1532196799' ).
It worth mentioning that MEZ/MSEZ are NOT standard abbreviations and should not be used. For example, MEZ is the same as CET. Look at list of common time zones here or here.
Also, it's better to stick to list of available time zones of your system, which is table TTZZ.

Just add the epoch time to '19700101' date with initial time.
DATA lv_initial_timestamp TYPE timestamp.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP lv_initial_timestamp TIME ZONE sy-zonlo.
ev_timestamp = cl_abap_tstmp=>add( tstmp = lv_initial_timestamp secs = iv_epoch_timestamp ).
CONVERT TIME STAMP ev_timestamp TIME ZONE sy-zonlo INTO DATE ev_date TIME ev_time.
For MEZ/MESZ you can choose time zone accordingly.

Related

Subtract number of hours from current time in Java

I have a string that represents time duration of 54:34:41 i.e. 54 hours, 34 minutes, 41 seconds.
I would like to extract the 54 hours and subtract it from the current system time.
However when I run below I get java.time.format.DateTimeParseException: Text '54:34:41' could not be parsed: Invalid value for HourOfDay (valid values 0 - 23): 54
How can I extract 54 hours and subtract from current time?
private val formatterForTime: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
val timeDuration = formatterForTime.parse("54:34:41")
val currentTime = LocalDateTime.now()
val newTime = currentTime.minusHours(timeDuration.get(ChronoField.HOUR_OF_DAY).toLong())
tl;dr
ZonedDateTime
.now(
ZoneId.of( "Asia/Tokyo" )
)
.minusHours(
Integer.parseInt( "54:34:41".split( ":" )[0] )
)
Details
Parse hours
Get the number of hours.
int hours = Integer.parseInt( "54:34:41".split( ":" )[0] ) ;
ISO 8601
Your input text for a span-of-time does not comply with the ISO 8601 standard for date-time values. The java.time classes by default use the standard formats when parsing/generating text.
If instead of 54:34:41 you had PT54H34M41S, then we could use:
int hours = Duration.parse( "PT54H34M41S" ).toHours() ;
I recommend you stick with the standard format rather than the ambiguous clock-time format.
Capture current moment
Capture the current moment as seen in a particular time zone.
ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Subtract hours
Subtract your hours.
ZonedDateTime earlier = zdt.minusHours( hours ) )

NIFI Executescript UTC Error for unsupported operand type "+" in java.sql.Timestamp and timedelta

I am trying to split a flowfile into multiple flow files on the basis of adding a month to a date which i am getting in the coming flowfile.
eg.
{"to":"2019-12-31T00:00:00Z","from":"2019-03-19T15:36:48Z"}
be the dates i am getting in a flowfile . so i have to split this single flow file into 11 flowfiles with date ranges like
{"to":"2019-04-19","from":"2019-03-19"}
{"to":"2019-05-19","from":"2019-04-19"}
{"to":"2019-06-19","from":"2019-05-19"}
....... and so till
{"to":"2019-12-31","from":"2019-12-19"} .
i have been trying with example inputs to split files with this into day wise flowfiles:
`
begin = '2018-02-15'
end = '2018-04-23'
dt_start = datetime.strptime(begin, '%Y-%m-%d')
dt_end = datetime.strptime(end, '%Y-%m-%d')
one_day = timedelta(days = 1)
start_dates = [dt_start]
end_dates = []
today = dt_start
while today <= dt_end:
tomorrow = today + one_day
print(tomorrow)
`
but i get a error in my Execute script processor. nifi flow screenshot
Since you're using Jython, you may have to cast today to some Jython/Python time variable or call today.getTime() in order to do arithmetic operations on it.

SAS date formatting hours / minutes / seconds

Because my time data has some messy characters in it ( *, #, char, etc) I'm inputting the data in best.32 format and then using compress to remove the irrelevant char - time_1 = compress(Tim_original,'*#','l');
However, my data takes the form of mm:ss and hh:mm:ss and for some reason when I use time_1=input(time_1,time8.) to convert from the string to a num, it makes my mm into hours...! How do I covert my string to time/minutes and not have the minutes turned into hours with ":00" added at the end?
If your text only has one : then the informat TIME will take that to mean hh:mm and not mm:ss. You could just test your string and divide the result of the INPUT() function call by 60 to convert it.
data test;
input #1 timestr $8. ;
time1=input(timestr,time8.);
time2=input(timestr,time8.);
if countw(timestr,':') < 3 then time2=time2/60 ;
format time1 time2 time8.;
cards;
12:34
0:12:34
;

How to set timezone in date field using jodatime library

I am using JodaTime2 library to create a date object with a given timezone as follow:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
DateTimeZone tz = DateTimeZone.forID("America/New_York");
System.out.println("timezone=" + tz);
Date d = new DateTime(2013, 1, 1, 0, 0, tz).toDate();
System.out.println("Cur Date = " + d);
However when I print this date, the timezone reported is CST. What am I missing ?
timezone=America/New_York
Cur Date = Tue Jan 01 13:00:00 CST 2013
You're printing out the value of a Date object. Date doesn't have a time zone - Date.toString() always just uses the "default" time zone. A Date is just a number of milliseconds since the Unix epoch; it doesn't know about calendars or time zones.
You should either just stick within the Joda Time world, or (if you must) use a SimpleDateFormatter to convert a Date to a String - you can set the time zone on the formatter.

How to convert string to datetime format classic asp

I have one variable
Dim tt="2008-10-20 10:00:00.0000000"
I want to change it into date,
Try CDATE(tt) see http://www.w3schools.com/vbscript/func_cdate.asp. I used
vbscript cdate
as keywords at Google. There were more results.
Edit: Based on the comment below (I'm sorry for mixing up), using
FormatDateTime(date,format)
Format contains following constants:
0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if
specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
(copied from http://www.w3schools.com/vbscript/func_formatdatetime.asp)
This link, (MS CDate page), explains that:
adate = CDate(astring)
converts a string into a date object. For there, you can format it with the FormatDateTime function
str = FormatDateTime(Date)
the FormatDateTime function is "smart" -- it will format as date and time if both are present, otherwise it will format with whichever of date or time is present.
I propose a safe solution which returns the result only if the conversion is successful:
s="2008-10-20 10:00:00.0000000"
On Error Resume Next
d=CDate(Left(s,19))
On Error Goto 0
if not IsEmpty(d) then MsgBox d
Try it for a non-valid date or non-valid format. The result will be empty.
s="2008-02-31 10:00:00"
In same contexts, it is necessary to initialize the variable collecting result of CData. I recommend to initialize it as Empty. Example below shows such case - counting valid dates in a string array:
Lines = array("2008-10-20 10:00:00.0000000", "2008-10-20 10:00:00", "", "2008-02-31", "Today", "2017-02-7")
On Error Resume Next
Count=0
for each Line in Lines
d=Empty
d=CDate(Line)
if not IsEmpty(d) then Count=Count+1
next
On Error Goto 0
MsgBox "Number of valid dates is "&Count
The correct answer is 2. Without initialization we get 5 as the CDate does not do anything on error so variable keeps the value from a recent iteration in the loop.
If do not need your milliseconds, your could use the following:
<script type="text/vbscript">
s="2008-10-20 10:00:00.0000000"
arr= Split(s, ".")
d=CDate(arr(0))
document.write(d)
</script>
I believe cdate is dependent on local settings to parse the string. This is no good in many situations.
To avoid this you need to use
DateSerial()
and if needed add any time components to the result separately.
The date literal in classic asp is unreliable. If the first or second part is greater than 12, it takes that value for day, the other as month. If both parts are less than 12, the interpretation is unpredictable: sometimes american and sometimes british.
A work-around is to force the entry of dates into separate fields or use a date entry module which can set the date into british or american style.
A date literal should be treated as american and use a function to convert that into a date variable using Dateserial().
function amerdate(str)
'str 3/5/2023 form: in american format: use for calculations including date
Dim d
d = Split(str, "/")
amerdate = Dateserial(d(2), d(0), d(1))
end function
Use only american date in calculations like Dateadd() etc.
someday = "3/5/2023" '5th march, american date literal
nextday = Dateadd("d", 1, amerdate(someday))
Whenever a date display is required, convert to british date and show it.
function britdate(str)
'str: 3/5/2023 form: display in british form. not for calculations
Dim d
d= Split(str, "/")
britdate = d(1) & "/" & d(0) & "/" & d(2)
end function

Resources