473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using timegm and gmtime_r functions in time.h header

hqprog
1
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard.

I need to use these two functions in myprog.c (on pc - see *** in code snippet below) which processes a large csv file into smaller csv files based on time (GMT, UTC). myprog.c was run on MAC OSX and but won't compile for me. Probably these two functions were defined in the time.h that was used. But they're not in my own time.h header.

BSD Library Functions Manual

TIMEGM

SO - - - I think I need to add these two functions into my time.h, because they are not there.

BTW --- I also found and added these two fields in the structure for tm per OSX description

char *tm_zone; /* abbreviation of timezone name */
long tm_gmtoff; /* offset from UTC in seconds */

AM I on the RIGHT TRACK to getting myprog.c to compile?
Thanks!

Expand|Select|Wrap|Line Numbers
  1. //   Program to process into CSV input files for loading into MySQL
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. // Min/Max
  8. #define min(a,b) ((a) < (b) ? (a) : (b))
  9. #define max(a,b) ((a) > (b) ? (a) : (b))
  10.  
  11. // Time structure for manipulating time values
  12. struct tm tm;
  13.  
  14. // Time of next operation
  15. time_t gmt;
  16.  
  17. // Tick data
  18. FILE * tick_file;      
  19. int    tick_count = 0; 
  20. time_t tick_gmt;       
  21. float  tick_bid;       
  22. float  tick_ask;       
  23.  
  24. // OHLC data
  25. typedef struct 
  26. {
  27.   FILE * file;      
  28.   int    count;     
  29.   time_t start_gmt; 
  30.   time_t end_gmt;   
  31.   float  open_bid;  
  32.   float  open_ask;  
  33.   float  high_bid;  
  34.   float  high_ask;  
  35.   float  low_bid;   
  36.   float  low_ask;   
  37.   float  close_bid; 
  38.   float  close_ask; 
  39. } ohlc_struct;
  40.  
  41. ohlc_struct minute_ohlc;
  42. ohlc_struct hour_ohlc;
  43. ohlc_struct day_ohlc;
  44. // Log output
  45. time_t stdout_gmt;
  46.  
  47. // ***THIS IS WHERE FUNCTION timegm IS USED
  48. // Open OHLC file and initialise variables
  49. open_ohlc (ohlc_struct * ohlc, char * pair, char * suffix, time_t period)
  50. {
  51.   char * filename = calloc (6 + strlen (pair) + strlen (suffix) + 1, 1);
  52.   strcpy (filename, "abcde_");
  53.   strcat (filename, pair);
  54.   strcat (filename, suffix);
  55.   ohlc->file = fopen (filename, "w");
  56.   if (ohlc->file == NULL)
  57.   {
  58.     printf ("Unable to open %s for output\n", filename);
  59.     exit (0);
  60.   }
  61.   ohlc->count = 0;
  62.   ohlc->start_gmt = timegm (&tm);
  63.   ohlc->end_gmt = ohlc->start_gmt + period - 1;
  64.   ohlc->open_bid = 0.0;
  65.   ohlc->open_ask = 0.0;
  66.   ohlc->close_bid = ohlc->high_bid = ohlc->low_bid = tick_bid;
  67.   ohlc->close_ask = ohlc->high_ask = ohlc->low_ask = tick_ask;
  68.   free (filename);
  69. }
  70.  
  71. // Read tick into variables
  72. int read_tick ()
  73. {
  74.   if (8 != fscanf (tick_file, "%d/%d/%d %d:%d:%d,%f,%f\n", &tm.tm_mday, &tm.tm_mon, &tm.tm_year, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tick_bid, &tick_ask)) return 0;
  75.  
  76.   tm.tm_mon -= 1;
  77.   tm.tm_year += 100;
  78.   tm.tm_wday = 0;
  79.   tm.tm_yday = 0;
  80.   tm.tm_isdst = 0;
  81.   tm.tm_gmtoff = 0;
  82.   tm.tm_zone  = "UTC";
  83.  
  84.   tick_gmt = timegm (&tm);
  85.   tick_count += 1;
  86.  
  87.   return 1;
  88. }
  89.  
  90. // Update OHLC with tick data
  91. update_ohlc (ohlc_struct * ohlc)
  92. {
  93.   if (tick_gmt == ohlc->start_gmt)
  94.   {
  95.     ohlc->close_bid = ohlc->high_bid = ohlc->low_bid = ohlc->open_bid = tick_bid;
  96.     ohlc->close_ask = ohlc->high_ask = ohlc->low_ask = ohlc->open_ask = tick_ask;
  97.   }
  98.   else
  99.   {
  100.     ohlc->close_bid = tick_bid;
  101.     ohlc->close_ask = tick_ask;
  102.     ohlc->high_bid  = max (tick_bid, ohlc->high_bid);
  103.     ohlc->high_ask  = max (tick_ask, ohlc->high_ask);
  104.     ohlc->low_bid   = min (tick_bid, ohlc->low_bid);
  105.     ohlc->low_ask   = min (tick_ask, ohlc->low_ask);
  106.   }
  107. }
  108.  
  109.  
  110. // Write OHLC and reset data
  111. write_ohlc (ohlc_struct * ohlc)
  112. {
  113.   time_t period;
  114.   period = ohlc->end_gmt - ohlc->start_gmt + 1;
  115.   if (ohlc->close_bid != 0.0)
  116.   {
  117.     if (ohlc->open_bid != 0.0)
  118.     {
  119.       gmtime_r (&ohlc->start_gmt, &tm);
  120.       fprintf (ohlc->file, "%04d%02d%02d%02d%02d%02d,%3.5f,%3.5f,%3.5f,%3.5f,%3.5f,%3.5f,%3.5f,%3.5f\n",
  121.            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
  122.            ohlc->open_bid, ohlc->open_ask, ohlc->high_bid, ohlc->high_ask,
  123.            ohlc->low_bid, ohlc->low_ask, ohlc->close_bid, ohlc->close_ask);
  124.       fflush (ohlc->file);
  125.       ohlc->count += 1;
  126.     }
  127.     ohlc->high_bid = ohlc->low_bid = ohlc->open_bid = ohlc->close_bid;
  128.     ohlc->high_ask = ohlc->low_ask = ohlc->open_ask = ohlc->close_ask;
  129.     ohlc->close_bid = 0.0;
  130.     ohlc->close_ask = 0.0;
  131.   }
  132.   ohlc->start_gmt += period;
  133.   ohlc->end_gmt   += period;
  134. }
  135.  
  136. // Main program
  137. main (int argc, char *argv[])
  138. {
  139.   // Check number of arguments
  140.   if (argc != 3)
  141.   {
  142.     printf ("Usage: fxticks2mysql csv-in pair\n");
  143.     printf ("       (generates 3 csv output files - minute, hour and day)\n");
  144.     exit (0);
  145.   }
  146.  
  147.   // Open tick input file
  148.   tick_file = fopen (argv[1], "r");
  149.   if (tick_file == NULL)
  150.   {
  151.     printf ("Unable to open %s for input\n", argv[1]);
  152.     exit (0);
  153.   }
  154.  
  155.   // Read first tick
  156.   if (read_tick () == 0)
  157.   {
  158.     printf ("Cannot read first line from %s\n", argv[1]);
  159.     exit (0);
  160.   };
  161.  
  162.   // Open ohlc files and initialise data structures
  163.   tm.tm_sec = 0;
  164.   open_ohlc (&minute_ohlc, argv[2], "_minute.csv", 60);
  165.   tm.tm_min = 0;
  166.   open_ohlc (&hour_ohlc, argv[2], "_hour.csv", 3600);
  167.   tm.tm_hour = 0;
  168.   open_ohlc (&day_ohlc, argv[2], "_day.csv", 86400);
  169.  
  170.   // Standard output GMT
  171.   stdout_gmt = timegm (&tm) + 86399;
  172.  
  173.   // Loop through GMT values
  174.   while (1)
  175.   {
  176.     // Determine next gmt to process
  177.     gmt = min (stdout_gmt, min (tick_gmt, min (minute_ohlc.end_gmt, min (hour_ohlc.end_gmt, day_ohlc.end_gmt))));
  178.  
  179.     // Update ohlc prices
  180.     if (gmt == tick_gmt)
  181.     {
  182.       update_ohlc (&minute_ohlc);
  183.       update_ohlc (&hour_ohlc);
  184.       update_ohlc (&day_ohlc);
  185.       if (read_tick () == 0) break;
  186.     }
  187.  
  188.     // End of minute, hour or day
  189.     if (gmt == minute_ohlc.end_gmt) write_ohlc (&minute_ohlc);
  190.     if (gmt == hour_ohlc.end_gmt) write_ohlc (&hour_ohlc);
  191.     if (gmt == day_ohlc.end_gmt) write_ohlc (&day_ohlc);
  192.  
  193.     // Day log
  194.     if (gmt == stdout_gmt)
  195.     {
  196.       gmtime_r (&gmt, &tm);
  197.       printf ("%04d-%02d-%02d: %d ticks, %d minutes, %d hours, %d days\n", 
  198.           tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tick_count, minute_ohlc.count, hour_ohlc.count, day_ohlc.count);
  199.       stdout_gmt += 86400;
  200.     }
  201.   }
  202.  
  203.   // Write final lines
  204.   if (tick_gmt >= minute_ohlc.start_gmt) write_ohlc (&minute_ohlc);
  205.   if (tick_gmt >= hour_ohlc.start_gmt) write_ohlc (&hour_ohlc);
  206.   if (tick_gmt >= day_ohlc.start_gmt) write_ohlc (&day_ohlc);
  207.  
  208. // ***THIS IS WHERE FUNCTION gmtime_r IS USED
  209.   // Final day log
  210.   gmtime_r (&gmt, &tm);
  211.   printf ("%04d-%02d-%02d: %d ticks, %d minutes, %d hours, %d days\n", 
  212.       tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tick_count, minute_ohlc.count, hour_ohlc.count, day_ohlc.count);
  213.  
  214.   // Close files
  215.   fclose (tick_file);
  216.   fclose (minute_ohlc.file);
  217.   fclose (hour_ohlc.file);
  218.   fclose (day_ohlc.file);
  219.  
  220.   exit (1);
  221. }
  222.  
Feb 21 '07 #1
0 3242

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Uncle_Alias | last post by:
I would like use some of the GD image functions, so I ran a couple of short scripts to see if it worked, such as: <?php /* Create a red square */ $image = imagecreate(200, 200); $colorRed =...
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
5
by: Tony Johansson | last post by:
Hello experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. Here is the whole section: It says" Because...
2
by: Steve McLellan | last post by:
Hi, We've found some pretty serious performance hits that we didn't expect in a mixed mode C++ application. The number crunching bits of our algorithms are compiled with #pragma unmanaged. They...
6
by: greek_bill | last post by:
Hi, I'm interested in developing an application that needs to run on more than one operating system. Naturally, a lot of the code will be shared between the various OSs, with OS specific...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
7
by: eskgwin | last post by:
I have this piece of code that takes a day of the year and converts it to the month and day of month: char start_mon; char start_day; int s_day = 84; time_t daysecs = (s_day * 86400) - 1;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.