Should Postgres timezone be changed from UTC? - ruby-on-rails

I have a cron that runs a script in my rails app hourly looking for new transactions to run. Typically this only picks up new transactions set to start on the current day, until midnight UTC, when all the following day's transactions become due. My rails app is operating in Central Time, but Postgres is set to UTC. Since I'm using a scope with a where clause, and now() to compare the dates, transactions for the new day run in the early evening (in the US). Is there a downside to switching Postgres's timezone to Central Time? From what I can tell, it will record all timestamps in UTC regardless, but will run queries based on its timezone setting.

You should always use UTC inside the application, and only perform conversions to other timezones at the edge of the application where necessary - such as in controllers or views.
In your query, perform some date manipulation on now() to translate it to the timezone you need.

Related

Scheduling a Cognos report based on Dates from a data warehouse table

We have a report already written for Student Services, but we need to schedule it for specific times in the term; these times are from the date table in our data warehouse. For example, we need it on the first day of the term (one of the MANY dates defined in our date table), and two weeks prior to the first day of the term. If the current date is either one of these dates, we need the report to run; otherwise no. Should I use trigger-based Cognos reporting? Is there a way to do it in regular Cognos scheduling? Should I schedule it out of an external (Oracle) stored procedure?
We were able to set up Event Studio to first have a daily check to see if it is 14 days before term (had to add that to the date table), and 2 weeks after start of term (also in our date table). Set up the run condition, set up tasks for the reports required, then set up the email. Could not set up Run Agent in Event studio (IBM was singularly unhelpful here) so we scheduled it in Cognos. It runs like a charm.

How to prevent edge cases or delay for a deadline of a scheduled task?

Suppose I have an event scheduled in the future with start_date, end_date (inclusive), timezone, and status. The dates are wall-time since users are from all of the places with different timezones. There will be incoming transactions and I'd like to check the validity of these transactions against the status of the task.
If the status of the task is active then it's still valid. What I'm planning to do is to setup a cron job (or ActiveJob in Rails) to run every 15 minutes to check whether the task has already started or ended according to the user's timezone setting and update the status field.
The problem is that suppose the task's end_date is 15 December and the cron job begins at exactly 16 December 00:00 at which the task should be already expired and the cron job takes approximately 2 minutes to complete the whole database. Then the status will updated to inactive at 16 around December 00:02. If there is a transaction coming in at 16 December 00:01 and then the application checks the validity against its status which is still active but in fact it's already past the deadline.
Any ideas how to combat this problem?
By the way, although the application itself is not really serious and can afford some mismatches, I'm still worried about the data intregrity when querying since there will be some datapoints missing out of the valid date range.
Cron won't work since it creates the problem you describe, but with activejob you can tell it the exact date and time to run https://edgeguides.rubyonrails.org/active_job_basics.html#enqueue-the-job
By the way, if something depends on a date, why don't you actually check that date instead of the "status" column?

Identify and inter conversion between plain date times (GMT/BST) to UTC

I got a legacy system(SQL Server DB) which holds date in plain date time format.
There is also a MS Dynamics CRM system which user interacts and inserts data to CRM DB. Data flows from legacy system to CRM.
The problem is CRM thinks all the data coming from legacy system is UTC formatted, in actual its a combination of GMT & BST plain date time values.
This results in some transactions time out of phase by an hour.
How should I tackle this problem?
The one solution I can think of is, to identify if the date falls under BST, subtract one hour from it and supply to CRM.
As BST = GMT + 1 hr and GMT and UTC are likely the same, thought this might solve the problem.
I'm not sure if I have ruled out all the possible issue with this problem.
Are there any alternative approaches?
Manipulating the difference & sending the UTC timestamp to CRM works fine.
Alternatively you can incorporate a new UTC field in legacy system & that can be used as offset value, so that sync between two systems.

How should I deal with Time Zones on a Client-Server app?

I have an iOS application and a rails backend.
The iOS client allows user to pick Dates in the (Year|Month|Day) format. No time is necessary, it's preferable if 00:00:00 is used.
What's the best practice for this, and what should I use?
1) Should the client convert times into UTC, and then save those to the server. And then when the client fetches times, convert it back into the user local time?
2) Should the client just push up whatever time it wants, and leave it for the server to decide what to do with it?
Here's what I'm doing with my apps:
Backend/server only deals with UTC. Always store times in UTC into the MySQL database (or whatever database you're using).
Push the conversion of time differences logic to the client. Most clients have a third party solution dealing with conversion of time (for iOS, check: https://github.com/yannickl/YLMoment ).
In mobile clients, I would still store the times in UTC into the local database. The only time I do any conversion is when the date is being displayed on the UI. This allows for users to travel into different timezones and the dates will be updated to reflect the local timezone.

Managing timezones for different users

I have a website where I have many users coming from different countries. Users can schedule a task based on their timezone. Now there is a cron running on the server after every min, the cron executes a script which checks if there are any scheduled task of any user and if so it does the needful.
Since my server is based in the US, the script executed by the cron considers the timezone of the US. What do I have to do in my script that will execute the user's task based on user's timezone instead of server's timezone?
Thanks in advance for any ideas
Lookup the user's timezone.
Compute the current time in the user's timezone.
For each job, look up the last time it was run and compute the next time it should run.
For any job whose next run time is now or in the past, run that job and update the record of the last time each job was run.
I did something similar on the iPhone a few months ago.
My solution was to capture the time as a string. So if the user selected 8am, I would just capture 08:00 and their time zone e.g. Europe/London.
Every 5 minutes or so on my server, I could then convert this 08:00 into the current UTC time based on the timezone. If this time was "present", I would carry about a check on the user's transport status and issue alerts.
To help me with the TimeZones, I used NodaTime. http://noda-time.blogspot.co.uk/

Resources