week of year and PureSMTP_SetXMailer to current domain name

Share your advanced PureBasic knowledge/code with the community.
alokdube
Enthusiast
Enthusiast
Posts: 148
Joined: Fri Nov 02, 2007 10:55 am
Location: India
Contact:

week of year and PureSMTP_SetXMailer to current domain name

Post by alokdube »

Guys working with databases and auto email alerts may find this useful

Here is a procedure which gives week number from date:

Code: Select all

Procedure WeekNumberFromDate()
Weeknum=1

For i=1 To DayOfYear(Date())

day_of_week=DayOfWeek(AddDate( Date(Year(Date()),01,01,1,1,1), #PB_Date_Day , i-1))

If (day_of_week=0) And Not(i=1)
Weeknum=Weeknum+1
;;PrintN ("day of week=0")
EndIf
;;PrintN ("Weeknum:"+Str(Weeknum))
Next i
ProcedureReturn Weeknum
EndProcedure
Here is another snippet to set the xmailer in the puresmtp library to the current domain name

it relys on the DNSAPIs at
http://www.purebasic.fr/english/viewtop ... highlight=

Code: Select all

LoadDnsApi(0) 
PureSMTP_SetContentType("text/html; charset=iso-8859-1")
ExamineIPAddresses()
;Debug IPString(NextIPAddress())
;Debug  ReverseDnsQuery(NextIPAddress())
PureSMTP_SetXMailer(ReverseDnsQuery(NextIPAddress()))
CloseLibrary(0)
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: week of year and PureSMTP_SetXMailer to current domain n

Post by jesperbrannmark »

I think it should start with
Weeknum=0
instead of
Weeknum=1

I don't know this, but here in Europe - a week start with monday and in US on sunday. I guess this also mean that the week number switch on sunday in US and on monday in europe.

Updated code: (not taking sunday/monday issue in count):

Code: Select all

Procedure WeekNumberFromDate(datum.i)
  Weeknum=0  
  For i=1 To DayOfYear(datum.i)    
    day_of_week=DayOfWeek(AddDate(Date(Year(datum.i),01,01,1,1,1), #PB_Date_Day , i-1))    
    If (day_of_week=0) And Not(i=1)
      Weeknum=Weeknum+1
    EndIf
  Next i
  ProcedureReturn Weeknum
EndProcedure
Debug WeekNumberFromDate(Date(1993,01,31,0,0,0))
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: week of year and PureSMTP_SetXMailer to current domain n

Post by blueznl »

That would depend on the country, in the Netherlands we consider Su Mo Tu We Th Fr Sa a week, so weeks here start on Sunday.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: week of year and PureSMTP_SetXMailer to current domain n

Post by infratec »

Code: Select all

Enumeration
  #Sunday
  #Monday
  #Tuesday
  #Wednesday
  #Thursday
  #Friday
  #Saturday
EndEnumeration

Procedure WeekNumberFromDate(datum.i, WeekStartDay = #Monday)
  Weeknum = 0 
  For i = 1 To DayOfYear(datum)
    day_of_week = DayOfWeek(AddDate(Date(Year(datum),01,01,1,1,1), #PB_Date_Day , i - 1))
    If day_of_week = WeekStartDay And i <> 1
      Weeknum + 1
    EndIf
  Next i
  ProcedureReturn Weeknum
EndProcedure

Debug WeekNumberFromDate(Date(2011,05,01,0,0,0))
Debug WeekNumberFromDate(Date(2011,05,01,0,0,0), #Sunday)
Satisfied :?:

Bernd
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: week of year and PureSMTP_SetXMailer to current domain n

Post by infratec »

But it is still not 100% correct:

Code: Select all

Debug WeekNumberFromDate(Date(2009,01,01,0,0,0))
results in 0 :(

But it was week 1. (In germany and in USA)

Bernd
Post Reply