I'm working on an app that will be used by the public services (ambulance). Since those people work around the clock, they will realistically need to enter date/time values at ANY time of the day. Which also includes DST changeover times.
Now, when entering the time at the "short" day, things are easy. Simply any value between 3:00:00 and 3:59:59 is invalid. Problem is with the "long" day, where values in that same interval are ambiguous.
Is there any standard way/notation for entering time at that dreadful hour? Have you ever used any other workarounds, and which ones did your users find good?
Added: Also cloned on ux.stackexchange.
Allow the user to enter the localised time (i.e. whatever their PC clock is displaying) but save it in UTC. When you display them remember to use a localised time method and it will fix itself, see this SO Question for more details: How to display localized date and time information to web users with ASP.NET
Related
I need to store datetime records with IANA database current version used (2022g for example). How could I get it with Go?
I tried to search this on standard "time" package, but it seems that there isn't any functionality for this. I am expecting that there is some function that can return IANA tzdb version as a string.
UPDATE 1 according to comments below I need to clarify the problem:
The main problem is I want to store some FUTURE events. The event object has several fields:
Local dateTime
Timezone
UTC datetime
To keep my data up to date with IANA database (timezone, daylight saving time may change) I need to store current version of tzdb version. That will help me to write correct data migration of my events when new version of tzdb was released. So I need to add one more field with version of current tzdb that had been used to populate the time.
And I am trying to figure out how can I get the current version of my tzdb that Go application is using right now to store that version.
Also I am opened to alternative solutions of storing time records with extra accuracy of long-lived future events.
Update 2: This events are bounded to exact location.
The discussion thread in the comment is pretty long, but I'll attempt to answer and address some of the concerns. (I won't address the question in the title, as I believe that is not straightforward in Go.)
Indeed, future scheduling of events should be in terms of the time zone where the event takes place - which is usually not UTC.
Time zone identifiers will never be removed or renamed (with rare exception anyway). Once introduced, the identifier will either maintained as a Zone or as a Link in the TZDB indefinitely. Thus, you don't need to check that the time zone still exists. (Windows time zone IDs are also like this.)
DST is only one aspect of picking the correct offset. The standard time may have changed as well. However, all of that is encapsulated in the tzdb itself. You shouldn't need to concern yourself about which version of the tzdb was in effect when you created the event.
The general approach to this issue in most cases is:
Store the scheduled local date, time, and time zone ID of the event (local with regard to the time zone of the event).
Example: 2030-12-31T00:00:00[America/New_York]
At the time you create the event, also calculate a UTC value (or equivalent DateTimeOffset value) and store that in a separate field - so you know exactly when to fire the event:
Example: 2030-12-31T05:00:00Z (or 2030-12-31T00:00:00-05:00)
Periodically check that your UTC equivalent is correct. This can be in a daily maintenance task, or on application startup, or just before the event (perhaps also an hour before the event), or all of these.
The offset will only be different than projected if the time zone data changed on the device to give it a new offset. For example, let's hypothetically say the lawmakers in the USA succeed at making DST permanent sometime before this event takes place. Then the equivalent UTC time for the same event would now be 2030-12-31T04:00:00Z (or 2030-12-31T00:00:00-04:00).
In such cases, update the UTC time of the event if it has changed, but the original local time of the event usually should not be modified. Human beings tend to schedule things in terms of local time, not in terms of their UTC equivalents.
The only advantage knowing the TZDB version would give you, is you could do that last step less often - only when knowing the data has changed. I see that as an optimization though - it's not usually required.
Without such legal changes to time zone definitions, the mere start/stop of DST as scheduled is not a reason to worry about this. That is already accounted for by using the TZDB in the first place.
If the event is recurring (say a 10:00 AM daily meeting), each occurrence might have a different offset, but the local time will be consistent and the TZDB doesn't need to be updated to calculate it.
When creating a new Coursework via the Classroom API, a "due" date and time can be added (
Classroom API TimeOfDay reference ) and the instructions say that "The date and time zone are either not significant or are specified elsewhere.".
In the context of a class, my expectation was that the number sent would be the number displayed (the teacher generally knows what "11AM" will mean to the class).
However, what actually happens is that the time zone that the server communicating with the API is located in is used to interpret the time. I.e. if the teacher is two time zones away from the backend server, the time will be two hours out.
Is the documentation just wrong? Or is there some way to "specify elsewhere" which I can't find?
This question is a little old, but I just went through some of the same issues, so responding here in case anyone else needs help...
It's not entirely clear how you formatted your create request based on the original question. The TimeOfDay object is used to set dueTime, and as stated in the docs, all dates and times for CourseWork create requests should be converted into UTC: https://developers.google.com/classroom/reference/rest/v1/courses.courseWork
I.e. you need to do any conversions into UTC based on your application/user needs, and the Google Classroom UI automatically converts this date/time into the user's local/date time. Currently there is no other way to do this with this API
Implementing this properly can get tricky depending on how your app/UI is set up, and is further complicated by variations like Daylight Savings Time. For example, if you are using JavaScript, JS dates use UTC internally, the user's browser tracks the current time for that user, and there is currently no other built in functionality for any other timezones. See some of the really excellent explanations about this (and various workarounds) below:
Calculate the UTC offset given a TimeZone string in JavaScript
get timezone offset of another timezone in javascript without using Strings
How to initialize a JavaScript Date to a particular time zone
I hope that's at least a little helpful. It's hard to specifically answer your question about 'what's happening' without more information :)
I can't figure out how to make user input for a time field local. I'm creating events in a form with a start_time and an end_time. I'd like to bring in a time from a time field, and store the user's input as their local time. I'd then like to convert it to UTC and compare it to Time.now.utc, in order to determine whether or not the event has started or ended.
I thought about converting the start and end times with start_time.localtime, and then adding/subtracting that from start_time (the default for time_field inputs is UTC), and storing that offset as a variable. Then add/subtract that offset from my stored times, and compare to Time.now (which the default is local).
This seems like much more work than is necessary, as I'm sure this is a common function that people need to use. Is there some local_time_field implementation for forms that I haven't been able to find, or any other overlooked simple solutions?
Any input is appreciated. Thanks!
The most direct way to get at the user's locale is in the browser, with JS. new Date(userDateString).getTimeZoneOffset() will return the user's offset in minutes from UTC. You can use that to convert the times to UTC before sending them up to the server. Of course, this won't work if you need to support JS-less users. Those users will need some other approach--you could make a "my time-zone" dropdown, for example.
Whatever approach you take, you'll have far fewer headaches if you only store times in UTC. Convert user-local times to UTC as early as possible in processing, and only convert back to the user's locale when you're about to display it. Otherwise you have to deal with not only time zones but also DST, and it's a huge headache.
Perhaps this question should be broken up into two posts, but I currently have an API for a few business customers. I am currently using ISO 8601 timestamps with a UTC time zone to represent times. However, I don't like the idea of these timestamps being attached to any timezone because the times should be the same no matter what timezone you are in. 5PM UTC should be 5PM CST, etc...
I know that you can leave the Z off of an ISO timestamp, and it will be interpreted as whatever local time you are in. Is this ok practice? And if so, how do I do this in Ruby? I read the doc for the Time class and didn't see anything about this.
EDIT: Let me re-word this just a little bit, or atleast clarify something. The reason why I'm seeking timestamps that aren't attached to a timezone is exactly because I know that my client servers and API server will hardly ever match up. If a client is submitting an event with a time, that time needs to be ambiguously equal to the ambiguous locale specific to the event that the user is working on.
That's a mouthful...assume that I'm working on an event scheduler. Each event belongs to a storefront or location of a company. When times are being shown for a location, it is assumed that the times shown are in the timezone of the location, and for clarity's sake should never be shown at a time formatted to a user's local timezone. If I'm looking at the scheduler on the East Coast, but looking at events for locations on the West Coast, the times I should see should be local to the locations on the West Coast, not adjusted for my timezone.
I know a solution could be to simply store times with timezone information for the location its associated to. But the use case that a user would want to convert a time to their timezone is VERY rare, and I'd rather make implementing my API easier...this was actually my original implementation but implementing the API in many different environments and across multiple programming languages, it became clear that it is a hurdle to show times local to that timestamp's timezone for a lot of languages. If a user wanted to convert times to their local timezone I could easily store global timezone information for the location object itself.
I don't know what you mean by "the times should be the same no matter what timezone you are in. 5PM UTC should be 5PM CST, etc..". 5PM UTC clearly isn't 5PM CST!
Anyway, I don't think that what you are proposing is an ok practice. Suppose you leave off the Z and have a timestamp be interpreted as whatever local time you are in. Since this is a network API, the client and server might not be in the same timezone. When the client submits a "local" time, what does it mean? The local time on the client (if so, how does the server know what that is?)? The local time on the server? It's ambiguous. This is the crux of the reason why just about the only reasonable thing to do is to use UTC throughout.
What you can do is attach a timezone to a timestamp if it might be relevant. For example, "you should observe one minute of silence at 2012-11-10T22:00:00Z in honour of the soldiers who died in WW1" sounds weird because Rememberance Day isn't on November 10! "you should observe one minute of silence at 2012-11-11T11:00:00+13:00" sounds a lot better once you put that New Zealand time zone in there... In this case you can keep and timestamp (in either local or UTC) together with the timezone offset (e.g. store both of them together in your database).
It does, however, depend on what your times represent. For example, in "at equinoxes, sunset happens at 18:00" it makes sense to use an abstract time that isn't qualified with a timezone (it's true in every timezone, and/or you're talking about solar time). But attaching a date to this abstract time makes little sense, so I don't think you would be talking about ISO8601 in this case.
In the iphone game "Tiny Tower", I'm guessing it uses some kind of simulation based on the time spent between the last play and the current time, because you can set the current time forward and you will get the benefit from the fake elapsed time span.
Is there an algorithm that I can use to prevent this sort of thing? (Or at least make it difficult enough for the average user to pull off!)
Edit: thanks, I understand that, despite my wording, there's no way to prevent things you store on the client side, but I want to make it at least more difficult than "changing the time" to hack it!
The gamecube had a way to do this so it must be possible.
Is there an event triggered when the iphone time is set ? In that case you can react that.
Another solution is to require to be online when the game is launched, this way you can check time on a remote server.
You could has well check if you got an event on the phone login or wake up react to it, saving the time at that moment in your DB. You would have the last non modified time.
A last possible trick is to check for a file you know is going to be modified by an action prior to time change (such as login), and check the 'last modification' date.
You can investigate in the GPS direction as well. A GPS need to be synchronised with the satellite it contact, so it must keep track of time in some way, and maybe there is an API for that.
Unfortunatly you are on an iphone, which mean your possibilities are limited since applications got very few rights and are sandboxed.
EDIT:
Just though about it but, can you create event in the iphone calendar ? And check if it has been trigered ? Cause you could set a fake meeting or something for every day. Not clean, but creative.
EDIT 2: can you set a timer as a code for IOS to execute in 60 minutes ? If you can, set this timer, pass the time expected to be when this code run, then when the code run, compare and inform your program.
One way to prevent it is to monitor time passing by checking timestamps for their logins in a database. It doesn't matter if the client's iPhone's time is off; the database on your end will still know how long it's been since the last login.
I think if you have internet access you can take the time from a server.
A second solution : You can record the "datetime" and every time you see a "BIG" difference between the record datetime and the running datetime you know there might be a problem.
but this is not elegant, i know.
You can also record a small ammount of datetimes that the application started and check the diffrence with the running datetime.
Also you can use "Activity"->"Datetime" so the "Updates" (levels etc) can't be retaken.
Because the system Datetime can be changed by user, there is potential for "hack".
call a web service to get the time, rather than rely on the phone. There are several places you could get time from, google is your friend i'm sure, or create one yourself, and use the local time of the machine the service runs on for the time.
You could also use the Network Time Protocol (NTP) servers to get a consistent time