• Support
  • Articles
  • Resources
  • Products

Math with Dates

Moderator: boris

2 postsPage 1 of 1

IshmaelCallMe
Guru

Posts:
672
Joined: Thu Jan 26, 2006 2:28 am
by IshmaelCallMe » Tue Apr 15, 2008 9:16 pm
These subroutines convert a date and time into the number of seconds since 2003 and back. You can then add or subtract seconds and then convert back. This way, even if the time conversion goes across dates or the midnight or noon boundary, it will still be correct.

Code: Select all

  1.  
  2. timezone='Central'
  3.  
  4. gettime timestr
  5. getdate datestr
  6. strcopy datestr 6 2 month
  7. strcopy datestr 9 2 day
  8. strcopy datestr 1 4 year
  9. strcopy timestr 1 2 hour
  10. strcopy timestr 4 2 min
  11. strcopy timestr 7 2 sec
  12. call timeconv
  13.  
  14. strscan 'Central Eastern Pacific' timezone
  15. if result=1 total=total-3600
  16. if result=17 total=total-(3*3600)
  17.  
  18. call convtime
  19. messagebox n_date 'New time is'
  20. exit
  21.  
  22. :timeconv   ;convert year/month/day/time to seconds
  23. str2int imonth month               ;for numeric month
  24. strscan 'JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC' month
  25. if result<>0 imonth=((result-1)/4)+1      ;for spelled out month
  26. str2int iyear year
  27. n=(iyear-1901)*365+((iyear-1901)/4)
  28. for i 1 imonth-1
  29.    strcopy '31 28 31 30 31 30 31 31 30 31 30 31' ((i-1)*3)+1 2 sndays
  30.    str2int ndays sndays
  31.    if i=2 if iyear%4=0 ndays=ndays+1
  32.    n=n+ndays
  33. next
  34. str2int iday day
  35. n=n+iday-(102*365)   ;subtract days from 1901 to 2003 to keep from overflowing
  36. str2int ihour hour
  37. str2int imin min
  38. str2int isec sec
  39. total=n*24*60*60+ihour*60*60+imin*60+isec
  40. return
  41.  
  42. :convtime   ;convert seconds to year/month/day/time
  43. n_year=2003
  44. while (((n_year-1901)*365+((n_year-1901)/4))-(102*365))*24*60*60 < total
  45.    n_year=n_year+1
  46. endwhile
  47. n_year=n_year-1
  48. n_leftoversecs=total - ((n_year-1901)*365+(n_year-1901)/4-102*365)*24*60*60
  49. ntmp=0
  50. n_month=1
  51. while ntmp < n_leftoversecs
  52.    strcopy '31 28 31 30 31 30 31 31 30 31 30 31' ((n_month-1)*3)+1 2 sndays
  53.    str2int ndays sndays
  54.    if n_month=2 if n_year%4=0 ndays=ndays+1
  55.    ntmp=ntmp+ndays*24*60*60
  56.    n_month=n_month+1
  57. endwhile
  58. n_month=n_month-1
  59. n_leftoversecs=n_leftoversecs-ntmp+ndays*24*60*60
  60. n_day=n_leftoversecs/(24*60*60)
  61. n_leftoversecs=n_leftoversecs-n_day*24*60*60
  62. n_hour=n_leftoversecs/(60*60)
  63. n_leftoversecs=n_leftoversecs-n_hour*60*60
  64. n_min=n_leftoversecs/60
  65. n_sec=n_leftoversecs-n_min*60
  66.  
  67. int2str n_date n_year
  68. strconcat n_date '/'
  69. int2str stmp n_month
  70. call dig2
  71. strconcat n_date '/'
  72. int2str stmp n_day
  73. call dig2
  74. strconcat n_date ' '
  75. int2str stmp n_hour
  76. call dig2
  77. strconcat n_date ':'
  78. int2str stmp n_min
  79. call dig2
  80. strconcat n_date ':'
  81. int2str stmp n_sec
  82. call dig2
  83. return
  84.  
  85. :dig2         ;convert number to 2 digits
  86. strlen stmp
  87. if result=1 then
  88.    stmp1='0'
  89.    strconcat stmp1 stmp
  90.    stmp=stmp1
  91. endif
  92. strconcat n_date stmp
  93. return
  94.  
Trevor
Guru

Posts:
474
Joined: Wed Jul 26, 2006 8:47 am
by Trevor » Thu Nov 14, 2013 1:58 pm
If you need to deal with epoch times then to get seconds as a standard unix epoch time (seconds since 1st Jan1970 00:00:00) in timeconv change this line:

Code: Select all

  1. n=n+iday-(102*365) ;subtract days from 1901 to 2003 to keep from overflowing
to this:

Code: Select all

  1. n=n+iday-25203 ;subtract days from 1901 to 1970


The conversion the other way (convtime) from date string to seconds would also need adjusting but I haven't yet needed to do this , will post again if I do make this adjustment for convtime also.

Regards
Trevor
Display posts from previous:
Sort by:

2 postsPage 1 of 1

Users browsing this forum: No registered users