Convert Integer to Timestamp in Teradata - teradata

I have two cloumns one of them is date(it is date format 2020-06-17), another one is time of transaction (it is integer 1258) how I can concat them as Timestamp?
Example
Date Time Expected_Result
2020-06-17 1258 2020-06-17 12:58:00
2020-08-15 2315 2020-08-15 23:15:00
2020-10-10 1238 2020-10-10 12:38:00

Multiple approaches are possible. One would be convert everything to string first:
cast(cast(cast(d as format 'yyyy-mm-dd') as varchar(11))||' '||cast(cast(t as format '99:99') as varchar(10))||':00' as timestamp(0))
Another possibility is to split up hours and minutes using integer arithmetic and convert units individually:
cast(d as timestamp(0))+ (t/100)*interval '1' hour + (t mod 100)*interval '1' minute

Related

How to combine 12-hour time sheet and AM/PM column from spreadsheet in r

I have a spreadsheet that has the date and 12 hour time in one column and then another column that specifies AM/PM. How do I combine these files so I can use them as a POSIXct/POSIXlt/POSIXt object?
The spreadsheet has the time column as
DAY/MONTH/YEAR HOUR:MINUTE
while hour is in a 12-hour format from a roster of check in times. The other column just says AM or PM. I am trying to combine these columns and then convert them to 24 hour time and use it as a POSIXt object.
Example of what I see:
Timesheet
AM-PM
8/10/2022 9:00
AM
8/10/2022 9:01
AM
And this continues until 5:00 PM (same day)
What I have tried so far:
Timesheet %>%
unite("timestamp_24", c("timestamp_12","am_pm"),na.rm=FALSE)%>%
mutate(timestamp=(as.POSIXct(timestamp, format = "%d-%m-%Y %H:%M"))
This does not work as when they are combined it gives:
Timestamp_24
DAY/MONTH/YEAR HOUR:MINUTE_AM
and I think this is the crux of the issue because then as.POSIXct can't read it.
Here's my solution. The approach is simply to extract the hour, +12 if it is PM, then format correctly with as.POSXct (you need to use / rather than - in the format argument if the your dataframe is at is appears in your example).
I've done that with stringr::str_replace() which allows you to set a function for the replace argument.
Timesheet %>%
mutate(
time_24hr = stringr::str_replace(
time,
"\\d+(?=:..$)",
function(x) {
hr <- as.numeric(x) %% 12
ifelse(am_pm == "PM", hr + 12, hr)
}
),
time_24hr = as.POSIXct(time_24hr, format = "%d/%m/%Y %H:%M")
)
This is the result:
time am_pm time_24hr
1 8/10/2022 9:00 AM 2022-10-08 09:00:00
2 8/10/2022 9:01 PM 2022-10-08 21:01:00
3 8/10/2022 12:01 PM 2022-10-08 12:01:00
4 8/10/2022 12:01 AM 2022-10-08 00:01:00
EDIT. realized that this didn't work for 11 and 12 as the regex was only extracting the first character before :. Also wasn't working for 12:xx times. Fixed both. Added test cases to show that these work now.

Dates out by 2 days when I convert to Date Format in R

When I am converting dates from characters to "dates" it seems to be off by 2 days from excel?
My example
mydata <- c(38808,40422,40493,40606)
as.Date(mydata, origin="1900-01-01")
# [1] "2006-04-03" "2010-09-03" "2010-11-13" "2011-03-06"
yet in excel the dates are as follows
Date in Excel in R Delta
38808 2006-04-01 2006-04-03 2
40422 2010-09-01 2010-09-03 2
40493 2010-11-11 2010-11-13 2
40606 2011-03-04 2011-03-06 2
I get around it by changing origin date to 1899-12-30 but I am sure I am doing something wrong.
Thanks
It is a known problem that Excel thinks 1900 was a leap year, but it was not. So Excel counts an extra day (for nonexistent Feb 29, 1900). In addition, Excel considers "1900-01-01" as day 1, not day 0.
Maybe the link will help:
http://www.cpearson.com/excel/datetime.htm
For excel dates you need this one:
mydata <- c(38808,40422,40493,40606)
as.Date(mydata, origin = "1899-12-30")
[1] "2006-04-01" "2010-09-01" "2010-11-11" "2011-03-04"

standardize timezone in a large dataset

I have a large dataset corresponding to different sites and timezone.
I'd like to standardize all of the sites to "UCT". I'm struggling to transform the dates (which are a factor) to get the proper date format.
One small sample of my date looks like this:
head(data_tz)
site DatetimeEnd tzone
FR01001 2014-10-28 00:00:00 UTC
FR01001 2014-11-02 00:00:00 UTC
FR01001 2014-01-20 00:00:00 UTC
FR01001 2014-11-01 00:00:00 UTC
FR01001 2014-01-13 00:00:00 UTC
FR01001 2014-09-17 00:00:00 UTC
..........
This is a large dataset with 4 different tzone:
unique(data_tz$tzone)
"UTC" "UTC-04" "UTC+04" "UTC-03"
And DatetimeEnd is a factor, which I need to convert to POSIXct, and then each site to "UTC". I have been trying different approaches but neither of them worked.
I am using:
newdata$DatetimeEnd <- as.POSIXct(data_tz$DatetimeEnd, format="%Y-%m-%d %H:%M:%S",tz=data_tz$tzone)
But I got:
Error in strptime(x, format, tz = tz) : invalid 'tz' value
And the same when using:
newdata$DatetimeEnd <- as.POSIXct(strptime(data_tz$DatetimeEnd,
format="%Y-%m-%d %H:%M:%S",tz=data_tz$tzone))
If I use:
newdata$DatetimeEnd <- as.POSIXct(data_tz$DatetimeEnd, format="%Y-%m-%d %H:%M:%S",tz="UTC +01")
It works, but it is not what I want, since there are some columns (sites) with "UCT +02"..(different tzone)
How can I use here the tz as an argument to get the right timezone? Any idea/suggestion would be really helpful.
Thanks
You can use purrr::map2 to iterate over the rows of the columns DatetimeEnd and tzone, creating a new vector.

How to convert a numeric number in R to timestamp in R? For example, from 62280 to 17:18:00

I have a RData file, one column in which is time. However, when I load it in R, it is shown in numbers.
For example, 17:18:00 is shown as 62280 ,
06:30:00 is shown as 23400 ,
18:30:00 is shown as 66600 ,
10:30:00 is shown as 37800 ,
01:00:00 is shown as 3600 .
I figure out number divided by 3600 is equal to time, as 23400/3600 = 6.5.
But I still stuck how to do in the next step. Could anyone give a clue? Also, is this "divided by 3600" is a standard time conversion in R? If so, is there a function for this conversion?
Thank you!
1) Using the chron package this gives a "times" class object or if you prefer character strings use format(times(...)).
library(chron)
x <- c(62280, 23400, 66600, 37800, 3600)
times(x / (24 * 60 * 60))
## [1] 17:18:00 06:30:00 18:30:00 10:30:00 01:00:00
2) Using POSIXct class and no packages we can get character strings like this:
format(as.POSIXct(x, origin = "1970-01-01", tz = "UTC"), "%H:%M:%S")
## [1] "17:18:00" "06:30:00" "18:30:00" "10:30:00" "01:00:00"

Financial timestamp to date object

I have a chr variable in formats:
20160101 000000
20160101 235600
which I want to convert to:
2016-01-01 00:00:00
2016-01-01 23:56:00
I can convert the date easily with:
as.Date('20160101 000000', format="%Y%m%d")
but that only converts the date. So I wonder if there is a function that can do both.
Cheers
Update:
Per suggestion of David Arenburg I use:
as.POSIXct('20160101 000000', format='%Y%m%d %H%M%S')
and this solves my problem.

Resources