转换时间格式

时间:2014-03-11 09:19:46

标签: c date

在下面的代码中,我试图通过将UTC转换为本地时间来打印时间。即加入5:30分钟。现在我想将当地时间转换为UTC。如果我在5:30分钟下属,实际和变化之间有45个差异。

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
static int MNTH_DAYS[12]={
  31,28,31,30,31,30,31,31,30,31,30,31};
float tzone=5.5;
static long tzone_offset_s = tzone*3600UL;
static int dd;
static int mm;
static int yy;
static int s;
static int h;
static int m;
static int wkDay;
int CENTURY=2000;
static int local_day;
static int local_month;
static int local_year;
int local_s;
static int local_h;
static int local_m;


void setup(){
  Wire.begin();
  Serial.begin(9600);
  s=10;
  m=9;
  h=5;
  yy=14;
  mm=3;
  dd=15;
  wkDay;
  rtcWrite();      
}

void loop(){
  getRTCDateTime();
  convertUTCtoLocal();
  Print_Date_time();

  delay(1000);
}

void rtcWrite(){    
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(s));
  Wire.write(decToBcd(m));
  Wire.write(decToBcd(h));
  Wire.write(decToBcd(wkDay));
  Wire.write(decToBcd(dd));
  Wire.write(decToBcd(mm));
  Wire.write(decToBcd(getLastTwoDigOfYear(yy)));

  Wire.write(zero); //start

  Wire.endTransmission();
}

int getLastTwoDigOfYear(int y){
  return(y%100); 
}

byte decToBcd(byte val){
  // Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void getRTCDateTime()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  s = bcdToDec(Wire.read());
  m = bcdToDec(Wire.read());
  h = bcdToDec(Wire.read() & 0b111111); //24 hour time
  wkDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  dd = bcdToDec(Wire.read());
  mm = bcdToDec(Wire.read());
  yy = CENTURY + bcdToDec(Wire.read());
}

void convertUTCtoLocal(){    
  long no_s_day_start= h*3600UL + m*60 + s; //no of seconds since day start
  long day_change_cutoff_s = 86400UL-tzone_offset_s;//max utc before changing date

  long additional_s;//additional seconds after day change

  local_year = yy;

  if(no_s_day_start>day_change_cutoff_s){
    if(dd+1 > MNTH_DAYS[mm-1]){
      local_day = 1;
      if(mm==12){
        local_month=1;
        local_year = yy+1;
      }
      else
        local_month = mm+1;
    }
    else{
      local_day=dd+1;
      local_month=mm;
    }

    additional_s = no_s_day_start - day_change_cutoff_s; 
    convertSecondsToLocalHMS(additional_s);
  }
  else{
    local_day = dd;
    local_month = mm;
    convertSecondsToLocalHMS(no_s_day_start+tzone_offset_s);
  }
}

void convertSecondsToLocalHMS(long secs){    
  local_h = secs/3600; //hour
  local_m=(secs%3600)/60;//min
  local_s = secs - local_h*3600UL - local_m*60;
}

int sign(int v)
{
  return v >= 0 ? 1 : -1;
}

void Print_Date_time(){
  Serial.print("Local Date:");
  Serial.print(local_day);
  Serial.print("/");
  Serial.print(local_month);
  Serial.print("/");
  Serial.print(local_year);
  Serial.print("\t");
  Serial.print("local_time is:");
  Serial.print(local_h);
  Serial.print("-");
  Serial.print(local_m);
  Serial.print("-");
  Serial.println(local_s);   
}

此函数从我将值存储到h,m,s

void setCustomDateTime(int date_time[])
{
  dd=date_time[0];
  mm=date_time[1];
  yy=date_time[2];
  h=date_time[3]-5;
  if(date_time[4]>0 && date_time[4]<=30)
  {
    m=date_time[4]-30;
  }
  else
  {
    m=date_time[4]-30;
    m=sign(m);
  }
  s=date_time[5];
  wkDay =2;
  rtcWrite(s,m,h,wkDay,dd,mm,yy);  
}
void rtcWrite(int sec1,int min1,int hour1, int  wkday1,int dd1,int mm1,int yy1)
{  
  rtcSetup();
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator
  Wire.write(decToBcd(sec1));
  Wire.write(decToBcd(min1));
  Wire.write(decToBcd(hour1));
  Wire.write(decToBcd(wkday1));
  Wire.write(decToBcd(dd1));
  Wire.write(decToBcd(mm1));
  // Wire.write(decToBcd(yy1);
  Wire.write(decToBcd(getLastTwoDigOfYear(yy1)));
  Wire.write(zero); //start
  Wire.endTransmission();
  getRTCDateTime();
  convertUTCtoLocal();
}

我在论坛中查了一下:这里如何分离出浮点并存储为整数。 Ntime以浮点形式出现

Time1=(3600*5)+(60*30);
Time2=(3600*date_time[3])+(60*date_time[4])+date_time[5];
Ntime=Time2-Time1;
Ntime=((Ntime%60)/100+(Ntime/60));

2 个答案:

答案 0 :(得分:0)

你试过吗

tzset();

检查This

希望这会有所帮助。 请留下反馈。

答案 1 :(得分:0)

建议先修复以下内容。

  1. 修复年度计算

    int getLastTwoDigOfYear(int y){
      // return(y%1000); 
      return(y%100); 
    }
    
  2. 抵消可能是负数。 if(no_s_day_start>day_change_cutoff_s){处理+更改日期,但没有代码 - 更改日期。

  3. if(date_time[4]>=30 && date_time[4]<=30)很好奇。怀疑你想要 if(date_time[4]>=30)