Apparently neither java.util.Date nor java.util.Calendar implement Persistable, so cannot be used in Persistable classes. What is the best alternative for persisting dates and times?
You could write your code to expect that dates and times are kept in the Unix epoch format (number of seconds since 1/1/1970). Then your problem is simply a matter of storing a long number.
storing as a long as Dan mentions is the way to go. System.currentTimeMillis() will return milliseconds since Unix Epoch, which is 1/1/1970 GMT. It's obviously fairly trivial to convert between seconds and milliseconds. For timezone corrections, the Calendar class can help you handle this.
Note:
getInstance(Timezone zone)
getTimeInMillis()
setTimeInMillis(long millis)
setTimeZone(Timezone value)
Related
Some countries are observing daylight saving, but I want to get the timezone name irrespective of daylight saving.
In short I want standard time of such countries instead of daylight saving time.
Is there any way to convert this or anything else?
P.S. I want this because of my server requirement.
//----------------------------------------------------------------
I have seen this reference in apple.
+ (instancetype)timeZoneForSecondsFromGMT:(NSInteger)seconds
Discussion The name of the new time zone is GMT +/– the offset, in
hours and minutes. Time zones created with this method never have
daylight savings, and the offset is constant no matter the date.
But I wonder how it will useful to me for my problem? As I want timezone name without daylight saving not any time with offset.
If you just need the time zone with respect to GMT without accounting for DST, the simplest option would be to:
Find the DST offset using 'daylightSavingTimeOffset' on your suspect time zone.
Subtract that value from the timezone's GMT offset.
Create a new time zone with the now adjusted offset.
If you need the name of the standard time zone, the simplest approach might just be to look for "DST" and "Daylight" in the time zone description and then pick the time zone that has the greatest number of words matching to the DST time zone. That might not be the most elegant solution, but it would work for most time zones.
It was actually this
typedef enum : NSInteger {
NSTimeZoneNameStyleStandard,
NSTimeZoneNameStyleShortStandard,
NSTimeZoneNameStyleDaylightSaving,
NSTimeZoneNameStyleShortDaylightSaving,
NSTimeZoneNameStyleGeneric,
NSTimeZoneNameStyleShortGeneric
} NSTimeZoneNameStyle;
The enum answered it for me. My bad , I should have read this earlier.
Given an NSTimeInterval representing a period of time, how do I create a human-readable string that expresses that time period in a way that respects i18N?
For example, if the NSTimeInterval is equal to 10823.23435 seconds, then some reasonable ways of rendering that as a string for those in the US might be:
3:23
3 hours, 23 seconds
etc.
(I.e., stuff you'd see on a stopwatch.)
Presumably other locales might use different renderings.
I'm aware of NSDateFormatter and NSNumberFormatter, but it's not clear to me if or how to use one of these for this problem.
I'm also aware of FormatterKit, but that only does relative time periods (e.g., "3 minutes ago"). I'm looking for absolute time periods.
It is trivial to cook up my own locale-specific solution, but has this problem been solved properly already?
Thanks.
For iOS 8 there are NSDateComponentsFormatter & NSDateIntervalFormatter, they may work for your application.
Docs are lacking see NSHipster: NSFormatter
and
Foundation/NSDateIntervalFormatter.h
As the subject asks; do UNIX timestamps change in each timezone?
For example, if I sent a request to another email the other side of the world saying, "Send out an email when the time is 1397484936", would the other server's timestamp be 12 hours behind my own?
The definition of UNIX timestamp is time zone independent. The UNIX timestamp is the number of seconds (or milliseconds) elapsed since an absolute point in time, midnight of Jan 1 1970 in UTC time. (UTC is Greenwich Mean Time without Daylight Savings time adjustments.)
Regardless of your time zone, the UNIX timestamp represents a moment that is the same everywhere. Of course you can convert back and forth to a local time zone representation (time 1397484936 is such-and-such local time in New York, or some other local time in Djakarta) if you want.
The article at http://en.wikipedia.org/wiki/Unix_time is pretty impressive if you'd like a longer read.
Unix time is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. So the answer is no
Unix timestamps do not change accross timezones, they are created for the purpose of having a standard time across globe.
NOTE:-
Timestamps are calculated on the basis of current time in the computer thus do not rely on them until and unless you are very sure about the time settings in the participating machines.
Someone stated that "UTC is Greenwich Mean Time without Daylight Savings time adjustments." This is simply untrue. GMT does not have Dayllight Savings Time. GMT is measured in Greenwich, England (at the Naval Obeservatory) [0 longitude, but not 0 lattitude]. UTC is measured at the equator [0 longitude and 0 lattitude - which happens to lie in the ocean off the cost of Africa].
What difference does it make? It doesn't make a difference in terms of "what time of day is it?" It does, however, make a difference in terms of calculating a year. Now you'd think a year would be measured based upon the location of the center (the core) of the earth, right? When the earth's core is back in the same location it was ~365 days ago, it has been a year. It isn't measured that way. It is measured by a specific location on the earth getting back to the same location (relative to the sun) that it was ~365 days ago. But the period of a day and a year don't divide evenly. Once the earth is back to about where it was a year ago, the earth isn't facing the same direction it was last year, so that spot on the earth isn't facing the same direction it was a year ago. Being further north, Greenwich isn't going to get back to the same spot (relative to the sun) that it was last year at the same time that 0 Lat / 0 Long is. So if you base the definition on Greenwith vs. 0/0, you get a, albeit slightly, different answer to the question "how many days are in a year". To put it another way, when a given spot on the earth gets back to where it was a year ago (relative to the Sun), the core of the earth isn't in the same spot it was a year ago, so what spot you pick matters because the core of the earth is going to be in a different spot (relative to the sun) than it was one year ago, if you pick a different spot on the earth.
Neither UTC nor GMT have daylight savings time. Europe/London time, the timezone that Greenwich resides in, does. But GMT does not. GMT is, what Americans would call a "Standard Time" - i.e. without DST.
Getting back to the question, Epoch time doesn't technically have a timezone. It is based on a particular point in time, which just so happens to line up to an "even" UTC time (at the exact beginning of a year and a decade, etc.). If that concept doesn't fit well in your brain, and if it helps to think of Epoch time as being in UTC, go right ahead. You're in good company and in the grand scheme of things, it really doesn't matter. You ever see those law suits where somoene is awarded $1. It's kind of a "you're right, but it doesn't really matter" type of verdict. If someone sued you for saying Epoch time is in the UTC timezone, they would win $1. That wouldn't buy them a cup of coffee at any Starbucks in any timezone on the planet.
IF both computers are set up correctly with their clocks set for the correct timezone and UTC values, they should return the same value.
Of course that's a big IF. There's almost certain to be a difference of at least a second, more often minutes between the time reported by two computers. And many computers are set up to have incorrect timezone settings, and will report their local time when asked a timestamp rather than UTC.
And in that lies the difference between theory and practice. In theory it's all the same, in practice you should not rely on it.
No, epoch timestamp should not change, because it has a fixed timezone which is UTC.
If you want to use a time object in other time zone, just look it up in libraries of the language you use, but do NOT try to add/substract a couple of hours from epoch timestamp and assume it's in another time zone, which will make things very confusing to other people, especially when you expose it in your API.
If you use C++, I recommend this library. I heard it will soon be added into standard library.
For all, I understand sometimes time object is hard to deal with and it looks easier to add/substruct on epoch timestamp. Please don't do it and do not persuade others to do it. A time object is much easier once you get used to it and can take care of time zone conversion easily without messing up with historical time zone changes due to politics/law etc...
I want to store times type value (but not the date) in Core Data. Which data type should I use?
Possible options:
Three integer attributes for hour, minutes, seconds.
One string attribute for "HH:MM:SS".
One integer attribute for the number of seconds since midnight.
The best representation depends on what type of queries involving the time you want to
execute. The first option is better if you want to search for a given hour, minute or second. The other options are better if you have to search for time intervals.
My approach would probably be to convert it to an HHMMSS format, either as an integer or a string. NSDate isn't appropriate because it is stored as a interval since an epoch (seconds since 1970)
The date portion of an NSDate is inherently part of time. NSDate is a NSTimeInterval number of seconds positive or negative since the reference date. With a locale and a timezone you can get an appropriate localized display string on the fly.
In most cases it makes far more sense to store the number behind the NSDate returned by timeIntervalSinceReferenceDate
It's small and portable and correct.
Then recreate an NSDate with dateWithTimeIntervalSinceReferenceDate:
Then use an NSDateFormatter with NSLocale and NSTimeZone to get an appropriate value for display to users.
It's an unwieldy and cumbersome set of tools that actually help you do it right.
The good news is all the effort you expend in doing this pays off in making it easier next time and giving you code that will do the right thing and have a backing value that is actually really easy to store and manipulate.
Additionally by using the API correctly you will lose no precision and have more possibilities open to you.
Trying to do it with format strings alone seems easy and simple but is the road to bad code.
I realise this question could have been answered by writing some test code. I'm not lazy, I just thought that the answer might be generally useful.
I have an app that has generated a large amount of data with records that were stamped with the local time (as returned by the NOW routine). We have run into a snag with transitions in and out of daylight savings time - namely that there is an hour missing when we change to DST, and an hour repeated when we exit from DST. This causes problems with manipulations that assume date ordered records.
The app has been altered therefore to work with all datetimes in UTC, but I will have the ability to display datetimes in UTC or in local time. I also have to deal with datetimes that were stored in local time, and make sure they are correctly shifted to UTC. This is tricky, as the datetime might have been stored while DST was in effect, so in the general case I need to be able to determine if any random date is within or outside a DST period. There is of course a period of one hour where a datetime is ambiguous and could be in the last hour before daylight savings ended, or in the first hour after it ended. There is no way of resolving this.
In coding the changes, I wondered about the result of NOW calls. Internally it calls GetLocalTime. What does GetLocalTime (and NOW) return when you are inside a DST period, but the option to "Adjust clock for daylight saving changes" is turned off?
How do I write a routine that returns the current datetime inside a DST period (with the DST bias applied) regardless of whether "Adjust clock for daylight saving changes" is off or on?
I don't think you can solve your problem easily.
There are too many variables:
the stored timestamp
the time zone you are in
the ever changing time zone rules
confirmation that these time zone rules are accurate on all the equipment you use (i.e. everyone always applied their patches)
the inaccuracy of your clock
There is a Delphi TZDB project that can help you with the time zone rules.
I think it is much more practical to not rely on all the above variables, but store three fields:
the timestamp in your local format
the current timezone
the timestamp in UTC format
You perform the sorting on the third field, and the first two fields for displaying.
--jeroen
Use TzSpecificLocalTimeToSystemTime (and its obvious inverse). These allow you to convert between UTC and local date/time based on the daylight savings settings in effect at the local date/time. If you want your app to run on anything earlier than XP, load this (from kernel32) with the 'delayed' function attribute:
function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall;
function TzSpecificLocalTimeToSystemTime; external kernel32 name 'TzSpecificLocalTimeToSystemTime' delayed;