ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  perl: How is the value of chartTime() created?
Posted by Donavon on Aug-03-2013 03:14
Hello...

I am working to implement a gantt chart and I'm trying to figure out how to populate the date variables.


my $startDate = [perlchartdir::chartTime(2013, 8, 16), perlchartdir::chartTime(2013,
      8, 30), perlchartdir::chartTime(2013, 9, 13)];


This means:
perlchartdir::chartTime(2013, 8, 16) = 63512208000

How is this "63512208000" value created?

I need to populate the $startDate variable without using chartTime().

Thank You,
~Donavon

  Re: perl: How is the value of chartTime() created?
Posted by Peter Kwan on Aug-05-2013 22:28
Hi Donavon,

In ChartDirector for Perl, time is represented as the number of calendar seconds elapsed since Jan 1, 0001 00:00:00. The seconds elapsed is computed based on the following assumptions:

1. One year has 365 days, except years divisible by 4 which would have 366 days.

2. One day has 24 hours.

3. One hour as 64 minutes. One minute has 60 seconds.

The "perlchartdir::chartTime(2013, 8, 16) = 63512208000" means 63512208000 seconds has elapsed from Jan 1, 0001 00:00:00 to Aug 16, 2013 00:00:00.

For your case, you can use the following method:

For a given date, compute the seconds elapsed since 2010-1-1 00:00:00 using your own code, then add 63397900800 (this number is the seconds elasped from Jan 1, 0001 00:00:00 to 2010-1-1 00:00:00).

Hope this can help.

Regards
Peter Kwan

  Re: perl: How is the value of chartTime() created?
Posted by Donavon on Aug-06-2013 05:22
Thank You...

I also figured out a way to loop Chart Directors chartTime() function.  Hope this helps some others.

It would be cool if this could be worked into a map statement.

perl:
my @dateSeries = ("2013-08-08", "2013/08/15-12:30:99", "2012.12.30", "2012 05 10");
my @chartDirectorDateSeries = ();

foreach my $dateTime (@dateSeries) {
    my ($year, $month, $day, $hour, $minute, $second) = split(/\\D/, $dateTime);
    my $chartDirectorDate = perlchartdir::chartTime($year, $month, $day, $hour, $minute, $second);
    push(@chartDirectorDateSeries, $chartDirectorDate);
}

  Re: perl: How is the value of chartTime() created?
Posted by William Fergus Martin on May-07-2014 18:48
Thanks for that Donavon. That really helped me a lot.

As a thank you and for anyone else who needs it, I dug around and found a way to do it with map.

I got the idea from here stackoverflow.com
http://stackoverflow.com/questions/4215456/how-can-i-apply-a-function-to-a-list-using-map

They show an example of using an anonymous subroutine with map:
my $squared = sub {
        my $arg = shift();
        return $arg ** 2;
  };
my @list = map { &$squared($_)  } 0 .. 12;

---
I changed it to this:

use FinanceChart;

my @ary = ("2013-09-30", "2013-10-01", "2013-10-02", "2013-10-03");
my @list = map { &$CD_CharTime($_)  } @ary;
print "@list\\n";

my $CD_CharTime = sub {
     my $arg = shift();
     my ($year, $month, $day) = split(/\\-/, $arg);
     my $new_datetime = perlchartdir::chartTime($year, $month, $day);
     return $new_datetime;
};

gives:
63516096000 63516182400 63516268800 63516355200

The values in @ary get passed to the curly brackets as the special variable $_;
$CD_CharTime is defined as an anonymous subroutine, which we can call via &$CD_CharTime().
$CD_CharTime takes the value passed to it, puts it in $arg and uses perlchartdir::chartTime (a function withinChartDirector) to convert it to ChartDirector time format.

We pass the variable $_ to $CD_CharTime and it will return the date values in the right format, as so:
&$CD_CharTime($_)

map will output the results as in array context and they end up in @list.

---
to pass to $timeStamps which looks to me like an array reference we put square brackets round the whole right hand side of the equation, thusly:

my $timeStamps = [ map { &$CD_CharTime($_) } @ary ];

print "@{$timeStamps}\\n";
# the {$array_name} de-references the array so we can access it

gives:
63516096000 63516182400 63516268800 63516355200

----
In my case I use stock data. Using yahoo as an example:

yahoo daily stock data is like this:
date, o,h,l,c, volume and adjclose
2014-05-01,592,594.8,586.36,591.48,8721700,591.48

I put data from yahoo into an array of arrays (where each set of values for the day; date, o,h,l,c, volume and adjclose, are an array ref in an array).

my @AoA = (
           [ "2014-05-05","590.14","601","590","600.96","10252300","600.96"],
  [ "2014-05-02","592.51","594.2","589.71","592.58","6839700","592.58" ],
    ....
    );


To complicate things even more, that array of arrays is actually an array reference.

my $AoA_ref= [
[ "2014-05-05","590.14","601","590","600.96","10252300","600.96"],
  [ "2014-05-02","592.51","594.2","589.71","592.58","6839700","592.58" ],
...
];

(If you want to know more about arrays of arrays go here:
http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/pod/perllol.html)


I have to pass dates to $timeStamps like this:

my $timeStamps = [ map { &$CD_CharTime($_->[0]) } @{$myAoA_ref} ];

$myAoA_ref  is the reference to my array of arrays.
@{$myAoA_ref}  this de-references the array of arrays so we an access it.
($_->[0])  accesses the first item in each array with the array of arrays, which are the date values, and passes these to the $CD_CharTime anonymous subroutine.
CD_CharTime  is the little anonymous subroutine shown earlier, which converts the date to the format which $timeStamps needs.
map passes the resulting values as an array.
The [  ] around the right hand side of the equation makes the results into an anonymous array.
The left hand side 'my $timeStamps =' makes $timeStamps the reference to the anonymous array.

---
If using an array of arrays but not an as an array reference you would just do this:
Assuming dates values are first item in each sub array.

my $timeStamps = [ map { &$CD_CharTime($_->[0]) } @myAoA ];

---

Hope this helps somebody.
William
"Forgiveness is an act of Power" - http://forgiveness-is-power.com/