EPOCH Time from System Time Using Notetab Clip

I want to do a time comparison between two times. I figured a good way to do that might be to generate a Unix or EPOCH time as is commonly used in Perl, PHP, and many other programming languages. I searched help and help on clip programming and did not find any reference to EPOCH time in either, so I decided I was on my own.

My method is set out in the beginning of the clip in a comment. I did take into account leap years (skipping over the issue that ever other century or so you skip a leap year even though the year is divisible by four…or something like that). I think it is working. I may in my next go let you set a random time. Currently this is outputting most interim data calculated, but I obviously would modify that for a final product.

Here is the NoteTab Pro Clip to generate EPOCH time from the system time. You can check your results using an EPOCH to human time converter.

;*** Effort by Don Passenger
;*** don at htmlfixit dot com
;*** Creates EPOCH Time (seconds since Jan 1, 1970 to current)
;*** from time on computer
;*** copy posted at:
;*** http://htmlfixit.com/blog/?p=356
;*******************************************************
^!InsertText +++++++++++++++++get date off system++++++++++++++++++++++^P
:GrabDates
;get year
^!Set %year%="^$GetDate(yyyy)$"
;get month (no leading zeros)
^!Set %month%="^$GetDate(m)$"
;get days (no leading zeros)
^!Set %day%="^$GetDate(d)$"
;get hours (no leading zeros)
^!Set %hours%=^[h^]
;get minutes (no leading zeros)
^!Set %minutes%=^[n^]
;get seconds (no leading zeros)
^!Set %seconds%=^[ss^]

:FigureEpochTime
;Method Used:
;base year (from Jan 1 70 to Jan 1 2007)
;+ seconds in each year after 2006
;+ seconds in each month in current year
;+ seconds in days up to today
;+ seconds in hours, minutes, seconds of today
;I disregard leap seconds and figure I'll
;be dead before the next non-leap year
;divisible by four

:CalculateSecondsInYearsFrom2006
^!InsertText ++++++++++++++calc seconds in years after 2006++++++++++++++++++^P
;calculate days in years between 12/31/2006
;and current year (whole years only)
^!Set %baseyear%="2006"
^!Set %numberofyears%="^$Calc(^%year%-2006-1)$"
:Loop_years_to_seconds
^!Set %counter%="0"
^!Set %secondsinyears%="0"
;skip years entirely if is currently 2007
^!If ^%numberofyears% = 0 Loop_months_to_seconds

:next_year
;first time thiw becomes 2007 and then it
;counts up to current year
^!Inc %baseyear%
;set days in month for non-leap years
^!SetArray %daysinmonth%=31;28;31;30;31;30;31;31;30;31;30;31
;determine if year being worked on is leap year
;fix Feb days if yes
^!If ^$calc(^%baseyear% mod 4)$<>0 Skip
^!Set %daysinmonth2%=29

:calcsecondsinyears
;abort any time we are out of whole years 2007-
^!If ^%numberofyears% = 0 Loop_months_to_seconds
;in counter so that we can get days in each month of year
^!Inc %counter%
;add to seconds in year the active month in the active year
^!Set %secondsinyears%=^$Calc(^%secondsinyears%+(^%daysinmonth^%counter%%*86400))$
;this is just for testing to show the output after each month
;while calculating the active year
^!InsertText ^%secondsinyears% to ^%baseyear%-^%counter% ^$Calc(^%secondsinyears%+1167609600)$^P

;do the next month in active year unless we are at 12 (ie no more months)
^!If ^%counter% <> 12 calcsecondsinyears
;reduce years to be calculated by one and recycle to calc seconds in next year
^!Dec %numberofyears%
;reset month counter to zero
^!Set %counter%="0"
;goto next year 2008-
^!Goto next_year

