I have some old data from oracle and the time is in milliseconds generated from Java.
I use this conversion:
to_date('19700101000000','YYYYMMDDHH24MISS')+ column/86400
And it always have a 5 hour shift to the actual time (which I cannot figure out why), so I just directly subtract 5/24 from the date above.
But it does not comes with day time conversion so in winter it is correct but for summer time it is 1 hour shift. How can I solve this?
Also if any one can point why there is a 5 hour shift will be much appreciated!
The 5 hour shift is probably caused by the difference between your local time (Central Time Zone, based on your user name ) and UTC time. Actually the shift should be 6 hours (CST is UTC - 6), so you've probably got it backwards; your summer time is probably closer to correct, and your winter time is still off by an hour.
I'm writing a macro for slick-edit (with the C similar language slick-C)
As far as I found there are no time functions to help me convert epoch time (1321357827) to a human readable date and time
(if any of you knows how to do it in slick-C it's great, those of you who doesn't know it - assume C without time.h or any other libs)
In SE16 you'll find a whole class for date/time manipulation in se/datetime/DateTime.e.
In addition, the built-in function _time has an option to return the epoch time.
You should find enough example code there.
And for the basic algorithm I found another SO question answered on this: Includes a link to gmtime source. From there you should be able to adapt to SlickEdit code.
If you need only the time you could do:
sec_of_day = epoch % (24 * 60 * 60);
hour = sec_of_day / (60 * 60);
minute = sec_of_day % (60 * 60) / 60;
second = epoch % 60;
. This is of course not considering the timezone of your system.
If you need the date, you need to consider leap years.
EDIT: Warning: this code does not take into account leap seconds.
The function mktime takes a struct tm as argument. One of the members of struct tm is tm_isdst. You can set this to 1 for wintertime, 0 for summertime, or -1 if you don't know.
However, if during winter, you try to convert 2009-09-01 00:00, mktime fails to see that although it is currently winter, the date you are converting is summertime. So the result is one hour off. For me (GMT+1) it's 2009-08-31 22:00, while it should be 23:00.
Is there a way to determine if a particular date is in the summer or wintertime period? Is it possible to convert a summer date to utc in the winter?
(I hit upon this problem trying to answer this question)
This is one of the (many) deficiencies in the time handling interfaces in Standard C. See also the Olson time zone database. There isn't an easy way to find when a time zone switches between winter and summer (daylight saving and standard) time, for example. Anything in the future is, of course, a prediction — the rule sets change often (the current release is 2017a).
Is there as far as you know, a UNIX specific solution?
I took a look at the code in tzcode2017a.tar.gz, and the mktime() there behaves as you want if you set the tm_isdst to -1 (unknown). So, if you use that (public domain) code, you would be OK - probably. Quoting from 'localtime(3)' as distributed with the Olson code:
Mktime converts the broken‐down time, expressed as local time, in the structure pointed to by tm into a calendar time value with the same encoding as that of the values returned by the time function. The original values of the tm_wday and tm_yday components of the structure are ignored, and the original values of the other components are not restricted to their normal ranges. (A positive or zero value for tm_isdst causes mktime to presume initially that summer time (for example, Daylight Saving Time in the U.S.A.) respectively, is or is not in effect for the specified time. A negative value for tm_isdst causes the mktime function to attempt to divine whether summer time is in effect for the specified time; in this case it does not use a consistent rule and may give a different answer when later presented with the same argument.)
I believe that the last caveat about a 'consistent rule' means that if the specification of the time zone changes (for example, as when the USA changed from 1st week in April to 2nd week in March for the change to Daylight Saving Time) mean that if you determined some time before the rule changed and after the rule changed, the same input data would give different outputs.
(Note there are useful HTML files in ftp://ftp.iana.org/tz/ directory, such as ftp://ftp.iana.org/tz/tz-link.html.)
I am stuck on the following problem from my C programming class:
Write a program that prompts the user to input a year, and then outputs the calendar(for the entire year).
I have no idea how to approach this problem. I can usually start my homework problems (this is an optional challenge problem), but I am really lost. We've worked through chapters 1-10 of Deitel & Deitel (loops, arrays, pointers, I/O, etc), but I don't know how to approach this at all. Any hints or suggestions would be appreciated.
It might help you to understand the mathematics of the calendar. If the fabulous book Calendrical Calculations is not in your university library, they may be able to get you a reprint of the article by the same authors in Software—Practice & Experience. And ask your prof to request the book for the library.
In general, when you have a big problem like this one, you want to break it down into little problems that are easier to solve.
Here's one possible little problem to start with: if you know how many days there are in a month, and what weekday the first of the month falls on, could you output a calendar for that month?
The hardest part is determining which day of the week the year starts on.
http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
But even without that knowledge, when I first implemented this, I used a reference date (for example, you know that today, January 11, 2010 is a Monday) and counted days from there. (Just keep in mind that leap years have an extra day, and that leap years are every 4 years except every 100 years except every 400 years.)
http://en.wikipedia.org/wiki/Leap_year
Does this code qualify? :-)
char command[]="cal 2010";
sprintf(command,"cal %d",argv[1]);
system(command);
It assumes a Unix machine with cal in the path.
you need to find out first the day on 1st of january
and then print the dates.
GO to https://sourceforge.net/projects/c-cpp-calender/
go through the code and you will understand it
A good start may be the localtime(3) and mktime(3) functions. Alternatively, you can implement the relevant date arithmetic from scratch. Then, simply, generate the first line of the calendar (find the weekday that corresponds to January 1st, then print taht in the right place, followed by the rest of the week), then print all but the last lines, then print the last line.
Depending on if you want a calendar paginated by month or not, this MAY be better done ona per-month instead of a per-year basis.
Well, first figure out the algorithmic part of your problem - given a year, find what day Jan 1st is.
After this, just note the number of days in each month (store it in an array, say num_days[]), and the note the number of months in a year and an array of strings for the months.
For e.g. the outermost loop iterates over the months. Say, iterate for(i=0;i<NumMonths;++i). Then, for each month, print the string, e.g. month[i], then a newline.
Then, with simple tabs, print Sun Mon Tue ..., and another newline.
Then using the day Jan 1st corresponds to (call it FirstDay), insert spaces, and start with that day. Keep printing the dates and newlines till you hit max_month[i] which is 31 (for January). Store the name of day of the last day of the previous month and just reiterate treating that day as FirstDay.
You need a couple pieces to start. First, you need a formula that computes the day of the week for January 1 of whatever year is entered. You'll also need a formula to determine if the year is a leap year. Both of these formulae are easily found with a simple Google search. The third item you need is a simple array containing the number of days in each of the 12 months for a non-leap year.
Once you have these things, its trivial to determine the week day for each month of the year. Make sure to account for February 29 in a leap year. From there, you just need to create a function that primts out the monthly calendar in a form that looks similar to the calendar hanging on the wall. Try sketching out the desired layout on paper first and use that as a template for creating the appropriate format statements.
Might check out the doomsday algorithm. This would get you certain "dooms days" like Jan 31 is a doomsday, for 2008, that was a saturday. You can work backwords from there
Basically, there are two approaches:
The easy / pragmatical way: Solve the task, and forget about everything else. Here you may check documentation for mktime() (you'll find an example based on mktime below..).
The scientific / engineering way: Learn to know how it works! You can start at the great wikipedia article about the gregorian calendar. Read it, understand it, and write code that implements the underlying algorithms (which are known, no rocket science, it is possible). This will improve your skills a lot (in fact, you really should do such a thing, maybe not the calender but another topic, it will give you a big leap in understanding all things).
Now some pragmatic code to start with. mktime() has a great feature: it knows the calender details, and it accepts e.g. a date "2010-01-60" and will convert it into February 29th, 2010. But, this will only work for dates after 1970. It will not work for earlier dates (though I am not 100% sure, but it shouldn't work, because unix time starts at January 1st, 1970, but try with other dates, maybe mktime() is not restricted to unix time).
Pseudo-Code, this prints each day on a single line (YYYY-MM-DD):
void print_cal( int year ) {
static char weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
struct tm tm;
for( int day=0; day<365; ++day ) {
memset( &tm, 0, sizeof(tm) );
tm.tm_year = year - 1900;
tm.tm_mday = day;
mktime( &tm ); // modifies tm
printf( "%04d-%02d-%02d, %s\n", tm.tm_year, tm.tm_mon+1, tm.tm_mday, weekdays[tm.tm_wday] );
}
}
This code ignores leap years. You still have to adjust it to be correct for leap years! Also the result is not very pretty yet, just one line per day.
EDIT: added output of weekdays.
How do I calculate the time period between 2 dates in C (any library, etc.)?
The program should take two (local) dates as input and provide the duration period between them as output.
For example,
startDate = OCT-09-1976 and endDate = OCT-09-2008
should show a duration of 32 years.
startDate = OCT-09-1976 and endDate = DEC-09-2008
should show a duration of 32 years and 2 months.
Thanks.
Convert the dates into two struct tm structures with strptime
Difftime gives you the difference between the two in seconds.
Convert that into months etc with the code here (in C++, but the only C++ is for the string formatting, easy to change)
EDIT: as a commentor observed, that avoids the month issue. There is (GPL'd) code for
isodiff_from_secs that can be converted to do what you want, if you're happy with its assumption that months have 30 days. See Google codesearch and the description of the standard here
Doing the fully-correct solution which takes acccount of the true months between the actual days would be pretty complex. Is that required for your problem?
I did something very similar recently using Boost.Date_Time, and presenting the resulting function as C, but this of course requires using the C++ linker.
Actually, the example leaves a little to be desired - will the start and end dates always be on the same day of the month? If so you can ignore the day number end up with a trivial subtraction of the month and year numbers.
However if your dates can be anywhere in the month it might be a bit more tricky. Remember that not all months have the same number of days!
C difftime doesn't help you with month calculations, which is why I used Boost, though you may not have that option.