VERSION using Datetime n=now() t=time() #current time, unix epoch dump(t) t2 = ccall(:clock_now, Float64, ()) #Seconds since unix epoch t-t2 tm=TmStruct(time()) # tm2 = TmStruct(23,32,10,5,10,113,2,308,0) strftime("%A",time()) #correct help(TmStruct) dump(tm) tns=time_ns() #high precision time strftime(t) methods(Base.strptime) Base.strptime("%Y/%m/%d","2013/11/4") TmStruct(time()) help(ByteString) isa("2013/11/4",ByteString) Datetime.today() Datetime.now() Datetime.calendar(Datetime.today()) Datetime.calendar(now()) Datetime.timezone(now()) Datetime.offset(UTC) Datetime.today() + Datetime.days(4) Datetime.today() - Datetime.weeks(1) using ICU ICU.getDefaultTimeZone() ICU.getNow() #epoch ICU.ICUCalendar() using Calendar t0=Calendar.now() t = ymd_hms(2013, 3, 10, 1, 59, 59) typeof(t) t + days(2) delta =t0-t dump(delta) s = Calendar.format("yyyy-MMMM-dd EEE HH:mm:ss x", t) t2 = Calendar.parse("yyyy-MMMM-dd EEE HH:mm:ss V", s) timezone(t2, "UTC") d = Datetime.today() tmd = TmStruct(0,0,0,Datetime.day(d), Datetime.month(d)-1,Datetime.year(d)-1900, Datetime.dayofweek(d), Datetime.dayofyear(d),0) #correct strftime("Today is Day %w of the week (a %A). Day %d of the month (%B)",tmd) strftime("Day %j of the year(%Y), in week %W of the year.",tmd) function Date2TmStruct(d::Datetime.Date) tmd = TmStruct(0,0,0,Datetime.day(d), Datetime.month(d)-1,Datetime.year(d)-1900, Datetime.dayofweek(d),Datetime.dayofyear(d),0) return tmd end tmd=Date2TmStruct(d) dd = date(tmd.year+1900,tmd.month+1,tmd.mday) function TmStruct2Date(tmd::TmStruct) return Datetime.date(tmd.year+1900,tmd.month+1,tmd.mday) end TmStruct2Date(tmd) import Base.strftime function strftime(fmt::String, d::Datetime.Date) """strftime date formatting using Datetime.Date""" tmd = Date2TmStruct(d) return strftime(fmt, tmd) end strftime("%A",Datetime.today()) strftime("%Y-%m-%d", now()) dt=now() #Year field of 113 rather than 2013 reflects base of 1900. #Month field of 10 rather than 11 reflect zero-based month numbering. tm = TmStruct(23,32,10,5,10,113,2,308,0) dump(tm) Datetime.second(dt), Datetime.minute(dt), Datetime.hour(dt), Datetime.day(dt), Datetime.month(dt), Datetime.year(dt), Datetime.dayofweek(dt), Datetime.dayofyear(dt) tmdt = TmStruct(Datetime.second(dt), Datetime.minute(dt), Datetime.hour(dt), Datetime.day(dt), Datetime.month(dt)-1,Datetime.year(dt)-1900, Datetime.dayofweek(dt)-1,Datetime.dayofyear(dt),0) ?TmStruct function TmStruct2Datetime(tmd::TmStruct) return datetime(int64(tmd.year+1900),int64(tmd.month+1),int64(tmd.mday), int64(tmd.hour), int64(tmd.min), int64(tmd.sec) ) end tmd =TmStruct(time()) TmStruct2Datetime(tm) #strftime("%Y-%m-%d %h:%M:%s",time()) strftime("%Y %H:%M:%S",time()) function DateTime2TmStruct(d::Datetime.DateTime) tmdt = TmStruct(Datetime.second(dt), Datetime.minute(dt), Datetime.hour(dt), Datetime.day(dt), Datetime.month(dt)-1,Datetime.year(dt)-1900, Datetime.dayofweek(dt),Datetime.dayofyear(dt),0) return tmdt end function strftime(fmt::String, dt::Datetime.DateTime) """strftime date formatting using Datetime.DateTime""" tmdt = DateTime2TmStruct(dt) return strftime(fmt, tmdt) end strftime("%A",dt) using Datetime dayofyear(today()) println("Today is day $(dayofyear(today())) of the current year.") println("Today is day $(dayofyear(today())) of $(year(today())).") println("The date is $(today()).") now() epoch_dt = datetime(1970,1,1,0,0,0) now() - epoch_dt function datetime2unix(dt::DateTime) #returns difference in seconds, to match Base.time() #unix2datetime{T<:Offsets}(x::Int64,tz::Type{T}) = convert(DateTime{CALENDAR,tz},UNIXEPOCH + x + leaps(UNIXEPOCH + x)) epoch_dt = datetime(1970,1,1,0,0,0) diff_secs = (dt - epoch_dt - Datetime.leaps(dt))/1000 return diff_secs end dt=now() ep=datetime2unix(dt) (time() - datetime2unix(now())) #seconds difference (time() - datetime2unix(now(UTC))) (time() - datetime2unix(now(EDT))) println("Epoch seconds: $(time())") t= time() #correct utc unix2datetime( int64(t*1000) , UTC) #correct local unix2datetime( int64(t*1000) , EDT) function time2datetime(t::Float64, timezone) """time() style epoch to Datetime in specified timezone.""" return unix2datetime( int64(t*1000) , timezone) end time2datetime_utc(t::Float64) = time2datetime(t::Float64, UTC) time2datetime_est(t::Float64) = time2datetime(t::Float64, EDT) # correct time2datetime_est(t) mydate = date(2013,1,2) println("One day in the future is $(mydate + day(1))") println("Two weeks in the past is $(mydate + week(-2))") dt2 = date(2013,2,14) dt2 - mydate birthtime = datetime(1973,1,18,3,45,50) then = birthtime + seconds(5)+minutes(17)+hours(2)+days(55) println("Then is $then") when = date(1973,1,18) + days(55) println("Nat was 55 days old on $when") diff = dt2 - mydate dump(diff) dt1 = now() dt2 = now() diff_dt = dt2-dt1 dump(diff_dt) bree = datetime(1981,6,16,4,35,25) nat = datetime(1973,1,18,3,45,50) dseconds = (bree-nat)/1000 float_days = dseconds/(60*60*24) ddays = int(float_days) #convert from milliseconds to days println("There were $(int(ddays)) days between Nat and Bree") dweeks = div(ddays,7) xdays = ddays % 7 day_fraction = float_days - ddays xhours = int(floor(24*day_fraction,0)) dminutes = int(dseconds/60) day_seconds = dseconds-(ddays*24*60*60) xhours= int(div(day_seconds, 60*60)) xminutes = int(div(day_seconds, 60)) xseconds = int(day_seconds % 60) #check the sum xseconds + 60*xminutes + 60*60*xhours println("$weeks weeks, $xdays days, $xhours:$xminutes:$xseconds") bree = ymd_hms(1981,6,16,4,35,25) nat = ymd_hms(1973,1,18,3,45,50) delta=bree-nat dump(delta) when = Datetime.date(1981,6,16) Datetime.dayofweek(when) # = Tuesday Datetime.year(when), Datetime.month(when), Datetime.day(when) strftime("%A",Datetime.today()) strftime(when) #this is correct strftime("Today is Day %w of the week (a %A). Day %d of the month (%B)",Datetime.today()) strftime("1981-06-16 was Day %w of the week (a %A). Day %d of the month (%B)",when) strftime("Day %j of the year(%Y), in week %W of the year.",when) # nope datetime("Tue Jun 16 20:18:03 1981") Calendar.parse("EEE MMM DD HH:mm:ss yyyy", "Tue Jun 16 20:18:03 1981") #time.strptime("16/6/1981", "%d/%m/%Y") Calendar.parse("DD/M/yyyy","16/6/1981") dt=Datetime.now() strftime("The date is %A (%a) %d/%m/%Y",dt) cdt = Calendar.now() Calendar.format("The date is yyyy",cdt) """The date is $(Calendar.format(" EEEE (EEE) d/MM/yyyy",cdt))""" Calendar.dayofweek(cdt) tic() elapsed=toc() # from https://github.com/JuliaLang/julia/issues/4478 function f1(n) sum = 0.0 g1(k) = 1.0/k for i = 1:n sum += g1(i) end sum end f1(99) @time f1(1e6) sleep(3.1) #sleep for 4 seconds @time( sleep(3.1) ) ;ipython nbconvert 3_pleac_datetime.ipynb