Author Topic: Epoch/UNIX Time Function  (Read 2914 times)

0 Members and 1 Guest are viewing this topic.

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Epoch/UNIX Time Function
« on: April 27, 2021, 08:43:18 am »
I needed a function to generate epoch time from within qb64.
the following code/file, is a function that generate epoch time in seconds.

The usage for the code is:
Code: QB64: [Select]
  1. x = epoch

Code: QB64: [Select]
  1. FUNCTION epoch&&
  2.     DIM mc(12)
  3.     '--get days passed since 1-1-1970--
  4.     year_day_count = INT((VAL(RIGHT$(DATE$, 4)) - 1970) * 365)
  5.     leap_year_days = INT((VAL(RIGHT$(DATE$, 4)) - 1970) / 4)
  6.     total_past_days = year_day_count + leap_year_days
  7.     total_past_sec = total_past_days * 86400
  8.  
  9.     '--get days so far in month--------
  10.     mc(1) = 31
  11.     mc(2) = 28
  12.     mc(3) = 31
  13.     mc(4) = 30
  14.     mc(5) = 31
  15.     mc(6) = 30
  16.     mc(7) = 31
  17.     mc(8) = 31
  18.     mc(9) = 30
  19.     mc(10) = 31
  20.     mc(11) = 30
  21.     mc(12) = 31
  22.     '--get days past to month start----
  23.     FOR i = 1 TO 12
  24.         x = x + 1
  25.         IF x >= VAL(LEFT$(DATE$, 2)) THEN EXIT FOR
  26.         day_count = day_count + mc(x)
  27.     NEXT
  28.     current_month_sec = day_count * 86400
  29.  
  30.     month_days = VAL(MID$(DATE$, 4, 2))
  31.     month_time_sec = month_days * 86400
  32.  
  33.     '--get current seconds in day------
  34.     hours = (VAL(LEFT$(TIME$, 2)) - 1) * 3600
  35.     mins = VAL(MID$(TIME$, 4, 2)) * 60
  36.     sec = VAL(RIGHT$(TIME$, 2))
  37.     day_time_sec = hours + mins + sec
  38.  
  39.     epoch = total_past_sec + month_time_sec + current_month_sec + day_time_sec

Hope someone finds this useful.
* epoch.bas (Filesize: 1.12 KB, Downloads: 180)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Epoch/UNIX Time Function
« Reply #1 on: April 27, 2021, 09:53:41 am »
I'm getting timestamps off by about 1 hour.

Code: QB64: [Select]
  1. x&& = ExtendedTimer
  2. y&& = epoch
  3. PRINT x&&
  4. PRINT y&&
  5. PRINT x&& - y&&
  6.  
  7. FUNCTION ExtendedTimer##
  8.     d$ = DATE$
  9.     l = INSTR(d$, "-")
  10.     l1 = INSTR(l + 1, d$, "-")
  11.     m = VAL(LEFT$(d$, l))
  12.     d = VAL(MID$(d$, l + 1))
  13.     y = VAL(MID$(d$, l1 + 1)) - 1970
  14.     FOR i = 1 TO m
  15.         SELECT CASE i 'Add the number of days for each previous month passed
  16.             CASE 1: d = d 'January doestn't have any carry over days.
  17.             CASE 2, 4, 6, 8, 9, 11: d = d + 31
  18.             CASE 3: d = d + 28
  19.             CASE 5, 7, 10, 12: d = d + 30
  20.         END SELECT
  21.     NEXT
  22.     FOR i = 1 TO y
  23.         d = d + 365
  24.     NEXT
  25.     FOR i = 2 TO y STEP 4
  26.         IF m > 2 THEN d = d + 1 'add an extra day for leap year every 4 years, starting in 1970
  27.     NEXT
  28.     d = d - 1 'for year 2000
  29.     s~&& = d * 24 * 60 * 60 'Seconds are days * 24 hours * 60 minutes * 60 seconds
  30.     ExtendedTimer## = (s~&& + TIMER)
  31.  
  32.  
  33.  
  34.  
  35. FUNCTION epoch&&
  36.     DIM mc(12)
  37.     '--get days passed since 1-1-1970--
  38.     year_day_count = INT((VAL(RIGHT$(DATE$, 4)) - 1970) * 365)
  39.     leap_year_days = INT((VAL(RIGHT$(DATE$, 4)) - 1970) / 4)
  40.     total_past_days = year_day_count + leap_year_days
  41.     total_past_sec = total_past_days * 86400
  42.  
  43.     '--get days so far in month--------
  44.     mc(1) = 31
  45.     mc(2) = 28
  46.     mc(3) = 31
  47.     mc(4) = 30
  48.     mc(5) = 31
  49.     mc(6) = 30
  50.     mc(7) = 31
  51.     mc(8) = 31
  52.     mc(9) = 30
  53.     mc(10) = 31
  54.     mc(11) = 30
  55.     mc(12) = 31
  56.     '--get days past to month start----
  57.     FOR i = 1 TO 12
  58.         x = x + 1
  59.         IF x >= VAL(LEFT$(DATE$, 2)) THEN EXIT FOR
  60.         day_count = day_count + mc(x)
  61.     NEXT
  62.     current_month_sec = day_count * 86400
  63.  
  64.     month_days = VAL(MID$(DATE$, 4, 2))
  65.     month_time_sec = month_days * 86400
  66.  
  67.     '--get current seconds in day------
  68.     hours = (VAL(LEFT$(TIME$, 2)) - 1) * 3600
  69.     mins = VAL(MID$(TIME$, 4, 2)) * 60
  70.     sec = VAL(RIGHT$(TIME$, 2))
  71.     day_time_sec = hours + mins + sec
  72.  
  73.     epoch = total_past_sec + month_time_sec + current_month_sec + day_time_sec
  74.  

(ExtendedTimer can be found in the sample area here on the forums: https://www.qb64.org/forum/index.php?topic=1598.0 )

Checking the timestamps against the website here, your values appear to be off by about an hour: https://www.epochconverter.com/

Looking at the code, is this intentional?       hours = (VAL(LEFT$(TIME$, 2)) - 1) * 3600

It seems as if you're subtracting an hour from the timer intentionally?  If this is for a timezone localization, you might want to make a note of that in your post and make it a variable which another user could set to adjust for their own timezone as needed.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline AtomicSlaughter

  • Newbie
  • Posts: 14
    • View Profile
Re: Epoch/UNIX Time Function
« Reply #2 on: April 27, 2021, 11:15:47 am »
i dont even know why that -1 is in there thats a bit odd i dont remember putting that -1 in there