I have to format a DateTime which is in UTC based on user settings which include its time zone (CET/IST etc) and time format (24hr/12hr).
I could find CONVERT TIME STAMP statement which takes only TIME ZONE as a parameter and does that conversion:
DATA: lv_timestampl TYPE timestampl VALUE '20200219095959.0000000',
lv_date TYPE d,
lv_time TYPE t.
CONVERT TIME STAMP lv_timestampl TIME ZONE sy-zonlo INTO DATE lv_date TIME lv_time.
My objective is to convert this lv_timestampl based on TimeZone and TimeFormat together.
PS: I could just do that -12 manipulation on lv_time after CONVERT TIME STAMP statement and append PM/AM but I am looking for a standard way of doing it.
OK, let me be your living help today, if you were not able to find string templates help.
Template to convert timestamp to UTC:
DATA(ld_tims_utc) = |{ lv_timestampl TIMESTAMP = ENVIRONMENT }|.
returns
19.02.2020 09:59:59,0000000
Template to convert timestamp to explicitly specified timezone:
DATA(ld_tims_zone) = |{ lv_timestampl TIMESTAMP = ENVIRONMENT TIMEZONE = 'CET' }|.
returns
19.02.2020 10:59:59,0000000
Getting time and date from timestamp (not timestampl, so conversion needed):
cl_abap_tstmp=>systemtstmp_utc2syst( EXPORTING
utc_tstmp = CONV timestamp( lv_timestampl )
IMPORTING
syst_date = lv_date
syst_time = lv_time ).
Output in 12h format:
SET COUNTRY 'US'.
DATA(time_us) = |{ lv_time TIME = ENVIRONMENT }|. "gives 01:55:43 PM
Output in 24h format:
SET COUNTRY 'DE'.
DATA(time_de) = |{ lv_time TIME = ENVIRONMENT }|. "gives 13:55:43
For the output of AM/PM time formats, they need to be maintained in field TIMEFM of table USR01 aka User settings or table t005x aka Country settings (tcode OY01).
FM HRVE_CONVERT_TIME can be used to convert in both ways (Convert 12hr-to 24h and 24hr to 12hr).
You can combine the results with the FM you already found.
Related
I need to convert 2021-10-03 15:10:00.0 as 2021-10-03T15:10:00-04:00
I tried with.
from_utc_timestamp(from_unixtime(unix_timestamp('2021-10-03 15:10:00.0', "yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd'T'HH:mm:ssXXX"),"America/New_York")
I got Null value
Any suggestions please
from_utc_timestamp can accept timestamp or compatible string (yyyy-MM-dd HH:mm:ss.S), or bigint, not this: "yyyy-MM-dd'T'HH:mm:ssXXX"
Hive timestamps are timezoneless. Once you converted from UTC to America/NY, the timezone information is lost, only you know in which timezone it is, having timestamp converted it is already impossible to derive the timezone from it.
You can concatenate with timezone, conversion like this returns what you need but it works for particular date only. In December -05:00 timezone should be usedm not +04:00:
date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss+04:00") --This is wrong!!!
From_utc_timestamp is Daylight saving aware. It can be -05:00 or -04:00 depending on the date.
Consider this example, first returns 5, second returns 4:
select (unix_timestamp("2020-01-01 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-01-01 12:00:00.0","America/New_York")))/60/60
select (unix_timestamp("2020-10-19 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-10-19 12:00:00.0","America/New_York")))/60/60
So, you can get current time zone corresponding to America/New_York for the same timestamp and concatenate it with converted timestamp:
select concat(date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss"),'+0',
--get hrs shift
(unix_timestamp("2021-10-03 15:10:00.0")-unix_timestamp(from_utc_timestamp("2021-10-03 15:10:00.0","America/New_York"))) div 3600,':00')
Result:
2021-10-03T11:10:00+04:00
It should work correctly with different timestamps taking into account daylight saving time for America/New_York.
I'm new to Big Query,
I want to convert the following date-time to UTC timezone.
DateTime format "2021-07-03T23:59:00+04:00[Asia/Dubai]" to UTC.
Thank you.
You just need to convert it to TIMESTAMP() without specifying any timezone (since it automatically sets the datetime at UTC) and then convert it back to DATETIME():
SELECT
your_datetime,
DATETIME(TIMESTAMP(your_datetime)) AS datetime_utc
FROM
<your_table>
OUTPUT:
your_datetime datetime_utc
------------------------- -------------------
2021-07-03T23:59:00+04:00 2021-07-03T19:59:00
I have been able to convert a TIMESTAMP field that is in UTC to CST using:
SELECT
TIMESTAMP(started_at) AS UTC,
TIMESTAMP_SUB(TIMESTAMP (started_at), INTERVAL 5 HOUR) AS CST
This returns:
ROW
UTC
CST
1
2020-05-17 13:07:22 UTC
2020-05-17 08:07:22 UTC
The second TIMESTAMP displays the correct date and time but UTC still shows. What is the simplest way to replace 'UTC' with 'CST' or, alternatively, remove 'UTC' altogether since I don't need the designation in the field itself?
You can use a plain sql replace function inline
select replace(TIMESTAMP_SUB(TIMESTAMP (started_at), INTERVAL 5 HOUR), 'UTC','') AS CST
The above searches the result of your expression (provided in your question) for 'UTC' and replaces it with nothing ''. You could also replace it with 'CST' as you noted.
Obligatory warning: I believe your query will only be correct half the year as long as DST is observed? You might want to look into sys.time_zone_info and its reference on the MSDN.
My advise is to convert the timestamp to a datetime local value:
select datetime(started_at, 'America/Chicago') as started_at_cst
Note that the time zone is not stored in the data value. Instead, this encodes the value in the string.
If you want to include the time zone value, I have found that the best approach is to use a string (argghh!). The following constructs a string:
format_timestamp('%F %X%z', started_at, 'America/Chicago') as started_at_str
which can be converted easily into a timestamp for date/time calculations:
timestamp(started_at_str)
When formatting my time i get different results depending on the use of hh or HH.
Can someone tell me why this happens?
The problem occurs with the following code, returning a NULL on the 2nd result.
declare #TIMEPART time = getdate()
declare #datetime datetime2(7) = getdate()
select format(#timepart, 'hh\:mm'), format(#timepart, 'HH\:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
The HH:mm format is not a valid conversion for the time datatype and for the format(time, time format) function it is not the correct syntax. Because of this the format function returns a null. HH:mm is the conversion type for 24 hour clock for datetime datatypes but is not compatible with the time datatype as the time datatype is already in the 24 hour clock format(not sure why this is the case but I think it is because the time datatype can do more than just represent 24h clock time). The AM\PM format type hh:mm is only valid with the datetime datatypes and for time it will just return the 24 hour time since this is all a time datatype can return. You would have to convert the time to a datetime if the input value datatype is time in order to display the AM/PM format as I have shown below.
declare #TIMEPART time = '21:20:20.0570000'
declare #datetime datetime2(7) = '21:20:20.0570000'
select format(#timepart, 'hh\:mm'), format(convert(datetime,#timepart), 'hh:mm'), format(#datetime, 'hh\:mm'), format(#datetime, 'HH\:mm')
If you still want the data type of the second column to be time then you would have to convert back to time
cast(format(convert(datetime,#timepart), 'hh:mm') as time)
However in this example the time would be 9:20 AM and not 9:20 PM since time is only stored as a 0 to 23 value for the hour. This is a good example of why is is advisable to always try to return any time value in the 0-23 hour format. If you do need to display the time in the 12 hour format you are better off converting it to a varchar.
convert(varchar(15), format(convert(datetime,#timepart), 'hh:mm'))
One other thing to note is that for datetimes the \ in the format is not needed, for example
format(#datetime, 'hh\:mm')
and
format(#datetime, 'hh:mm')
will return the same value. However the \ is needed for time datatype functions.
format(#timepart, 'hh\:mm')
will return a hh:mm time but
format(#timepart, 'hh:mm')
will return null
I have one column , data as 'Apr 06 2016 05:30:30' it is not in the time stamp formate, when using this one as timestamp I am getting null values. So stored as string, now I want to do some calculation on this when it is in time stamp formate. for that i converted into unixtimestamp and getting back to timestamp formate but the value of the date is changed. I used conversions as 'select from_unixtime(unix_timestamp(start_time, 'MMM DD YYYY HH:mm:ss')) from temp;'
I got value as '2015-12-27 05:30:30'.
I want final data as '2016-04-06 05:30:30'.
Please help me on this
You have just written the wrong format. The proper format string is 'MMM dd yyyy HH:mm:ss'. Take a look at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for reference for format strings.