[futurebasic] Re: [FB] calculating the day of the year

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2006 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Sun, 26 Nov 2006 03:23:34 -0500
Max asked:

>While we're at it here why not get the Month, Year, Day of Week, Day 
>of the Month, the Time as, Seconds, Minutes and Hour, etc. I know 
>these routines are also a part of the system but I do not know where 
>to find them.

Max,

Brian pointed you in one direction with the subset of CFDate.h 
functions that have been translated into FB as I used in my first 
example. Run that example in the FB Editor and Command-click on fn 
CFAbsoluteTimeGetCurrent. That takes you to FB's 
"CFGregorianDate.Incl."

Scroll to the top of that include, and you will find this remark from RP:

    // last mod  RP  29 October 2005
    // Gregorian date routines from CFDate.h

Thus FB's "CFGregorianDate.Incl" is, alas, just a subset of functions 
taken from Apple's CFDate.h header. The full body of CFDate.h is not 
yet available to FB users. Of course, you can translate you own 
Toolbox functions which runs the the gamut from easy to machine 
language complex. (At the bottom of FB's "CFGregorianDate.Incl," take 
a look at the glue functions Robert Purves has written to make things 
work in FB!)

Here's another approach using the Unix date function that, to be 
frank, I find much easier to code. But I am aware that some here have 
an aversion to CLI. So you pays your money and takes your choice.

A Kia and a Ferrari will both get you from Point A to Point B. But 
the ride will sure be different.

Best,

Ken


/*

Demo in applying man page instructions

    Ken Shmidheiser
    Somerset, KY 42501
    November 26, 2006

    The OS X man page for the Unix "date" command
    (to access it type "man date" in the Terminal)
    draws assumptions that make it kind of lame.

    Take a look at this date man page which spells
    out the options in a form that's more human
    understanbable:

       http://unixhelp.ed.ac.uk/CGI/man-cgi?date

    Here's an edited list of format options from this
    man page. These are preceded by a "+" sign when
    used with the Unix data command, e.g.:

       date +%d

       [returns the day of the month as integer]

    Run it it the Terminal and see.

    Note that these options can be strung together
    for custom formatting.

%%   a literal %
%a   abbreviated weekday name (Sun..Sat)
%A   full weekday name, variable length (Sunday..Saturday)
%b   abbreviated month name (Jan..Dec)
%B   full month name, variable length (January..December)
%c   date and time (Sat Nov 04 12:02:33 EST 1989)
%C   century (year/ 100 & truncated to an integer) [00-99]
%d   day of month (01..31)
%D   date (mm/dd/yy)
%e   day of month, blank padded ( 1..31)
%F   same as %Y-%m-%d
%g   the 2-digit year corresponding to the %V week number
%G   the 4-digit year corresponding to the %V week number
%h   same as %b
%H   hour (00..23)
%I   hour (01..12)
%j   day of year (001..366)
%k   hour ( 0..23)
%l   hour ( 1..12)
%m   month (01..12)
%M   minute (00..59)
%n   a newline
%p   Uppercase AM or PM indicator (blank in many locales)
%P   Lowercase am or pm indicator (blank in many locales)
%r   time, 12-hour (hh:mm:ss [AP]M)
%R   time, 24-hour (hh:mm)
%s   seconds since `00:00:00 1970-01-01 UTC' (a GNU extension)
%S   second (00..60); 60 is needed to accommodate a leap second
%t   a horizontal tab
%T   time, 24-hour (hh:mm:ss)
%u   day of week (1..7); 1 represents Monday
%U   week number of year with Sunday as first day of week (00..53)
%V   week number of year with Monday as first day of week (01..53)
%w   day of week (0..6); 0 represents Sunday
%W   week number of year with Monday as first day of week (00..53)
%x   date representation (mm/dd/yy)
%X   time representation (%H:%M:%S)
%y   last two digits of year (00..99)
%Y   year (2006...)
%z   RFC-822 style numeric timezone (-0500) (a nonstandard extension)
%Z   time zone (e.g., EDT); nothing if time zone is not determinable

*/

_vanilla$         = ""   // The default date command
_monthNumber$     = "+%m"
_monthName$       = "+%B"
_thisYear$        = "+%Y"
_dayOfYearNumber$ = "+%j"
_weekdayName$     = "+%A"
_weekdayNumber$   = "+%w"
_dayOfMonth$      = "+%d"
_currentTime$     = "+%X"
_hourOfDay$       = "+%H"
_seconds$         = "+%S"
_hourAndMinutes$  = "+%H:%M"
_hourMinsSecs$    = "+%H:%M:%S"
_hour24HourClock$ = "+%H"
_hour12HourClock$ = "+%I"
_timeZone$        = "+%Z"
_meridian $       = "+%p"
_dateToday$       = "+%x"
_wholeEnchilada$  = "+%c"

local fn TimeCommand$( cmd as str255 )
dim as str255 s, result

open "Unix", 2, "date " + cmd
do : line input #2, s
if (s[0]) then result = s : exit do
until eof (2) : close  2

end fn = result

window 1
def tab 8
print "Default date returns this: ",   fn TimeCommand$(_vanilla$        )
print "The number of this month is: ", fn TimeCommand$(_monthNumber$    )
print "The name of this month is: ",   fn TimeCommand$(_monthName$      )
print "The current year is: ",         fn TimeCommand$(_thisYear$       )
print "The day of the year is: ",      fn TimeCommand$(_dayOfYearNumber$)
print "Today's full name is: ",        fn TimeCommand$(_weekdayName$    )
print "Today's weekday number is: ",   fn TimeCommand$(_weekdayNumber$  )
print "The day of the month is: ",     fn TimeCommand$(_dayOfMonth$     )
print "The 24-hour clock time is: ",   fn TimeCommand$(_currentTime$    )
print "The hour of the day is: ",      fn TimeCommand$(_hourOfDay$      )
print "Elapsed seconds this minute: ", fn TimeCommand$(_seconds$        )
print "Hour of 24-hour clock: ",       fn TimeCommand$(_hour24HourClock$)
print "Hour of 12-hour clock: ",       fn TimeCommand$(_hour12HourClock$)
print "The hour and minutes are: ",    fn TimeCommand$(_hourAndMinutes$ )
print "Hour, minutes & seconds are: ", fn TimeCommand$(_hourMinsSecs$   )
print "The current time zone is: ",    fn TimeCommand$(_timeZone$       )
print "Is it morning or afternoon?: ", fn TimeCommand$(_meridian$       )
print "Today's full date is: ",        fn TimeCommand$(_dateToday$      )
print "The whole Enchilada: ",         fn TimeCommand$(_wholeEnchilada$ )

include "Subs Quick Event Loop.Incl"