:Loop_months_to_seconds
^!InsertText ++++++++++++++++++calc seconds in whole months of current year++++++++++++++++^P
;calculate days in whole months for current year
;set the months to 0 so we can process each month
^!Set %basemonth%="0"
;start the base counter
^!Set %secondsinmonths%="0"
;get us out of here if it is Jan
^!If ^$Calc(^%basemonth%+1)$ = ^%month% Loop_days_to_seconds
;set months array for non-leap years
^!SetArray %daysinmonth%=31;28;31;30;31;30;31;31;30;31;30;31
;if current year is leap year, correct Feb
^!If ^$calc(^%year% mod 4)$<>0 Skip
^!Set %daysinmonth2%=29

:next_month
;start with month 1 (Jsn first time and increas by one each time)
^!Inc %basemonth%
;if it is current month we are done with months
^!If ^%basemonth% = ^%month% Loop_days_to_seconds

:calcsecondsinmonths
;do math to get seconds in current month and add to prior months we calculated
;on previousl iterations of this
^!Set %secondsinmonths%=^$Calc(^%secondsinmonths%+(^%daysinmonth^%basemonth%%*86400))$
;insert text is just for testing so we can see we are where we belong at the end of
;each successive month of calculation
^!InsertText ^%secondsinmonths% to Month-^%basemonth% ^$Calc(^%secondsinmonths%+^%secondsinyears%+1167609600)$^P
;repeat until we hit current month (tested above)
^!Goto next_month

:Loop_days_to_seconds
^!InsertText +++++++++++++++++++++calc seconds for whole days current month+++++++++++++++++^P
;set base variable for calculation of seconds in whole days in current month
^!Set %baseday%="0"
^!Set %secondsindays%="0"

:next_day
;increment baseday to 1 first iteration and increasing on each pass
^!Inc %baseday%
;get us out of here when no more full days left to calc
^!If ^%baseday% = ^%day% Loop_hours_to_seconds

:calcsecondsindays
;do math adding second for active day to prior whole days of seconds in current month
^!Set %secondsindays%=^$Calc(^%secondsindays%+86400)$
;insert text just for testing to see if we are where we belong
^!InsertText Current Month Day-^%baseday% ^$Calc(^%secondsindays%+^%secondsinmonths%+^%secondsinyears%+1167609600)$^P
;repeat until we are out of whole days in current month (tested above)
^!Goto next_day

:Loop_hours_to_seconds
^!InsertText +++++++++++++++++++++++Calc Whole Hours Today to Seconds++++++++++++++++++++++++++++++^P
;calculate seconds in whole hours today
;set the hours to 0 so we can process each one
^!Set %basehour%="0"
;start the base counter
^!Set %secondsinhours%="0"
;get us out of here if it is < Midnight+1hour
^!If ^$Calc(^%basehour%+1)$ = ^%hours% Loop_minutes_to_seconds

:next_hour
;start with hour 1 (12:00:01am first time and increase by one each time)
^!Inc %basehour%
;if it is current month we are done with months
^!If ^%basehour% > ^%hours% Loop_minutes_to_seconds

:calcsecondsinhours
;do math to add 3600 seconds to total for this hour
^!Set %secondsinhours%=^$Calc(^%secondsinhours%+3600)$
;insert text is just for testing so we can see we are where we belong at the end of
;each successive month of calculation
^!InsertText Seconds in Hour ^%basehour% ^$Calc(^%secondsinhours%+^%secondsindays%+^%secondsinmonths%+^%secondsinyears%+1167609600)$^P
;repeat until we hit current month (tested above)
^!Goto next_hour

:Loop_minutes_to_seconds
^!InsertText +++++++++++++++++++Full Minutes to Seconds Plus Seconds++++++++++++++++++++++++++^P
;calculate seconds in whole minutes today AND the REMAINING SECONDS TOO
^!Set %secondsinminutes%=^$Calc((^%minutes%*60)+^%seconds%)$
;insert text is just for testing so we can see we are where we belong at the end of
;each successive month of calculation
^!InsertText EPOCH TIME: ^$Calc(^%secondsinminutes%+^%secondsinhours%+^%secondsindays%+^%secondsinmonths%+^%secondsinyears%+1167609600)$^P
^!Goto end

Leave a Reply

Recent Posts

Archives

Topics