Skip to content

Instantly share code, notes, and snippets.

@grandadmiral-thrawn
Created August 21, 2014 05:12
Show Gist options
  • Save grandadmiral-thrawn/916667f0b2ddd6e503f2 to your computer and use it in GitHub Desktop.
Save grandadmiral-thrawn/916667f0b2ddd6e503f2 to your computer and use it in GitHub Desktop.
julia_datenum.jl

JULIA_DATENUM

felt annoyed at Julia for not having exactly the same syntax for datenum/datevec/datestr as MATLAB? Me too.

If you have a date in a traditional format of "yyyy-mm-dd HH:MM:SS" julia_datenum will do the annoyance of converting the UTF8 characters into INT64 that can go in the DateTime format. It will also do the 1 day correction on the rata time, and the stupid math of getting the hours, minutes, and seconds all lined up.

it's not much, but it helps

####### Parse by hand dates in the common format
# years
function julia_years(String)
int(String[1:4])
end
# months
function julia_months(String)
int(String[6:7])
end
# days
function julia_days(String)
int(String[9:10])
end
# hours
function julia_hours(String)
int(String[12:13])
end
# minutes
function julia_minutes(String)
int(String[15:16])
end
# seconds
function julia_seconds(String)
int(String[18:19])
end
# datevec
function julia_datevec(String)
{int(String[1:4]),int(String[6:7]),int(String[9:10]),int(String[12:13]),int(String[15:16]), int(String[18:19])}
end
# date num 2 day resolution
function julia_datenum_round(String)
datetime2rata(DateTime(int(String[1:4]),int(String[6:7]),int(String[9:10])+1,int(String[12:13]),int(String[15:16]), int(String[18:19])))
end
# date num full thing
function julia_datenum(String)
datetime2rata(DateTime(int(String[1:4]),int(String[6:7]),int(String[9:10]))) + int(String[12:13])/24 + int(String[15:16])/(24*60)+ int(String[18:19])/(24*3600)
end
@wizebt
Copy link

wizebt commented Dec 25, 2019

Thanks very much for these utility functions. Curs 481417rently running on Julia 1.3.0 gives
ERROR: LoadError: syntax: { } vector syntax is discontinued
easy to fix ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment