Logging time zone aware events - mysql

I know pages and pages have been written on this topics, however, I need to clarify this issue, and be sure I'm doing what is sensible.
The users of my app can be in any part of the world. I log a number of events/actions a user performs on my app. I want to be able to see what events/actions a user made in it's own time zone, or a timezone of my choosing.
At the moment, my app is sending the date and time of the event in UTC in the format "YYYY-MM-DD HH:MM:SS" and also, in a separate field, the time zone offset in minutes (e.g. -120 or +60).
When I get the events, I store them in MySQL in the following way: I use a DATETIME column to store the date and time of the event as is; I use an INT column to store the offset.
When I want to do a select and get back the event in the user's time zone, I just do:
SELECT cdate + INTERVAL tz_offset MINUTES FROM events WHERE uid=123;
I believe this is the best option for me, and offers versatility.
What do you think?
Thank you!

Related

How do I store a toggleable duration in a database

I want to give users of my Laravel Application the option to track the time they spend on different tasks. On clicking a "start" button, I want to create a new instance of a "tracking" object, which has an id and a reference to the task it is tracking the time for as a foreign key. Also, I want to store a timestamp from the second it was created on. Later, the user is supposed to stop the tracking with another button click.
I need to store the duration of the tracking somehow, because I want to give reports of what task had how many trackings, what was the total time of all summed up, etc. It would be an option to calculate the duration by subtracting start from stop, as soon as the tracking is being stopped and save it to the object.
However, I want the user to be able to continue a tracking later, so that it starts counting again. If I would then set the timestamp on ending again, it would calculate the entire duration, even though it could have been disabled inbetween.
Another idea was to have a last_started and a total_duration column. When the user (re-)starts a tracking, the timestamp is included. As soon as he closes it, the duration is added to the current total_duration and and last_started is set to null again. This feels like a dirty workaround and I am sure there are better options out there, which I would love to hear.
Thanks in advance!
Your question is a bit vague to me, but if I understand correctly, you just want ONE tracking record per task per user. I don't see why you can't make it a bit easier.
Create a model TaskTimeTrack with
user_id
task_id
starttime
endtime
then in your controller, you should probably have a method start and stop
In your start method check if you have a TaskTimeTrack record for the user and the task and endtime IS NULL; if so, he shouldn't be able to start a new tracking, as he is still on an active tracking; just return the existing TaskTimeTrack (if you want to return anything)
If you don't find such record, just create a new one then.
In your stop method, just search for the TaskTimeTRack for the user and task where endtime IS NULL, and update the endtime to NOW.
there is no reason why you would want to limit just 1 record per user / task. You just calculate the total time by sum(endtime - starttime) of all the TaskTimeTRack for a particular user or task or combination of both.

Time adjustment with ajax and mysql

I am developing a facebookapp where two users play against and the time counts to zero. Whats the best way to do this? I tried to send an ajax request to a php script every second which updates the database with the current time in seconds that the user has left. But sometimes I have 0 values at the columns and the user has no time left. The query strings are ok. Would it be better to store timestamps?
If I would not store the times and the user refresh the page he gets the time back.
Why don't you save the exact timestamp when the clock shows zero?
You can handle all outputs with these one number. You don't have to send requests all the time. Only at first and than when your timestamp reached.

MySQL Database Structure For Employee Timeclock

I'm working on an app that is partly an employee time clock. It's not too complex but I want to make sure I head in the right direction the first time. I currently have this table structure:
id - int
employee_id - int (fk)
timestamp - mysql timestamp
event_code - int (1 for clock in, 0 for clock out)
I've got everything working where if their last event was a "clock in" they only see the "clock out" button and visa-versa.
My problem is that we will need to run a report that shows how many hours an employee has worked in a month and also total hours during the current fiscal year (Since June 1 of the current year).
Seems like I could store clock in and outs in the same record and maybe even calculate minutes worked between the two events and store that in a column called "worked". Then I would just need to get the sum of all that column for that employee to know how much time total.
Should I keep the structure I have, move to all on one row per pair of clock in and out events, or is there a better way that I'm totally missing?
I know human error is also a big issue for time clocks since people often forget to clock in or out and I'm not sure which structure can handle that easier.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Thanks for any advise/direction.
Rich
I would go with two tables:
One table should be simple log of what events occurred, like your existing design.
The second table contains the calculated working hours. There are columns for the logged in and logged out times and perhaps also a third column with the time difference between them precalculated.
The point is that the calculation of how many hours an employee has worked is complicated, as you mention. Employees may complain that they worked longer hours than your program reports. In this case you want to have access to the original log of all events with no information loss so that you can see and debug exactly what happened. But this raw format is slow and difficult to work with in SQL so for reporting purposes you also want the second table so that you can quickly generate reports with weekly, monthly or yearly sums.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Timestamp is good because there are lots of MySQL functions that work well with timestamp. You might also want to consider using datetime which is very similar to timestamp.
Related
Should I use field 'datetime' or 'timestamp'?

created_at in where statement and timezone

I'm trying to retrieve all the entries of the day in my model:
where('date(created_at) = date(?)', Date.today)
But, because of the timezone (My local time is UTC+2), I don't have the expected result. For example, if an entry has been created one minute after midnight, in the database, it will be stored at 22:01 (so not "Date.today").
Is there a way to do that ?
EDIT:
As the admin of the website, I want to display the number of the entries on my local time "today" (It's only for statistics purpose!). So I want to keep timestamps store in utc, but convert them to my local timezone during this request!
You can set the correct time zone in several ways. One is described in this answer:
https://stackoverflow.com/a/6118837/567126
adding following to application.rb works
config.time_zone = 'Eastern Time (US & Canada)'
where('date(created_at) = ?', Time.zone.now.to_date)
Time in the database should be stored as UTC. This way, when you do a worldwide website, you can serve each user with their own timezone.
It's the UI (views) responsibility to show the time in the user's local timezone.
For your where you should use Time.now.utc
This way, you are searching the DB with the common UTC.

Retrieving Data into a Table Depending on Time?

I'm building an 'events' table that will show the events for that particular day.
However, since most events start late in the evening and end early in the morning I would like to know as to how I should plot and retrieve the data. Let's say that and event day starts at 6:01 AM and ends at 6:00 AM. Is the time dependent on the server time? Can I change it to some other GMT time? How would the time table look in the mysql database?
Thank you!
I would have a table like this:
Event
======
EventID
Name
StartTime
EndTime
StartTime and EndTime would be stored as UTC dates. With this schema you can query however you like, and present the time for whatever time zone you like, but it keeps things consistent and well performing. You would want to index EventID, StartTime, and EndTime.