Is there a julian date function in APEX, - apex-code

Is there a julian date function in APEX,
For example today [May 28 2013] has the julian date 2456442
Thanks

Here is the answer:
public static Integer getJulianDate(integer I,integer J,integer K){
// I= YEAR
// J= MONTH
// K= DAY
return ( K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12) /12-3*((I+4900+(J-14)/12)/100)/4 );
}

Related

I have 2 dates in Laravel, how can I run a auto generate post to go through between two dates with time period?

I want to create a Question (question table) that has startDate and endDate and period that is in hour (1-2-3-... hours) and user_id
Now I want to auto generate question rows in another table (Question_user) between two dates with period
ex:
startDate = 2019-11-01 00:00:00
endDate = 2019-11-20 12:00:00
period = 12
Now I want to create questions in (Question_user) at:
2019-11-01 00:00:00
2019-11-01 12:00:00
2019-11-02 00:00:00
...
How can I do this?
*Date format -->Year/Month/Day
in your controller , add below function :
protected function GenerateDateRange(Carbon $start_date, Carbon $end_date,$period)
{
$dates = [];
for($date = $start_date; $date->lte($end_date); $date->addHours($period)) {
$dates[] = $date->format('Y-m-d H:i:s');
}
return $dates;
}
and use it like this :
$dateRanges=$this->GenerateDateRange(Carbon::parse("2019-11-01 00:00:00"),Carbon::parse("2019-11-20 12:00:00"),12);
then for loop through $dateRanges and insert your data .

Swift 2.0+ finding the day of the week a month starts

I am using Swift 2.0+ and need it to be compatible with iOS 8.0+. I want to find out what day of the week a month starts. For example, November 15 would return Sunday, December 15 would return Tuesday, and January 16 would return Friday. I think the best approach would be to get the current day of the week and day of the month then count backwards until you reach 1 and get that day. Currently I have
// Returns the day of the week as an integer 1 being Sunday and 7 being Saturday
func dayOfWeek() -> Int {
let components = calender.component(NSCalendarUnit.Weekday, fromDate: t)
return components
}
Which calculates the current day of the week, after that I hit a roadblock.
I found the answer to be
// This function should return the value I need 0 being Sunday and 6 being Saturday
func dayMonthStarted() -> Int {
// 0 -> Sunday 6 -> Saturday
var component = calender.component(NSCalendarUnit.Day, fromDate: date)
var day = dayOfWeek()
while component > 1 {
component--
if day == 0 {
day = 6
} else {
day--
}
}
return day - 1
}

Local system based timestamp on 00:00:00 of particular day

How do i get time in seconds from epoch at 00:00:00 of a particular day on local machine.I want to trigger a service on 00:00:00 of a particular day and so i wanted to know time in seconds from epoch on local machine when system time reach to 00:00:00.
I have to do it using C language .
Any help will be appreciated .
That's what the mktime function is for. See the documentation here.
struct tm time_str;
time_str.tm_year = 2014 - 1900; /* year minus 1900 */
time_str.tm_mon = 8 - 1; /* month minus 1 */
time_str.tm_mday = 11;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 0;
time_str.tm_isdst = -1;
time_t seconds_since_epoch = mktime(&time_str);

Find the specific day of a given month and given year

is there a Perl module which could give me for input month and year, let say, 06-2005, what the last day of this month for this year is? For this example, it is easy, because June always has 30 days, so the last day will be 30-06-2005. But it is not the case for February. So, if I have 02-1997, I would like to know whether to return 28-02-1997 or 29-02-1997. Thanks in advance.
Yes, there is a subroutine Days_in_Month in the Date::Calc module.
use strict;
use warnings;
use Date::Calc qw/Days_in_Month/;
for my $m (1 .. 12) {
print Days_in_Month(2005, $m), "\n";
}
OUTPUT
31
28
31
30
31
30
31
31
30
31
30
31
The last day in a given month is the "0th" day of the following month. mktime() takes a 0-based month and 1900-based year number, and returns an epoch timestamp.
use POSIX qw( mktime strftime );
sub last_day {
my ( $year, $mon ) = #_;
return mktime(0,0,0, 0, $mon, $year-1900);
}
You can pass that to localtime or strftime.
say scalar localtime last_day(2005, 5)
'Tue May 31 00:00:00 2005'
say scalar localtime last_day(2005, 6)
'Thu Jun 30 00:00:00 2005'
say scalar localtime last_day(1997, 2)
'Fri Feb 28 00:00:00 1997'
say scalar localtime last_day(2012, 2)
'Wed Feb 29 00:00:00 2012'
say (localtime last_day(1997, 2))[3]
'28'
say strftime "%d", localtime last_day(1997, 2)
'28'
As others said, use DateTime:
use DateTime;
my $dt = DateTime->last_day_of_month('year'=>2000, 'month'=>2);
print $dt->ymd; # '2000-02-29'
While DateTime may not be the fastest module for handling dates/times, it's definitely the most complete one. It's also the only one that handles the various quirks of date/time math correctly.

How do I convert a time to tm struct instead of CTime class

I currently have code that creates a CTime object from a defined value.
#define TIME_VALUE 0x301DDF00 // Aug 1, 1995 # 04:00:00
CTime t = CTime( TIME_VALUE );
This creates the desired date of Aug 1, 1995 04:00:00
I can no longer use CTime so I am trying to use time_t and tm instead. Since the CTime constructor takes in the number of seconds since Jan 1, 1970 and time_t represents the number of seconds since Jan 1, 1970, I tried to use the following code.
#define TIME_VALUE 0x301DDF00 // Aug 1, 1995 # 04:00:00
time_t tmpTime = TIME_VALUE;
struct tm createTime;
if( localtime_s( &createTime, &tmpTime ) == S_OK )
{
// Use createTime
}
createTime ends up as August 1, 0095 04:00:00. How am I supposed to go from the defined value to a time_t and tm successfully?
Thanks in advance.
Sorry. I didn't look at the tm documentation closely enough. The year is the actual year minus 1900 and the month is zero based. I got it now.

Resources