BASIC User Group > Code Challenges

Calculate week number

<< < (2/4) > >>

AIR:

--- Quote from: jalih on November 06, 2018, 10:03:36 am ---Your challenge is to write a procedure that returns the week number for a given date. If week number belongs to a week numbering of the previous year then that week number should be returned instead of the zero week.

--- End quote ---

Sometimes the most obvious solution is one that isn't considered:


--- Code: Bash ---/bin/date -d "20100215" +%V
Returns Week #7 of 2010.   ;D ;D

AIR.

AIR:
Okay, all kidding aside, here's a function written in BaCon (compile with: bacon -y -o -D_GNU_SOURCE weekofdate.bac):


--- Code: Text ---PRAGMA INCLUDE <time.h> DECLARE tmm TYPE struct tm PROTO strptimePROTO strftime  FUNCTION getWeekNumber$(int yr, int mon, int day) TYPE STRING    LOCAL format$    PRINT yr,mon,day FORMAT "%d-%02d-%02d" TO format$    strptime(format$, "%Y-%m-%d", &tmm)    strftime(format$, SIZEOF(format$), "%V", &tmm)    RETURN format$END FUNCTION  PRINT "WEEK Number is: ", getWeekNumber$(2010,02,15) 
AIR.

John:
I swear SB had a WEEKNUMBER() or FORMATDATE() option to return the week number. No joy.  :-\

AIR:
You're getting old, John.  You have FORMATDATE and TIMEVALUE.  LOL.

Look in examples/testtime.bas.

Geez, I don't even use SB that much and I know this!!!

AIR.

AIR:
This is one of those things you could stick in a utility module.  I called it "timeutils".


--- Code: Script BASIC ---import timeutils.bas print timeutils::WeekNumber(2015,02,15) 
riveraa@nas:~/src/sb_ubuntu-64$ ./AppRun testweek.bas
07

Interface:


--- Code: Script BASIC ---/*READ THIS FILE AND CHANGE THE SOURCE WHEREVER YOU SEE COMMENTS STARTINGWITH THE WORD *TODO* WHEN YOU ARE FINISHED YOU CAN    FILE   : interface.c  HEADER : interface.h  BAS    : timeutils.bas  AUTHOR : AIR   DATE:    CONTENT:  This is the interface.c file for the ScriptBasic module timeutils----------------------------------------------------------------------------   UXLIBS:-lm -ldl---------------------------------------------------------------------------- DWLIBS: */   #include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "../../basext.h"  typedef struct _ModuleObject {  char a; /* You may delete this. It is here to make the initial interface.c compilable. */  }ModuleObject,*pModuleObject;   besVERSION_NEGOTIATE  return (int)INTERFACE_VERSION;besEND  besSUB_ERRMSG   switch( iError ){    case 0x00080000: return "ERROR HAS HAPPENED";    }  return "Unknown timeutils module error.";besEND  besSUB_START  pModuleObject p;   besMODULEPOINTER = besALLOC(sizeof(ModuleObject));  if( besMODULEPOINTER == NULL )return 0; besEND  besSUB_FINISH  pModuleObject p;   /*    YOU CERTAINLY NEED THIS POINTER TO FREE ALL RESOURCES THAT YOU ALLOCATED    YOU NEED NOT CALL besFREE TO FREE THE MEMORY ALLOCATED USING besALLOC    CLOSE ANY FILE THAT REMAINED OPEN, RELEASE DATABASE HANDLES AND ALL    OTHER RESOURCES INCLUDING MEMORY *NOT* ALLOCATED CALLING besALLOC  */  p = (pModuleObject)besMODULEPOINTER;  if( p == NULL )return 0;   return 0;besEND  /**=section WeekNumber=H title that goes into the BASIC documentation for this function detail here what the function does so that the BASIC programmercan understand how he/she can use it*/besFUNCTION(WeekNumber)  pModuleObject p;  struct tm tm;  char format[32];  int yr,mon,day;   p = (pModuleObject)besMODULEPOINTER;   besARGUMENTS("iii")  &yr,&mon,&day  besARGEND   sprintf(format,"%d-%02d-%02d",yr,mon,day);  strptime(format, "%Y-%m-%d", &tm);  strftime(format, sizeof(format),"%V", &tm);  besSET_RETURN_STRING(format);besEND  SLFST TIMEUTILS_SLFST[] ={ { "versmodu" , versmodu },{ "bootmodu" , bootmodu },{ "finimodu" , finimodu },{ "emsgmodu" , emsgmodu },{ "WeekNumber" , WeekNumber },{ NULL , NULL }  }; 
AIR.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version