How convert decimal to POSIX time
@Arun's strategy works, but it might be easier to just do:
x <- c(6.49055593325792, 18.2873900837081)
today<-as.POSIXct('2012-01-23 00:00:00 EST')
today + (3600*x)
# "2012-01-23 06:29:26 EST" "2012-01-23 18:17:14 EST"
That will get you the seconds as well.
Convert a decimal time representation to unix epoch
My C is a bit rusty, but looking at the other two answers I would write a function as follows, returning the number of seconds since epoch or -1 in case of error.
#include <stdio.h>
#include <time.h>
time_t convertDecimalTime(long dt) {
struct tm time_str;
time_str.tm_isdst = -1;
time_str.tm_sec = dt%100; dt/=100;
time_str.tm_min = dt%100; dt/=100;
time_str.tm_hour = dt%100; dt/=100;
time_str.tm_mday = dt%100; dt/=100;
time_str.tm_mon = dt%100-1; dt/=100;
time_str.tm_year = dt%10000 - 1900;
return mktime(&time_str);
}
Converting combined Day/Time decimal format to POSIX
You can use as.POSIXct() to do so:
as.POSIXct.numeric((df$decimal.day-1)*24*3600,
origin=paste(df$Year, df$month, 1, sep = "/"), tz="GMT")
Note the -1 to balance starting the first day of the month
How to convert decimal time to time format
Another option:
library(lubridate)
video$video_duration <- as.numeric(video_end - video_begin, units = "secs")
video$video_duration <- seconds_to_period(video$video_duration)
video_begin video_end video_duration
1 2017-09-12 00:08:14 2017-09-12 00:39:08 30M 54S
2 2017-09-12 00:04:47 2017-09-12 00:47:10 42M 23S
3 2017-09-12 00:08:27 2017-09-12 00:49:51 41M 24S
4 2017-09-12 00:04:59 2017-09-12 00:44:31 39M 32S
5 2017-09-12 00:04:57 2017-09-12 00:39:41 34M 44S
6 2017-09-12 00:07:51 2017-09-12 00:47:12 39M 21S
7 2017-09-12 00:06:11 2017-09-12 00:40:13 34M 2S
8 2017-09-12 00:05:30 2017-09-12 00:46:52 41M 22S
Convert decimal day to HH:MM
Try this:
R> format(as.POSIXct(Sys.Date() + 0.8541667), "%H:%M", tz="UTC")
[1] "20:30"
R>
We start with a date--which can be any date, so we use today--and add your desired fractional day.
We then convert the Date type into a Datetime object.
Finally, we format the hour and minute part of the Datetime object, ensuring that UTC is used for the timezone.
Converting a day decimal number array to unix timestamp in Python
assuming these numbers are fractional days of the year (UTC) and the year is 2017, in Python you would do
from datetime import datetime, timedelta, timezone
year = datetime(2017,1,1, tzinfo=timezone.utc) # the starting point
doy = [279.341, 279.345, 279.348]
# add days to starting point as timedelta and call timestamp() method:
unix_t = [(year+timedelta(d)).timestamp() for d in doy]
# [1507363862.4, 1507364208.0, 1507364467.2]
Cast decimal number to datetime in Pandas
Here's what ended up working:
posBirch['TimeStamp'] = pd.to_datetime(posBirch['timestep_time'], unit='s')
Related Topics
Deleting Columns from a Data.Frame Where Na Is More Than 15% of the Column Length
Ggplot2 Shade Area Under Density Curve by Group
Specifying Column Names in a Data.Frame Changes Spaces to "."
How to Get Ranks with No Gaps When There Are Ties Among Values
Cumulative Sum Until Maximum Reached, Then Repeat from Zero in the Next Row
R Package Lattice Won't Plot If Run Using Source()
Dplyr/R Cumulative Sum with Reset
Python's Xrange Alternative for R or How to Loop Over Large Dataset Lazilly
How to Create a "Macro" for Regressors in R
Cowplot Made Ggplot2 Theme Disappear/How to See Current Ggplot2 Theme, and Restore the Default
Colorbar from Custom Colorramppalette
Rstudio Not Picking the Encoding I'm Telling It to Use When Reading a File
Getting a Stacked Area Plot in R